import pytest from fansifter_common.auth.account import Account from dmp.ad_reporting.dtos import AdReportingCampaignObjective from dmp.ad_reporting.enums import AdReportingObjective, AdReportingPlatform from dmp.ad_reporting.handlers import ( GetAdReportingCampaignsObjectivesHandler, GetAdReportingCampaignsObjectivesRequest, ) from dmp.ad_reporting.models import AdReportingCampaignDbt, AdReportingReport from dmp.meta.models import MetaAdAccount, MetaUserAdAccount from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel, CreateReportingModel class TestGetAdReportingCampaignsObjectivesHandler: @pytest.mark.db def test_get_allowed_campaigns_objectives( self, handler: GetAdReportingCampaignsObjectivesHandler, create_model: CreateModel, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: ad_account_1 = create_model(MetaAdAccount) ad_account_2 = create_model(MetaAdAccount) ad_account_3 = create_model(MetaAdAccount) create_model( MetaUserAdAccount, ad_account_id=ad_account_1.id, identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_model( MetaUserAdAccount, ad_account_id=ad_account_2.id, identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_model( MetaUserAdAccount, ad_account_id=ad_account_3.id, identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model( AdReportingCampaignDbt, account_id=ad_account_1.external_id, platform=AdReportingPlatform.META, objective=AdReportingObjective.APP_PROMOTION, ) create_reporting_model( AdReportingCampaignDbt, account_id=ad_account_2.external_id, platform=AdReportingPlatform.META, objective=AdReportingObjective.ENGAGEMENT, ) create_reporting_model( AdReportingCampaignDbt, account_id=ad_account_3.external_id, platform=AdReportingPlatform.META, objective=AdReportingObjective.OUTCOME_APP_PROMOTION, ) result = handler.handle( GetAdReportingCampaignsObjectivesRequest( identity_id=identity_id, ) ) assert result == [ AdReportingCampaignObjective( platform=AdReportingPlatform.META, objective=AdReportingObjective.APP_PROMOTION, ), AdReportingCampaignObjective( platform=AdReportingPlatform.META, objective=AdReportingObjective.ENGAGEMENT, ), AdReportingCampaignObjective( platform=AdReportingPlatform.META, objective=AdReportingObjective.OUTCOME_APP_PROMOTION, ), ] @pytest.mark.db def test_get_allowed_campaigns_objectives_no_allowed_accounts( self, handler: GetAdReportingCampaignsObjectivesHandler, create_reporting_model: CreateReportingModel, identity_id: str, ) -> None: create_reporting_model( AdReportingCampaignDbt, platform=AdReportingPlatform.META, objective=AdReportingObjective.OUTCOME_APP_PROMOTION, ) result = handler.handle( GetAdReportingCampaignsObjectivesRequest( identity_id=identity_id, ) ) assert result == [] @pytest.mark.db def test_get_allowed_campaigns_objectives_empty( self, handler: GetAdReportingCampaignsObjectivesHandler, identity_id: str, ) -> None: result = handler.handle( GetAdReportingCampaignsObjectivesRequest( identity_id=identity_id, ) ) assert result == [] @pytest.mark.db def test_get_campaigns_objectives_filter_by_report_id( self, handler: GetAdReportingCampaignsObjectivesHandler, create_model: CreateModel, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: ad_account = create_model(MetaAdAccount) create_model( MetaUserAdAccount, ad_account_id=ad_account.id, identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) campaign_1 = create_reporting_model( AdReportingCampaignDbt, account_id=ad_account.external_id, platform=AdReportingPlatform.META, objective=AdReportingObjective.APP_PROMOTION, ) create_reporting_model( AdReportingCampaignDbt, account_id=ad_account.external_id, platform=AdReportingPlatform.META, objective=AdReportingObjective.ENGAGEMENT, ) report = create_reporting_model( AdReportingReport, identity_id=identity_id, campaign_ids={campaign_1.id} ) result = handler.handle( GetAdReportingCampaignsObjectivesRequest( identity_id=identity_id, report_id=report.id ) ) assert len(result) == 1 assert result[0].platform == AdReportingPlatform.META assert result[0].objective == AdReportingObjective.APP_PROMOTION @pytest.mark.db def test_random_objective_converted_to_other( self, handler: GetAdReportingCampaignsObjectivesHandler, create_model: CreateModel, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, fake: FakerTyped, ) -> None: ad_account_1 = create_model(MetaAdAccount) create_model( MetaUserAdAccount, ad_account_id=ad_account_1.id, identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model( AdReportingCampaignDbt, account_id=ad_account_1.external_id, platform=AdReportingPlatform.META, objective=fake.pystr(), ) result = handler.handle( GetAdReportingCampaignsObjectivesRequest( identity_id=identity_id, ) ) assert result == [ AdReportingCampaignObjective( platform=AdReportingPlatform.META, objective=AdReportingObjective.OTHER, ), ]