# pylint: disable=redefined-outer-name, unused-argument from typing import Dict, List from unittest import mock import pytest from db_schema.postgres.connection import Connection from db_schema.schemas import slz from freezegun.api import FakeDatetime from smelog.factory import SmeBoundLogger from slz_spotify_charts_clean_up.services.db import DBService from tests.conftest import CommonValues, CSSet, UoWSet pytestmark = [pytest.mark.integration] @pytest.mark.parametrize( 'unit_to_test, unit_of_work__indirect', [ (UoWSet.DEFAULT, [UoWSet.DEFAULT]), (UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING]), (UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.ANOTHER_REPROCESSING]), (UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING, UoWSet.ANOTHER_REPROCESSING]), ], indirect=['unit_of_work__indirect'], ) def test_find_uow_to_cancel( db: Connection, now_frozen: FakeDatetime, unit_to_test: UoWSet, unit_of_work__indirect: Dict[UoWSet, slz.UnitOfWork], ): uows: Dict[UoWSet, slz.UnitOfWork] = unit_of_work__indirect uow_to_cancel: slz.UnitOfWork = uows[unit_to_test] other_uows: List[slz.UnitOfWork] = [uows[key] for key in uows.keys() if key != unit_to_test] db_service = DBService( logger=mock.MagicMock(spec=SmeBoundLogger), connection=db, operation_timestamp=now_frozen, ) result = db_service.get_uows_to_cancel(uow_to_cancel.unit_of_work_id) assert result == other_uows @pytest.mark.parametrize( 'unit_to_test, unit_of_work__indirect', [ (UoWSet.DEFAULT, [UoWSet.DEFAULT]), (UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING]), (UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.ANOTHER_REPROCESSING]), (UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING, UoWSet.ANOTHER_REPROCESSING]), ], indirect=['unit_of_work__indirect'], ) def test_cancel_uow_( db: Connection, now_frozen: FakeDatetime, unit_to_test: UoWSet, unit_of_work__indirect: Dict[UoWSet, slz.UnitOfWork], ): uows = unit_of_work__indirect uow_to_cancel: slz.UnitOfWork = uows[unit_to_test] other_uows: List[slz.UnitOfWork] = [uows[key] for key in uows.keys() if key != unit_to_test] assert uow_to_cancel.completeness_status != slz.CompletenessStatusEnum.CANCELLED assert all(u.completeness_status != slz.CompletenessStatusEnum.CANCELLED for u in other_uows) db_service = DBService( logger=mock.MagicMock(spec=SmeBoundLogger), connection=db, operation_timestamp=now_frozen, ) result = db_service.cancel_uow(uow_to_cancel) assert result is True assert uow_to_cancel.completeness_status == slz.CompletenessStatusEnum.CANCELLED assert all(u.completeness_status != slz.CompletenessStatusEnum.CANCELLED for u in other_uows) @pytest.mark.parametrize( 'unit_to_test, unit_of_work__indirect, content_status__indirect, expected', [ ( UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING], [ (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_1), (UoWSet.DEFAULT, CSSet.MISSING_JOB_1), (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_2), ], [CommonValues.job_1, CommonValues.job_2], ), ( UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING], [ (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_1), (UoWSet.DEFAULT, CSSet.MISSING_JOB_1), (UoWSet.REPROCESSING, CSSet.COMPLETE_JOB_2), ], [CommonValues.job_1], ), ( UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING], [ (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_1), (UoWSet.DEFAULT, CSSet.MISSING_JOB_1), (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_2), (UoWSet.REPROCESSING, CSSet.COMPLETE_JOB_3), ], [CommonValues.job_1, CommonValues.job_2], ), ], indirect=['unit_of_work__indirect', 'content_status__indirect'], ) def test_get_job_ids_of_uow( db: Connection, now_frozen: FakeDatetime, unit_to_test: UoWSet, unit_of_work__indirect: Dict[UoWSet, slz.UnitOfWork], content_status__indirect: Dict[UoWSet, Dict[CSSet, slz.ContentStatus]], expected: List[str], ): uow = unit_of_work__indirect[unit_to_test] db_service = DBService( logger=mock.MagicMock(spec=SmeBoundLogger), connection=db, operation_timestamp=now_frozen, ) result = db_service.get_job_ids_of_uow(uow.unit_of_work_id) assert result == expected @pytest.mark.parametrize( 'unit_to_test, unit_of_work__indirect, content_status__indirect, expected', [ ( UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING], [ (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_1), (UoWSet.DEFAULT, CSSet.MISSING_JOB_1), (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_2), ], 3, ), ( UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING], [ (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_1), (UoWSet.DEFAULT, CSSet.MISSING_JOB_1), (UoWSet.REPROCESSING, CSSet.COMPLETE_JOB_2), ], 2, ), ( UoWSet.DEFAULT, [UoWSet.DEFAULT, UoWSet.REPROCESSING], [ (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_1), (UoWSet.DEFAULT, CSSet.MISSING_JOB_1), (UoWSet.DEFAULT, CSSet.COMPLETE_JOB_2), (UoWSet.REPROCESSING, CSSet.COMPLETE_JOB_3), ], 3, ), ], indirect=['unit_of_work__indirect', 'content_status__indirect'], ) def test_update_content_status_of_uow( db: Connection, now_frozen: FakeDatetime, unit_to_test: UoWSet, unit_of_work__indirect: Dict[UoWSet, slz.UnitOfWork], content_status__indirect: Dict[UoWSet, Dict[CSSet, slz.ContentStatus]], expected: int, ): uow = unit_of_work__indirect[unit_to_test] number_of_cancelled = db.session.query(slz.ContentStatus) \ .filter(slz.ContentStatus.content_status == slz.ContentStatusEnum.CANCELLED) \ .count() assert number_of_cancelled == 0 db_service = DBService( logger=mock.MagicMock(spec=SmeBoundLogger), connection=db, operation_timestamp=now_frozen, ) db_service.cancel_content_status_of_uow(uow.unit_of_work_id) number_of_cancelled = db.session.query(slz.ContentStatus) \ .filter(slz.ContentStatus.content_status == slz.ContentStatusEnum.CANCELLED) \ .count() assert number_of_cancelled == expected