from unittest import mock import pytest from fansifter_common.auth.account import Account from dmp.ad_reporting.dtos import AdReportingAd from dmp.ad_reporting.enums import AdReportingCreativeType, AdReportingPlatform from dmp.ad_reporting.exceptions import AdReportingAdNotFoundError from dmp.ad_reporting.handlers import GetAdReportingAdHandler, GetAdReportingAdRequest from dmp.ad_reporting.models import AdReportingAdDbt, AdReportingCreativeThumbnail from dmp.meta.models import MetaAdAccount, MetaUserAdAccount from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel, CreateReportingModel class TestGetAdReportingAdHandler: @pytest.mark.db def test_get_ad( self, handler: GetAdReportingAdHandler, 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, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.META, account_id=ad_account.external_id, ) result = handler.handle( GetAdReportingAdRequest( identity_id=identity_id, ad_id=ad.id, ) ) assert result.id == ad.id @pytest.mark.db def test_get_ad_no_allowed_accounts( self, handler: GetAdReportingAdHandler, create_reporting_model: CreateReportingModel, identity_id: str, ) -> None: ad = create_reporting_model(AdReportingAdDbt, platform=AdReportingPlatform.META) with pytest.raises(AdReportingAdNotFoundError): handler.handle( GetAdReportingAdRequest( identity_id=identity_id, ad_id=ad.id, ) ) @pytest.mark.db def test_get_ad_not_found( self, handler: GetAdReportingAdHandler, identity_id: str, fake: FakerTyped ) -> None: with pytest.raises(AdReportingAdNotFoundError): handler.handle( GetAdReportingAdRequest( identity_id=identity_id, ad_id=fake.uuid4_string(), ) ) @pytest.mark.parametrize( "platform, image_url, video_url, post_url, thumbnail_url, outcome", [ # creative_image_url ( AdReportingPlatform.META, "image_url", None, None, None, AdReportingCreativeType.IMAGE, ), ( AdReportingPlatform.GOOGLE, "image_url", None, None, "thumbnail_url", AdReportingCreativeType.IMAGE, ), ( AdReportingPlatform.GOOGLE, "image_url", None, None, None, AdReportingCreativeType.LINK, ), ( AdReportingPlatform.TIKTOK, "image_url", None, None, None, AdReportingCreativeType.IMAGE, ), # creative_video_url ( AdReportingPlatform.META, None, "video_url", None, None, AdReportingCreativeType.VIDEO, ), ( AdReportingPlatform.TIKTOK, None, "video_url", None, None, AdReportingCreativeType.VIDEO, ), ( AdReportingPlatform.GOOGLE, None, "https://www.youtube.com/shorts/-1231234123", None, "thumbnail_url", AdReportingCreativeType.YOUTUBE_VIDEO, ), ( AdReportingPlatform.GOOGLE, None, "https://www.YOUTUBE.com/watch/-sdasdsdasd", None, "thumbnail_url", AdReportingCreativeType.YOUTUBE_VIDEO, ), ( AdReportingPlatform.GOOGLE, None, "https://www.youtube.com/watch/-sdasdsdasd", None, None, AdReportingCreativeType.LINK, ), # creative_post_url ( AdReportingPlatform.META, None, None, "post_url", None, AdReportingCreativeType.INSTAGRAM_POST, ), ( AdReportingPlatform.TIKTOK, None, None, "post_url", None, AdReportingCreativeType.TIKTOK_POST, ), # creative_thumbnail ( AdReportingPlatform.TIKTOK, None, None, None, "thumbnail_url", AdReportingCreativeType.IMAGE_THUMBNAIL, ), ( AdReportingPlatform.META, None, None, None, "thumbnail_url", AdReportingCreativeType.IMAGE_THUMBNAIL, ), ( AdReportingPlatform.GOOGLE, None, None, None, "thumbnail_url", AdReportingCreativeType.IMAGE_THUMBNAIL, ), ], ) @pytest.mark.db def test_get_ad_creative_type( self, platform: AdReportingPlatform, image_url: str | None, video_url: str | None, post_url: str | None, thumbnail_url: str | None, outcome: AdReportingCreativeType, handler: GetAdReportingAdHandler, 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, ) ad = create_reporting_model( AdReportingAdDbt, platform=platform, account_id=ad_account.external_id, creative_image_url=image_url, creative_video_url=video_url, creative_post_url=post_url, ) create_reporting_model( AdReportingCreativeThumbnail, platform=platform, creative_id=ad.creative_id, img_s3_obj_key=thumbnail_url, ) with mock.patch.object( AdReportingAd, "get_cdn_url_for_s3_path" ) as mocked_method: mocked_method.return_value = thumbnail_url result = handler.handle( GetAdReportingAdRequest( identity_id=identity_id, ad_id=ad.id, ) ) assert result.creative_type == outcome @pytest.mark.parametrize( "image_url, video_url, post_url, creative_url, outcome", [ ("image_url", "video_url", "post_url", "creative_url", "image_url"), (None, "video_url", "post_url", "creative_url", "video_url"), (None, None, "post_url", "creative_url", "post_url"), (None, None, None, "creative_url", "creative_url"), (None, None, None, None, None), ], ) @pytest.mark.db def test_get_ad_creative_url( self, image_url: str | None, video_url: str | None, post_url: str | None, creative_url: str | None, outcome: str | None, handler: GetAdReportingAdHandler, 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, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.META, account_id=ad_account.external_id, creative_image_url=image_url, creative_video_url=video_url, creative_post_url=post_url, ) with mock.patch.object( AdReportingAd, "get_cdn_url_for_s3_path" ) as mocked_method: mocked_method.return_value = creative_url result = handler.handle( GetAdReportingAdRequest( identity_id=identity_id, ad_id=ad.id, ) ) assert result.creative_url == outcome