Skip to content

Commit

Permalink
update gitignore, requirements and added test backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mikyyL committed Jul 22, 2023
1 parent 69f2b0a commit 49f6894
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
venv
venv
connection_db/conn.py
43 changes: 43 additions & 0 deletions connection_db/conn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import psycopg2
from sshtunnel import SSHTunnelForwarder


ssh_host = '165.22.78.188'
ssh_port = 22
ssh_username = 'amikulich'
ssh_password = '/Users/artemmikulich/.ssh/id_ed25519'

db_host = 'tldb-postgresql-fra1-81116-do-user-14346310-0.b.db.ondigitalocean.com'
db_port = 25060
db_username = 'trainlab'
db_password = 'AVNS_yXtS-TsJq6YJmCAe19y'
db_name = 'trainlab'
try:
def ssh_tunnel_and_db_connection(func):
def wrapper(*args, **kwargs):
with SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_username,
ssh_password=ssh_password,
remote_bind_address=(db_host, db_port)) as tunnel:
print('Connected to server')
conn = psycopg2.connect(
host='localhost',
port=tunnel.local_bind_port,
user=db_username,
password=db_password,
database=db_name
)

# Передаем соединение в декорируемую функцию
result = func(conn)

# Закрываем соединение после выполнения функции
conn.close()

return result

return wrapper

except BaseException as e:
print(f'connection failed. {e}')
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ tomli==2.0.1
sshtunnel~=0.4.0
paramiko~=3.2.0
requests~=2.31.0
psycopg2==2.9.6
sshtunnel
119 changes: 119 additions & 0 deletions test_db/connection_to_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from connection_db.conn import ssh_tunnel_and_db_connection


@ssh_tunnel_and_db_connection
def check_text_where_id_1(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=1"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_2(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=2"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_3(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=3"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_4(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=4"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_5(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=5"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_6(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=6"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_7(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=7"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_8(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=8"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result


@ssh_tunnel_and_db_connection
def check_text_where_id_9(connection):
cursor = connection.cursor()
new_query = """SELECT front_id FROM frontend_data WHERE id=9"""
cursor.execute(new_query)
front_id_result = cursor.fetchone()[0]
query = "SELECT text FROM frontend_data WHERE front_id=%s"
cursor.execute(query, (str(front_id_result),))
result = cursor.fetchone()[0]
cursor.close()
return result

99 changes: 99 additions & 0 deletions test_db/test_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import requests
from test_db.connection_to_db import *

def test_response():
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
assert response.status_code == 200, f'Expected status code 200, but was given {response.status_code}'


# with invalid url
def test_response_negative():
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/11'
response = requests.get(url)
assert response.status_code != 200, f'Expected status code not 200, but was given {response.status_code}'


@ssh_tunnel_and_db_connection
def test_text_id_1_1(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_1(connection)
text_from_api = response_json['1.1']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'


@ssh_tunnel_and_db_connection
def test_text_id_1_2(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_2(connection)
text_from_api = response_json['1.2']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'


@ssh_tunnel_and_db_connection
def test_text_id_1_3(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_3(connection)
text_from_api = response_json['1.3']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'


@ssh_tunnel_and_db_connection
def test_text_id_1_4(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_4(connection)
text_from_api = response_json['1.4']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'

@ssh_tunnel_and_db_connection
def test_text_id_1_5(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_5(connection)
text_from_api = response_json['1.5']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'

@ssh_tunnel_and_db_connection
def test_text_id_1_6(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_6(connection)
text_from_api = response_json['1.6']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'

@ssh_tunnel_and_db_connection
def test_text_id_1_7(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_7(connection)
text_from_api = response_json['1.7']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'

@ssh_tunnel_and_db_connection
def test_text_id_1_8(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_8(connection)
text_from_api = response_json['1.8']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'

@ssh_tunnel_and_db_connection
def test_text_id_1_9(connection):
url = 'https://back-test-4zwpv.ondigitalocean.app/front/pages/1'
response = requests.get(url)
response_json = response.json()
text_from_db = check_text_where_id_9(connection)
text_from_api = response_json['1.9']
assert text_from_api == text_from_db, f'Expected text {text_from_db} but was {text_from_api()}'

0 comments on commit 49f6894

Please sign in to comment.