"""Shared fixtures for tests.""" import os import pytest from abacus_common_logic.connectors.database import db from abacus_schedule.api import create_app from abacus_schedule.config import Config class TestConfig(Config): """Test configuration.""" MYSQL_DB_NAME = os.environ.get('MYSQL_TEST_DB_NAME', Config.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 = ('schedule', 'schedule_attachment') db.engine.execute('SET FOREIGN_KEY_CHECKS=0;') for table in top_level_tables: db.engine.execute(f'TRUNCATE TABLE {table}') db.engine.execute('SET FOREIGN_KEY_CHECKS=1;') @pytest.fixture def mock_schedule_fixture(): """Mock schedule data.""" return { 'schedule_id': 1234, 'conditions': None, 'target_id': '126', 'schedule_name': 'random name 1', 'target_type': 'contributor', }