# pylint: disable=unused-argument from datetime import datetime, timedelta, timezone from unittest import mock import pytest from db_schema.schemas import apps from data_completeness_updater.services import AppsEtlPgService from tests.conftest import apps_unit_of_work_stub, dbx_execution_stub FREEZED_DT = datetime(2020, 10, 15, 12, 23, 45, tzinfo=timezone.utc) @pytest.mark.integration @pytest.mark.parametrize( 'last_insertion_date, len_expected', [ (FREEZED_DT - timedelta(minutes=5), 1), (FREEZED_DT - timedelta(minutes=7), 2), (None, 2), ] ) @pytest.mark.freeze_time(FREEZED_DT) def test_get_apps_etl_uows__by_completed_dbx_execution( last_insertion_date, len_expected, db, clean_db ): uow1 = apps_unit_of_work_stub(report_id=6) uow2 = apps_unit_of_work_stub(report_id=6) dbx_ex_1 = dbx_execution_stub(unit_of_work=uow1, completed_at=FREEZED_DT - timedelta(minutes=3)) dbx_ex_2 = dbx_execution_stub(unit_of_work=uow2, completed_at=FREEZED_DT - timedelta(minutes=6)) dbx_ex_3 = dbx_execution_stub( unit_of_work=uow2, completed_at=FREEZED_DT - timedelta(minutes=3), status=apps.DBXJobStatusEnum.IN_PROGRESS ) db.add_all([uow1, uow2, dbx_ex_1, dbx_ex_2, dbx_ex_3]) db.flush() service = AppsEtlPgService(mock.Mock(), db) result = service.get_apps_etl_uows(last_insertion_date) assert len(result) == len_expected @pytest.mark.integration @pytest.mark.parametrize( 'last_insertion_date, len_expected', [ (FREEZED_DT - timedelta(minutes=5), 1), (FREEZED_DT - timedelta(minutes=7), 2), (None, 2), ] ) @pytest.mark.freeze_time(FREEZED_DT) def test_get_apps_etl_uows__by_completed_uow(last_insertion_date, len_expected, db, clean_db): uow1 = apps_unit_of_work_stub( report_id=6, last_updated_at=FREEZED_DT - timedelta(minutes=3), ) uow2 = apps_unit_of_work_stub( report_id=6, last_updated_at=FREEZED_DT - timedelta(minutes=6), ) uow3 = apps_unit_of_work_stub() dbx_ex_1 = dbx_execution_stub(unit_of_work=uow1) dbx_ex_2 = dbx_execution_stub(unit_of_work=uow2) db.add_all([uow1, uow2, uow3, dbx_ex_1, dbx_ex_2]) db.flush() service = AppsEtlPgService(mock.Mock(), db) result = service.get_apps_etl_uows(last_insertion_date) assert len(result) == len_expected @pytest.mark.integration @pytest.mark.freeze_time(FREEZED_DT) def test_get_apps_etl__amazon_uows(db, clean_db): uow1 = apps_unit_of_work_stub(report_id=6) # wrong report uow2 = apps_unit_of_work_stub(report_id=22) uow3 = apps_unit_of_work_stub(report_id=22) uow4 = apps_unit_of_work_stub(report_id=22, report_date='2020-02-01') # wrong date uow5 = apps_unit_of_work_stub(report_id=22) # no dbx run uow6 = apps_unit_of_work_stub(report_id=22) # only one dbx_run in progress for uow in [uow1, uow2, uow3, uow4]: dbx_ex = dbx_execution_stub(unit_of_work=uow) db.add(dbx_ex) dbx_ex_6 = dbx_execution_stub(unit_of_work=uow6, status=apps.DBXJobStatusEnum.IN_PROGRESS) db.add_all([uow1, uow2, uow3, uow4, uow5, uow6, dbx_ex_6]) db.flush() service = AppsEtlPgService(mock.Mock(), db) result = service.get_apps_etl_amazon_uows(report_dates=['2014-02-01']) assert len(result) == 2