def test_psycopg2_args_dict(self): config, intxn_table_map = parse_test_db_config() adapter = Psycopg2Adapter( Psycopg2Adapter.to_psycopg_args(config), intxn_table_map ) adapter.db_connection.cursor().execute("SELECT 1")
def test_psycopg2_args_invalid_config_fail(self): config, intxn_table_map = parse_test_db_config() self.assertRaisesRegex( ValueError, "unknown datatype for connect_params - expect dict or str, got .+", Psycopg2Adapter, 1, intxn_table_map )
def test_get_db_dict_cursor(self): config, _ = parse_test_db_config() connection_args = Psycopg2Adapter.to_psycopg_args(config) conn = psycopg2.connect(**connection_args) set_db(conn) cursor = get_db_dict_cursor() self.assertIsInstance(cursor, psycopg2.extensions.cursor) cursor.execute("SELECT 1;") disconnect_db()
def test_execute_db_correct_stmt_on_closed_connection(self): config, _ = parse_test_db_config() connect_db(config) tag2domain_api.app.util.db._db_conn.close() rows = execute_db("SELECT 1;") assert len(rows) == 1 assert rows[0][0] == 1 disconnect_db()
def test_set_db(self): config, _ = parse_test_db_config() connection_args = Psycopg2Adapter.to_psycopg_args(config) conn = psycopg2.connect(**connection_args) set_db(conn) db_conn = get_db() self.assertIsInstance(db_conn, psycopg2.extensions.connection) assert tag2domain_api.app.util.db._db_config is None assert tag2domain_api.app.util.db._config is None
def test_execute_db_correct_stmt(self): config, _ = parse_test_db_config() connection_args = Psycopg2Adapter.to_psycopg_args(config) conn = psycopg2.connect(**connection_args) set_db(conn) rows = execute_db("SELECT 1;") assert len(rows) == 1 assert rows[0][0] == 1 disconnect_db()
def test_get_stmt_unknown_stmt_fail(self): config, intxn_table_map = parse_test_db_config() connection_args = Psycopg2Adapter.to_psycopg_args(config) conn = psycopg2.connect(**connection_args) adapter = Psycopg2Adapter(conn, intxn_table_map) self.assertRaises( ValueError, adapter.get_compiled_stmt, "some unknown stmt", "domain" )
def test_get_stmt_unknown_type_fail(self): config, intxn_table_map = parse_test_db_config() connection_args = Psycopg2Adapter.to_psycopg_args(config) conn = psycopg2.connect(**connection_args) adapter = Psycopg2Adapter(conn, intxn_table_map) self.assertRaises( AdapterDBError, adapter.get_compiled_stmt, "get_open_tags", "some unknown type" )
def test_execute_failing_statement(self): config, _ = parse_test_db_config() connect_db(config) tag2domain_api.app.util.db._db_conn.close() self.assertRaises( RuntimeError, execute_db, "SELECT * FROM some_phantasy_table;" ) disconnect_db()
def test_psycopg2_args_string(self): config, intxn_table_map = parse_test_db_config() args_dict = Psycopg2Adapter.to_psycopg_args(config) args_list = [ "%s=%s" % (key, val) for key, val in args_dict.items() if key != "options" ] args = ' '.join(args_list) print(args) adapter = Psycopg2Adapter(args, intxn_table_map) adapter.db_connection.cursor().execute("SELECT 1")
def test_psycopg2_args_autocommmit_conn_fail(self): config, intxn_table_map = parse_test_db_config() connection_args = Psycopg2Adapter.to_psycopg_args(config) conn = psycopg2.connect(**connection_args) conn.set_session(autocommit=True) self.assertRaisesRegex( ValueError, "DB connection used must not be in in autocommit mode", Psycopg2Adapter, conn, intxn_table_map )
def test_connect_db_fails_when_set_db_used(self): config, _ = parse_test_db_config() old_db_conn = connect_db(config) connection_args = Psycopg2Adapter.to_psycopg_args(config) conn = psycopg2.connect(**connection_args) set_db(conn) old_db_conn.close() self.assertRaisesRegex( ValueError, "No config given", connect_db )
def test_reconnect_db(self): config, _ = parse_test_db_config() old_db_conn = connect_db(config) self.assertIsInstance(old_db_conn, psycopg2.extensions.connection) db_conn = connect_db(config) self.assertIsInstance(db_conn, psycopg2.extensions.connection) self.assertRaisesRegex( psycopg2.Error, "connection already closed", old_db_conn.cursor ) rows = execute_db("SELECT 1;") assert len(rows) == 1 assert rows[0][0] == 1 disconnect_db()
from fastapi.testclient import TestClient import pprint import psycopg2.extras import datetime from .db_test_classes import APIWriteTest from tests.util import parse_test_db_config from tag2domain_api.app.main import app pprinter = pprint.PrettyPrinter(indent=4) client = TestClient(app) _, INTXN_TABLE_MAPPINGS = parse_test_db_config() class MSM2TagEndpointMSM2TAGTest(APIWriteTest): def test_post_measurement(self): msm = { "version": "1", "tag_type": "intersection", "tagged_id": 3, "taxonomy": "tax_test1", "producer": "test", "measured_at":
def test_connect_db(self): config, _ = parse_test_db_config() db_conn = connect_db(config) self.assertIsInstance(db_conn, psycopg2.extensions.connection) disconnect_db() assert tag2domain_api.app.util.db._db_conn is None