from unittest import mock import pytest from dmp.adapters.google.models import User from dmp.audiences.enums import AudienceSharePlatform from dmp.audiences.models import Audience, AudienceShare from dmp.google.exceptions import ( GoogleAudienceNotFoundError, GoogleUserConnectionNotFoundError, ) from dmp.google.handlers import ( GetGoogleAudienceShareHandler, GetGoogleAudienceShareRequest, ) from dmp.google.models import ( GoogleAdAccount, GoogleAudience, GoogleUserAdAccount, GoogleUserConnection, GoogleUserConnectionAdAccount, ) from tests.unit.faker import FakerTyped from tests.unit.types import BuildModel, CreateModel class TestGetGoogleAudienceShareHandler: @pytest.mark.db def test_get_audience_share( self, handler: GetGoogleAudienceShareHandler, google_client_mock: mock.MagicMock, build_model: BuildModel, create_model: CreateModel, ) -> None: audience = create_model(Audience) ad_account = create_model(GoogleAdAccount) user_connection = create_model( GoogleUserConnection, is_valid=True, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account), ], ) create_model( GoogleUserAdAccount, ad_account_id=ad_account.id, identity_id=user_connection.identity_id, vendor_id=audience.vendor_id, subaccount_id=audience.subaccount_id, ) share = create_model( AudienceShare, audience=audience, platform=AudienceSharePlatform.GOOGLE, created_by=user_connection.identity_id, ) google_audience = create_model( GoogleAudience, share=share, ad_account=ad_account ) google_client_mock.get_user.return_value = build_model(User) response = handler.handle( GetGoogleAudienceShareRequest(share_id=google_audience.share_id), ) assert response.audience_id == google_audience.id assert response.audience_name == google_audience.name assert response.fans_count == google_audience.fans_count assert response.ad_account_external_id == google_audience.ad_account_external_id @pytest.mark.db def test_get_audience_share_not_exist( self, handler: GetGoogleAudienceShareHandler, fake: FakerTyped ) -> None: with pytest.raises(GoogleAudienceNotFoundError): handler.handle( GetGoogleAudienceShareRequest(share_id=fake.uuid4_string()), ) @pytest.mark.db def test_get_audience_share_user_connection_not_exist( self, handler: GetGoogleAudienceShareHandler, create_model: CreateModel ) -> None: google_audience = create_model(GoogleAudience) with pytest.raises(GoogleUserConnectionNotFoundError): handler.handle( GetGoogleAudienceShareRequest(share_id=google_audience.share_id), )