"""Integration test configuration.""" import os import pytest from abacus_common_logic.connectors.database import db from sqlalchemy import text from abacus_event.api import create_app from abacus_event.config import Config from tests.integration.utils.airflow_api_client import AirflowAPIClient from tests.integration.utils.ows_abacus_event_api_client import AbacusEventAPIClient QA_BASE_URL = os.environ.get('QA_BASE_URL', 'http://abacus-event-ows-abacus-event:8080') print(f'Using abacus-event URL {QA_BASE_URL}') AIRFLOW_API_BASE_URL = os.environ.get('AIRFLOW_API_BASE_URL', 'http://localhost:6303') print(f'Using airflow URL {AIRFLOW_API_BASE_URL}') @pytest.fixture def basic_headers(): """Return basic headers.""" return {'Content-Type': 'application/json'} class TestConfig(Config): """Test configuration.""" MYSQL_DB_NAME = os.environ.get('MYSQL_DB_NAME', Config.MYSQL_DB_NAME) MYSQL_DB_USER = os.environ.get('MYSQL_DB_USER', 'royalties') MYSQL_DB_HOST = os.environ.get('MYSQL_DB_HOST', 'mysql-royalties-container') MYSQL_DB_PORT = os.environ.get('MYSQL_DB_PORT', '3306') MYSQL_DB_PASS = os.environ.get('MYSQL_DB_PASS', '1234') @pytest.fixture(scope='session', autouse=True) def test_app(): """Create a test application.""" return create_app(TestConfig) @pytest.fixture(autouse=True) def test_app_in_context(test_app): """Push the test app onto the context.""" with test_app.app_context(): yield test_app def ows_abacus_event_api_client(basic_headers): """Create ows-abacus-event client.""" return AbacusEventAPIClient(QA_BASE_URL, basic_headers) @pytest.fixture def airflow_api_client(): """Create airflow client.""" return AirflowAPIClient(AIRFLOW_API_BASE_URL) @pytest.fixture def mock_commit_event(): """Mock commit event object.""" return {'target_id': '1', 'target_type': 'accounting_run', 'event_name': 'commit'} @pytest.fixture def clean_db(): """Clean tables between runs.""" tables = ['abacus_event'] with db.engine.connect() as connection: connection.execute(text('SET FOREIGN_KEY_CHECKS=0;')) for table in tables: connection.execute(text(f'TRUNCATE TABLE {table};')) connection.execute(text('SET FOREIGN_KEY_CHECKS=1;')) connection.commit() @pytest.fixture def drop_fks(): """Drop FKs.""" fk_sql = """ ALTER TABLE abacus_event DROP FOREIGN KEY fk_abacus_event_statement_period; """ with db.engine.connect() as connection: try: connection.execute(text(fk_sql)) connection.commit() except Exception as ex: print(f'FK was already deleted or error occurred: {ex}')