from unittest import mock import pytest from app.handlers import EventHandler from app.models import CustomAudience, Event @pytest.fixture def handler( kms_client_mock: mock.MagicMock, db_mock: mock.MagicMock, repository_mock: mock.MagicMock, api_client_mock: mock.MagicMock, ) -> EventHandler: return EventHandler( kms_client=kms_client_mock, db=db_mock, ad_account_repository=repository_mock, facebook_client=api_client_mock, tiktok_client=api_client_mock, ) def test_meta_audiences_cleanup( handler: EventHandler, api_client_mock: mock.MagicMock, kms_decrypt_value: str, ad_account: str, audience_ttl_days: int, test_audience: CustomAudience, ) -> None: event_dict = { "platform": "META", "ad_account": ad_account, "audience_ttl_days": audience_ttl_days, } event = Event.model_validate(event_dict) handler.handle(event) api_client_mock.get_audiences.assert_called_with(ad_account, kms_decrypt_value) api_client_mock.delete_audiences.assert_called_with( ad_account, [test_audience], kms_decrypt_value ) def test_tiktok_audiences_cleanup( handler: EventHandler, api_client_mock: mock.MagicMock, kms_decrypt_value: str, ad_account: str, audience_ttl_days: int, test_audience: CustomAudience, ) -> None: event_dict = { "platform": "TIKTOK", "ad_account": ad_account, "audience_ttl_days": audience_ttl_days, } event = Event.model_validate(event_dict) handler.handle(event) api_client_mock.get_audiences.assert_called_with(ad_account, kms_decrypt_value) api_client_mock.delete_audiences.assert_called_with( ad_account, [test_audience], kms_decrypt_value )