import os from random import randint from time import sleep import pytest from db_schema.schemas import apps from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @pytest.fixture(scope='session') def db(): test_engine = create_engine(os.getenv('DB_URL')) TestSession = sessionmaker(bind=test_engine) # pylint: disable=invalid-name test_session = TestSession(autocommit=True, autoflush=True) # Simple hack for waiting migrations to be completed while True: result = test_session.execute( # pylint: disable=no-member 'SELECT id FROM databasechangelog ORDER BY dateexecuted DESC LIMIT 1' ) if result.first(): break sleep(1) yield test_session @pytest.fixture(scope='function') def clean_db(db): # pylint: disable=redefined-outer-name yield for model in [apps.DatabricksExecution, apps.ContentStatus, apps.UnitOfWork]: db.query(model).delete() def apps_unit_of_work_stub(**kwargs): num = randint(0, 100) data = { 'unit_of_work_code': f'apple-20140201-test_{num}', 'report_date': '2014-02-01', 'report_id': 5, 'activity_status': apps.ActivityStatusEnum.NOT_IN_PROGRESS, 'completeness_status': apps.CompletenessStatusEnum.COMPLETE, 'next_run_at': '2020-08-20 14:44:48.114833', 'created_at': '2020-07-15 08:17:59.330130', 'last_updated_at': '2020-08-20 14:44:48.114836', 'failure_count': 0, 'latest_job_id': 'spotify-20140201-streams_day_1130_20200820T14.44.48', 'latest_content_status_timestamp': '2020-07-15 08:17:59.330130', 'latest_job_state': None, 'priority': 9, 'unit_of_work_type': 'DAILY', 'data_source': 'SLZ', } data.update(**kwargs) return apps.UnitOfWork(**data) def dbx_execution_stub(**kwargs): data = { 'dbx_job_id': 164, 'dbx_run_id': 7608, 'contexts': '[]', 'status': apps.DBXJobStatusEnum.COMPLETE, 'started_at': '2020-03-18 12:12:06.840446', 'completed_at': '2020-03-18 12:14:34.423176', 'dbx_job_expired_at': '2020-03-18 12:14:34.423176', 'sf_execution_name': 'amazonadsupported-20200309-stream_track2_2020-03-19T04.29.10' } data.update(**kwargs) return apps.DatabricksExecution(**data)