"""Shared fixtures for tests.""" import os import pytest from abacus_common_logic.connectors.database import db from abacus_legacy_sync.api import create_app from abacus_legacy_sync.config import Config from tests.utils.sql_templates import ( CREATE_COUNTRY_TABLE, CREATE_CURRENCIES_TABLE, CREATE_CUSTOMER_MASTER_MASTER, CREATE_DISTRIBUTION_TYPE_TABLE, CREATE_VENDOR_CONTRACT_DISTRIBUTION_TYPE_TABLE, CREATE_VENDOR_CONTRACT_TABLE, CREATE_VENDOR_DMS_MASTER_RESTRICTION, ) class TestConfig(Config): """Test configuration.""" AR_MYSQL_DB_NAME = os.environ.get( 'AR_MYSQL_TEST_DB_NAME', Config.AR_MYSQL_DB_NAME + '_test' ) @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 @pytest.fixture(autouse=True) def test_app_request(test_app_in_context): """Push the test app onto the context and trigger preprocessing.""" with test_app_in_context.test_request_context(): test_app_in_context.preprocess_request() yield test_app_in_context @pytest.fixture def fixture_client(test_app_in_context): """Create a client fixture.""" with test_app_in_context.test_client() as test_client: yield test_client @pytest.fixture def fresh_db(): """Refresh the test database.""" top_level_tables = ( 'country', 'currencies', 'distribution_type', 'vendor_contract_distribution_type', 'vendor_contract', 'customer_master_master', 'vendor_dms_master_restriction', ) con = db.engine.connect() con.execute('SET FOREIGN_KEY_CHECKS = 0;') con.execute(CREATE_COUNTRY_TABLE) con.execute(CREATE_CURRENCIES_TABLE) con.execute(CREATE_DISTRIBUTION_TYPE_TABLE) con.execute(CREATE_VENDOR_CONTRACT_TABLE) con.execute(CREATE_VENDOR_CONTRACT_DISTRIBUTION_TYPE_TABLE) con.execute(CREATE_CUSTOMER_MASTER_MASTER) con.execute(CREATE_VENDOR_DMS_MASTER_RESTRICTION) trans = con.begin() for table in top_level_tables: con.execute(f'TRUNCATE TABLE `{table}`') trans.commit() con.execute('SET FOREIGN_KEY_CHECKS = 1;')