import pytest from faker import Faker from fansifter_common.auth.account import Account from dmp.ad_reporting.enums import AdReportingPlatform from dmp.ad_reporting.exceptions import AdReportingInvalidReportIdError from dmp.ad_reporting.handlers import ( AddAdReportingCampaignToReportsHandler, AddAdReportingCampaignToReportsRequest, ) from dmp.ad_reporting.models import AdReportingCampaignDbt, AdReportingReport from dmp.meta.models import MetaAdAccount, MetaUserAdAccount from tests.unit.types import CreateModel, CreateReportingModel class TestAddAdReportingCampaignToReportsHandler: @pytest.mark.db def test_add_campaign_to_reports( self, handler: AddAdReportingCampaignToReportsHandler, 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 = create_reporting_model( AdReportingCampaignDbt, account_id=ad_account.external_id, platform=AdReportingPlatform.META, ) report_1 = create_reporting_model( AdReportingReport, identity_id=identity_id, ) report_2 = create_reporting_model( AdReportingReport, identity_id=identity_id, ) handler.handle( AddAdReportingCampaignToReportsRequest( identity_id=identity_id, campaign_id=campaign.id, report_ids={report_1.id, report_2.id}, ) ) assert report_1.campaign_ids == [campaign.id] assert report_2.campaign_ids == [campaign.id] @pytest.mark.db def test_add_campaign_to_reports_invalid_report_id( self, handler: AddAdReportingCampaignToReportsHandler, create_model: CreateModel, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, faker: Faker, ) -> 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 = create_reporting_model( AdReportingCampaignDbt, account_id=ad_account.external_id, platform=AdReportingPlatform.META, ) report_id = faker.uuid4() with pytest.raises(AdReportingInvalidReportIdError) as exc_info: handler.handle( AddAdReportingCampaignToReportsRequest( identity_id=identity_id, campaign_id=campaign.id, report_ids={report_id}, ) ) assert exc_info.value.report_id == report_id