From 49f68949954933f8b6c40e36c342b12f8e177337 Mon Sep 17 00:00:00 2001 From: artem Date: Sat, 22 Jul 2023 19:20:40 +0300 Subject: [PATCH] update gitignore, requirements and added test backend --- .gitignore | 3 +- connection_db/conn.py | 43 +++++++++++++ requirements.txt | 2 + test_db/connection_to_db.py | 119 ++++++++++++++++++++++++++++++++++++ test_db/test_endpoint.py | 99 ++++++++++++++++++++++++++++++ 5 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 connection_db/conn.py create mode 100644 test_db/connection_to_db.py create mode 100644 test_db/test_endpoint.py diff --git a/.gitignore b/.gitignore index f5e96db..2f2c3c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -venv \ No newline at end of file +venv +connection_db/conn.py \ No newline at end of file diff --git a/connection_db/conn.py b/connection_db/conn.py new file mode 100644 index 0000000..4082f72 --- /dev/null +++ b/connection_db/conn.py @@ -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}') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 8f4d514..472d1d0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/test_db/connection_to_db.py b/test_db/connection_to_db.py new file mode 100644 index 0000000..c021b16 --- /dev/null +++ b/test_db/connection_to_db.py @@ -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 + diff --git a/test_db/test_endpoint.py b/test_db/test_endpoint.py new file mode 100644 index 0000000..efc6423 --- /dev/null +++ b/test_db/test_endpoint.py @@ -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()}' \ No newline at end of file