from datetime import UTC, datetime, timedelta import pytest from app.connectors.database.models import ( AdReportingAdDbt, AdReportingCreativeThumbnail, GoogleAdReportingConnection, ) from app.connectors.database.repository import AdReportingRepository from app.enums import AdReportingPlatform, AppConnectionStatus from tests.unit.types import CreateReportingModel @pytest.mark.db def test_get_creatives_google_new( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 1 creative = creatives[0] assert creative.creative_id == ad.creative_id assert creative.platform == AdReportingPlatform.GOOGLE assert creative.source_url == ad.creative_image_url assert creative.fails_count == 0 @pytest.mark.db def test_get_creatives_google_connection_error( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.ERROR, ) create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 0 @pytest.mark.db def test_get_creatives_google_duplicate_creative_id( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, synced_at=datetime.now(UTC), ) create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, creative_id=ad.creative_id, synced_at=ad.synced_at - timedelta(seconds=1), ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 1 creative = creatives[0] assert creative.creative_id == ad.creative_id assert creative.platform == AdReportingPlatform.GOOGLE assert creative.source_url == ad.creative_image_url assert creative.fails_count == 0 @pytest.mark.db def test_get_creatives_google_failed_thumbnail( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, ) create_reporting_model( AdReportingCreativeThumbnail, platform=ad.platform, creative_id=ad.creative_id, img_s3_obj_key=None, fails_count=1, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 1 creative = creatives[0] assert creative.creative_id == ad.creative_id assert creative.platform == AdReportingPlatform.GOOGLE assert creative.source_url == ad.creative_image_url assert creative.fails_count == 1 @pytest.mark.db def test_get_creatives_google_failed_thumbnail_limit( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, ) create_reporting_model( AdReportingCreativeThumbnail, platform=ad.platform, creative_id=ad.creative_id, source_url=ad.creative_image_url, img_s3_obj_key=None, fails_count=50, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 0 @pytest.mark.db def test_get_creatives_google_thumbnail_url_null( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, creative_image_url=None, creative_video_url=None, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 0 @pytest.mark.db def test_get_creatives_google_creative_image_url_null( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, creative_image_url=None, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 1 creative = creatives[0] assert creative.creative_id == ad.creative_id assert creative.platform == AdReportingPlatform.GOOGLE assert creative.source_url == ad.creative_video_url assert creative.fails_count == 0 @pytest.mark.db def test_get_creatives_google_finished( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) ad = create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.GOOGLE, source_schema=connection.fivetran_schema, ) create_reporting_model( AdReportingCreativeThumbnail, platform=ad.platform, creative_id=ad.creative_id, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 0 @pytest.mark.db def test_get_creatives_google_duplicate_connection( ad_reporting_repository: AdReportingRepository, create_reporting_model: CreateReportingModel, ) -> None: connection = create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.CONNECTED, ) create_reporting_model( GoogleAdReportingConnection, status=AppConnectionStatus.PENDING, fivetran_schema=connection.fivetran_schema, _fivetran_deleted=True, ) create_reporting_model( AdReportingAdDbt, platform=AdReportingPlatform.META, source_schema=connection.fivetran_schema, ) creatives = ad_reporting_repository.get_creatives() assert len(creatives) == 1