from unittest import mock import pytest from campaigns.campaigns.enums import CampaignStatus from campaigns.campaigns.handlers import SearchCampaignsHandler, SearchCampaignsRequest from campaigns.connectors.db import Database from campaigns.meta.enums import CampaignObjective, CampaignPlatform from campaigns.meta.models import Campaign from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel @pytest.fixture def handler(db: Database, auth_service_mock: mock.MagicMock) -> SearchCampaignsHandler: return SearchCampaignsHandler(db=db, auth_service=auth_service_mock) async def test_search_campaigns_empty( handler: SearchCampaignsHandler, identity_id: str, profile_id: int ) -> None: result = await handler.handle( SearchCampaignsRequest(identity_id=identity_id, profile_id=profile_id) ) assert result.total == 0 assert result.items == [] async def test_search_campaigns_without_filters( handler: SearchCampaignsHandler, create_model: CreateModel, identity_id: str, profile_id: int, ) -> None: await create_model(Campaign) await create_model(Campaign) await create_model(Campaign) result = await handler.handle( SearchCampaignsRequest(identity_id=identity_id, profile_id=profile_id) ) assert result.total == 3 assert len(result.items) == result.total async def test_search_campaigns_filter_statuses( handler: SearchCampaignsHandler, create_model: CreateModel, identity_id: str, profile_id: int, ) -> None: await create_model(Campaign, status=CampaignStatus.DRAFT) await create_model(Campaign, status=CampaignStatus.SUBMITTED) await create_model(Campaign, status=CampaignStatus.DRAFT) result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, statuses=[CampaignStatus.SUBMITTED], ) ) assert result.total == 1 assert len(result.items) == result.total async def test_search_campaigns_filter_vendor_id( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: vendor_id = fake.integer() await create_model(Campaign, vendor_id=vendor_id) await create_model(Campaign, vendor_id=vendor_id) await create_model(Campaign, vendor_id=vendor_id) result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, vendor_id=vendor_id ) ) assert result.total == 3 assert len(result.items) == result.total async def test_search_campaigns_filter_subaccount_id( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: subaccount_id_1 = 1000 subaccount_id_2 = 2000 subaccount_id_3 = 3000 await create_model(Campaign, subaccount_id=subaccount_id_1) await create_model(Campaign, subaccount_id=subaccount_id_2) await create_model(Campaign, subaccount_id=subaccount_id_3) await create_model(Campaign, subaccount_id=subaccount_id_3) result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, subaccount_id=subaccount_id_3, ) ) assert result.total == 2 assert len(result.items) == result.total async def test_search_campaigns_filter_platforms( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: await create_model( Campaign, platforms=[CampaignPlatform.FACEBOOK], ) await create_model( Campaign, platforms=[CampaignPlatform.FACEBOOK, CampaignPlatform.INSTAGRAM], ) await create_model( Campaign, platforms=[CampaignPlatform.INSTAGRAM], ) result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, platforms=[CampaignPlatform.FACEBOOK], ) ) assert result.total == 2 assert len(result.items) == result.total async def test_search_campaigns_filter_objectives( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: await create_model(Campaign, objective=CampaignObjective.AWARENESS) await create_model(Campaign, objective=CampaignObjective.ENGAGEMENT) await create_model(Campaign, objective=CampaignObjective.TRAFFIC) result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, objectives=[CampaignObjective.AWARENESS, CampaignObjective.TRAFFIC], ) ) assert result.total == 2 assert len(result.items) == result.total async def test_search_campaigns_filter_global_participant_ids( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: global_participant_id_1 = fake.uuid4_string() global_participant_id_2 = fake.uuid4_string() global_participant_id_3 = fake.uuid4_string() await create_model(Campaign, global_participant_id=global_participant_id_1) await create_model(Campaign, global_participant_id=global_participant_id_2) await create_model(Campaign, global_participant_id=global_participant_id_3) await create_model(Campaign, global_participant_id=global_participant_id_2) await create_model(Campaign, global_participant_id=global_participant_id_3) result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, global_participant_ids=[global_participant_id_1, global_participant_id_3], ) ) assert result.total == 3 assert len(result.items) == result.total async def test_search_campaigns_filter_label_participants( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: vendor_id_1 = 100 vendor_id_2 = 200 subaccount_id_1 = 1000 subaccount_id_2 = 2000 subaccount_id_3 = 3000 subaccount_id_4 = 4000 subaccount_id_5 = 5000 global_participant_id_1 = fake.uuid4_string() global_participant_id_2 = fake.uuid4_string() await create_model(Campaign, vendor_id=vendor_id_1, subaccount_id=subaccount_id_1) await create_model(Campaign, vendor_id=vendor_id_2, subaccount_id=subaccount_id_2) await create_model( Campaign, vendor_id=vendor_id_1, subaccount_id=subaccount_id_3, global_participant_id=global_participant_id_1, ) await create_model(Campaign, vendor_id=vendor_id_1, subaccount_id=subaccount_id_4) await create_model(Campaign, vendor_id=vendor_id_2, subaccount_id=subaccount_id_5) await create_model( Campaign, vendor_id=vendor_id_2, subaccount_id=subaccount_id_4, global_participant_id=global_participant_id_2, ) await create_model( Campaign, vendor_id=vendor_id_1, global_participant_id=global_participant_id_1, ) result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, vendor_id=vendor_id_1, subaccount_id=subaccount_id_4, global_participant_ids=[global_participant_id_1, global_participant_id_2], ) ) assert result.total == 3 assert len(result.items) == result.total async def test_search_campaigns_search_term( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: await create_model(Campaign, name="test campaign") await create_model(Campaign, name="This is TEST") await create_model(Campaign, name="other") result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, search="test" ) ) assert result.total == 2 assert len(result.items) == result.total async def test_search_campaigns_search_term_malformed( handler: SearchCampaignsHandler, create_model: CreateModel, fake: FakerTyped, identity_id: str, profile_id: int, ) -> None: await create_model(Campaign, name="test campaign") result = await handler.handle( SearchCampaignsRequest( identity_id=identity_id, profile_id=profile_id, search="test'" ) ) assert result.total == 0 assert not result.items