import pytest import asyncio from unittest.mock import patch from sqlalchemy import insert, schema, text import config from tests.constants import ( APPLICATION_RESPONSE_DATA, TEST_HEADERS_FOR_ATLAS, TEST_X_USER_ID, REMOVE_ALL_FROM_DNA_USER_SEARCH_TABLE, REMOVE_ALL_FROM_DNA_CATEGORY_TABLE, REMOVE_ALL_FROM_DNA_CATEGORY_ENTITY_TABLE, REMOVE_ALL_FROM_DNA_LOGGING_RECORDS_TABLE, REMOVE_ALL_FROM_DNA_RECENT_SEARCHES_TABLE, ) from server.core.helpers import authorization from server.db.pg_connector import get_postgres_async_session, PostgresConnector with patch("server.db.es_connector.backoff.on_exception", lambda f: f): from server.db.es_connector import ES_INSTANCE @pytest.fixture(scope="session") def event_loop(): """Create an instance of the default event loop for each test case. Cleans up everything from table after each test case """ loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close() @pytest.fixture(scope="function") async def app_client(aiohttp_client): with patch.object( authorization, "make_request", side_effect=[TEST_HEADERS_FOR_ATLAS, APPLICATION_RESPONSE_DATA] ) as make_request_mock: # mock ApolloClient._get_application method from app import create_app app = create_app() yield await aiohttp_client(app) @pytest.fixture(scope="function") async def postgres_client(): yield DB_CONNECTION_PARAMS = { "user": config.POSTGRES_USER, "password": config.POSTGRES_PASSWORD, "host": config.POSTGRES_HOST, "database": config.POSTGRES_DB, "port": config.POSTGRES_PORT, } postgres_async_session = get_postgres_async_session(**DB_CONNECTION_PARAMS) await postgres_async_session.execute(text(REMOVE_ALL_FROM_DNA_USER_SEARCH_TABLE)) await postgres_async_session.execute(text(REMOVE_ALL_FROM_DNA_CATEGORY_ENTITY_TABLE)) await postgres_async_session.execute(text(REMOVE_ALL_FROM_DNA_CATEGORY_TABLE)) await postgres_async_session.execute(text(REMOVE_ALL_FROM_DNA_LOGGING_RECORDS_TABLE)) await postgres_async_session.execute(text(REMOVE_ALL_FROM_DNA_RECENT_SEARCHES_TABLE)) await postgres_async_session.commit() @pytest.fixture(scope="function") async def aggregates_postgres_client(request): # multiple tables if all(isinstance(el, tuple) for el in request.param): table_data_pairs = request.param # only one table else: table_data_pairs = (request.param,) DB_CONNECTION_PARAMS = { "user": config.POSTGRES_DATA_USER, "password": config.POSTGRES_DATA_PASSWORD, "host": config.POSTGRES_DATA_HOST, "database": config.POSTGRES_DATA_DB, "port": config.POSTGRES_DATA_PORT, } engine = PostgresConnector(**DB_CONNECTION_PARAMS)._create_engine(flag="sync") connection = engine.connect() connection.execute( schema.CreateSchema( config.POSTGRES_DATA_SCHEMA, ) ) for table, data in table_data_pairs: table.__table__.create(engine) if data: connection.execute(insert(table), data) yield connection for table, _ in table_data_pairs: table.__table__.drop(engine) connection.execute( schema.DropSchema( config.POSTGRES_DATA_SCHEMA, ) ) @pytest.fixture(scope="function") async def elastic_client(request): for tpl in request.param: index, data_to_insert = tpl await ES_INSTANCE.index(index=index, data=data_to_insert, force=True) await ES_INSTANCE.refresh(index=index) yield ES_INSTANCE for tpl in request.param: index, _ = tpl await ES_INSTANCE.index_delete(index) @pytest.fixture def auth_header(): return {"Authorization": config.DNA_API_APPKEY} @pytest.fixture def auth_header_authorized(): return {"Authorization": config.DNA_API_APPKEY, "X-User-Id": TEST_X_USER_ID}