import pytest from dirty_equals import IsList from dmp.google.dtos import GoogleUserAdAccountLabel from dmp.google.models import ( GoogleAdAccount, GoogleAdReportingConnection, GoogleUserAdAccount, GoogleUserConnection, GoogleUserConnectionAdAccount, ) from dmp.google.repositories import GoogleAdAccountRepository from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel class TestGoogleAdAccountRepository: @pytest.mark.db def test_find_by_external_ids( self, repository: GoogleAdAccountRepository, create_model: CreateModel, ) -> None: ad_account_1 = create_model(GoogleAdAccount) ad_account_2 = create_model(GoogleAdAccount) create_model(GoogleAdAccount) result = repository.find_by_external_ids( [ad_account_1.external_id, ad_account_2.external_id] ) assert result assert len(result) == 2 @pytest.mark.db def test_find_by_identity_id_and_user_id_for_assigment( self, repository: GoogleAdAccountRepository, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_account_1 = create_model(GoogleAdAccount, name="ad_account_1") ad_account_2 = create_model(GoogleAdAccount, name="ad_account_2") ad_account_3 = create_model(GoogleAdAccount, name="ad_account_3") user_ad_account_1 = create_model( GoogleUserAdAccount, identity_id=identity_id, ad_account_id=ad_account_1.id, ) user_ad_account_2 = create_model( GoogleUserAdAccount, identity_id=identity_id, ad_account_id=ad_account_2.id, ) create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account_1), GoogleUserConnectionAdAccount(ad_account=ad_account_2), ], ) create_model( GoogleAdReportingConnection, identity_id=identity_id, user_id=user_id, ad_accounts=[ad_account_2, ad_account_3], ) ad_accounts = repository.find_by_identity_id_and_user_id_for_assigment( identity_id, user_id ) assert ad_accounts == [ GoogleUserAdAccountLabel.model_construct( id=ad_account_1.id, external_id=ad_account_1.external_id, name=ad_account_1.name or "", business_account_name=ad_account_1.business_account_name, campaigns_count=ad_account_1.campaigns_count, vendor_id=user_ad_account_1.vendor_id, subaccount_id=user_ad_account_1.subaccount_id, ), GoogleUserAdAccountLabel.model_construct( id=ad_account_2.id, external_id=ad_account_2.external_id, name=ad_account_2.name or "", business_account_name=ad_account_2.business_account_name, campaigns_count=ad_account_2.campaigns_count, vendor_id=user_ad_account_2.vendor_id, subaccount_id=user_ad_account_2.subaccount_id, ), GoogleUserAdAccountLabel.model_construct( id=ad_account_3.id, external_id=ad_account_3.external_id, name=ad_account_3.name or "", business_account_name=ad_account_3.business_account_name, campaigns_count=ad_account_3.campaigns_count, vendor_id=None, subaccount_id=None, ), ] @pytest.mark.db def test_find_by_identity_id_and_user_id_for_assigment_user_connected( self, repository: GoogleAdAccountRepository, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_account = create_model(GoogleAdAccount) create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account), ], ) ad_accounts = repository.find_by_identity_id_and_user_id_for_assigment( identity_id, user_id ) assert ad_accounts == [ GoogleUserAdAccountLabel.model_construct( id=ad_account.id, external_id=ad_account.external_id, name=ad_account.name or "", business_account_name=ad_account.business_account_name, campaigns_count=ad_account.campaigns_count, vendor_id=None, subaccount_id=None, ), ] @pytest.mark.db def test_find_by_identity_id_and_user_id_for_assigment_ad_reporting_connected( self, repository: GoogleAdAccountRepository, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_account_1 = create_model(GoogleAdAccount, name="ad_account_1") ad_account_2 = create_model(GoogleAdAccount, name="ad_account_2") create_model( GoogleAdReportingConnection, identity_id=identity_id, ad_accounts=[ad_account_1, ad_account_2], user_id=user_id, ) ad_accounts = repository.find_by_identity_id_and_user_id_for_assigment( identity_id, user_id ) assert ad_accounts == [ GoogleUserAdAccountLabel.model_construct( id=ad_account_1.id, external_id=ad_account_1.external_id, name=ad_account_1.name or "", business_account_name=ad_account_1.business_account_name, campaigns_count=ad_account_1.campaigns_count, vendor_id=None, subaccount_id=None, ), GoogleUserAdAccountLabel.model_construct( id=ad_account_2.id, external_id=ad_account_2.external_id, name=ad_account_2.name or "", business_account_name=ad_account_2.business_account_name, campaigns_count=ad_account_2.campaigns_count, vendor_id=None, subaccount_id=None, ), ] @pytest.mark.db def test_find_by_identity_id_and_user_id_for_assigment_no_name_available_yet( self, repository: GoogleAdAccountRepository, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_account_1 = create_model(GoogleAdAccount, name=None) ad_account_2 = create_model(GoogleAdAccount, name=None) create_model( GoogleAdReportingConnection, identity_id=identity_id, ad_accounts=[ad_account_1, ad_account_2], user_id=user_id, ) ad_accounts = repository.find_by_identity_id_and_user_id_for_assigment( identity_id, user_id=user_id ) assert ad_accounts == IsList( GoogleUserAdAccountLabel.model_construct( id=ad_account_1.id, external_id=ad_account_1.external_id, name=ad_account_1.external_id, business_account_name=ad_account_1.business_account_name, campaigns_count=ad_account_1.campaigns_count, vendor_id=None, subaccount_id=None, ), GoogleUserAdAccountLabel.model_construct( id=ad_account_2.id, external_id=ad_account_2.external_id, name=ad_account_2.external_id, business_account_name=ad_account_2.business_account_name, campaigns_count=ad_account_2.campaigns_count, vendor_id=None, subaccount_id=None, ), check_order=False, ) @pytest.mark.db def test_find_by_identity_id_and_user_id_for_assigment_user_connection_empty_ad_reporting_has_accounts( self, repository: GoogleAdAccountRepository, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_account_1 = create_model(GoogleAdAccount, name="ad_account_1") ad_account_2 = create_model(GoogleAdAccount, name="ad_account_2") # User connection exists but WITHOUT ad accounts create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[], ) # Ad reporting has accounts create_model( GoogleAdReportingConnection, identity_id=identity_id, user_id=user_id, ad_accounts=[ad_account_1, ad_account_2], ) ad_accounts = repository.find_by_identity_id_and_user_id_for_assigment( identity_id, user_id ) # Should return accounts from ad reporting assert ad_accounts == [ GoogleUserAdAccountLabel.model_construct( id=ad_account_1.id, external_id=ad_account_1.external_id, name=ad_account_1.name or "", business_account_name=ad_account_1.business_account_name, campaigns_count=ad_account_1.campaigns_count, vendor_id=None, subaccount_id=None, ), GoogleUserAdAccountLabel.model_construct( id=ad_account_2.id, external_id=ad_account_2.external_id, name=ad_account_2.name or "", business_account_name=ad_account_2.business_account_name, campaigns_count=ad_account_2.campaigns_count, vendor_id=None, subaccount_id=None, ), ] @pytest.mark.db def test_find_by_identity_id_and_user_id_for_assigment_not_connected( self, repository: GoogleAdAccountRepository, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: ad_account_1 = create_model(GoogleAdAccount, name="ad_account_1") ad_account_2 = create_model(GoogleAdAccount, name="ad_account_2") user_id = fake.pystr() create_model( GoogleUserAdAccount, identity_id=identity_id, ad_account_id=ad_account_1.id ) create_model( GoogleUserAdAccount, identity_id=identity_id, ad_account_id=ad_account_2.id ) ad_accounts = repository.find_by_identity_id_and_user_id_for_assigment( identity_id, user_id=user_id ) assert ad_accounts == []