import pytest from dmp.google.dtos import AssignGoogleUserAdAccountLabelData from dmp.google.exceptions import ( GoogleUserConnectionNotFoundError, InvalidGoogleAdAccountId, ) from dmp.google.handlers import ( AssignLabelsToGoogleUserAdAccountsHandler, AssignLabelsToGoogleUserAdAccountsRequest, ) from dmp.google.models import ( GoogleAdAccount, GoogleAdReportingConnection, GoogleUserAdAccount, GoogleUserConnection, GoogleUserConnectionAdAccount, ) from dmp.google.repositories import GoogleUserAdAccountRepository from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel class TestAssignLabelsToGoogleUserAdAccountsHandler: @pytest.mark.db def test_assign_labels_to_the_user_ad_account( self, handler: AssignLabelsToGoogleUserAdAccountsHandler, user_ad_account_repository: GoogleUserAdAccountRepository, create_model: CreateModel, identity_id: str, user_id: str, fake: FakerTyped, ) -> None: ad_account = create_model(GoogleAdAccount) ad_account2 = create_model(GoogleAdAccount) create_model(GoogleAdAccount) user_ad_account = create_model( GoogleUserAdAccount, identity_id=identity_id, ad_account_id=ad_account.id, vendor_id=fake.integer(), subaccount_id=fake.integer(), ) create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account2), GoogleUserConnectionAdAccount(ad_account=ad_account), ], ) new_vendor_id = fake.integer() new_subaccount_id = fake.integer() handler.handle( AssignLabelsToGoogleUserAdAccountsRequest( identity_id=identity_id, assignments=[ AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account.id, vendor_id=user_ad_account.vendor_id, subaccount_id=user_ad_account.subaccount_id, ), AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account2.id, vendor_id=new_vendor_id, subaccount_id=new_subaccount_id, ), ], ) ) user_ad_accounts = user_ad_account_repository.find_by_identity_id( identity_id=identity_id ) assert user_ad_accounts assert len(user_ad_accounts) == 2 assert user_ad_accounts[0].ad_account_id == ad_account.id assert user_ad_accounts[0].vendor_id == user_ad_account.vendor_id assert user_ad_accounts[0].subaccount_id == user_ad_account.subaccount_id assert user_ad_accounts[1].ad_account_id == ad_account2.id assert user_ad_accounts[1].vendor_id == new_vendor_id assert user_ad_accounts[1].subaccount_id == new_subaccount_id @pytest.mark.db def test_assign_labels_to_the_user_ad_account_with_invalid_ad_account_id( self, handler: AssignLabelsToGoogleUserAdAccountsHandler, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: ad_account = create_model(GoogleAdAccount) with pytest.raises(InvalidGoogleAdAccountId): handler.handle( AssignLabelsToGoogleUserAdAccountsRequest( identity_id=identity_id, assignments=[ AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account.id, vendor_id=fake.integer(), subaccount_id=fake.integer(), ), AssignGoogleUserAdAccountLabelData( ad_account_id=fake.pystr(), vendor_id=fake.integer(), subaccount_id=fake.integer(), ), ], ) ) @pytest.mark.db def test_assign_labels_to_the_user_ad_account_with_missed_connection_for_ad_account( self, handler: AssignLabelsToGoogleUserAdAccountsHandler, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: ad_account = create_model(GoogleAdAccount) ad_account2 = create_model(GoogleAdAccount) create_model(GoogleAdAccount) user_ad_account = create_model( GoogleUserAdAccount, identity_id=identity_id, ad_account_id=ad_account.id, vendor_id=fake.integer(), subaccount_id=fake.integer(), ) new_vendor_id = fake.integer() new_subaccount_id = fake.integer() with pytest.raises(GoogleUserConnectionNotFoundError): handler.handle( AssignLabelsToGoogleUserAdAccountsRequest( identity_id=identity_id, assignments=[ AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account.id, vendor_id=user_ad_account.vendor_id, subaccount_id=user_ad_account.subaccount_id, ), AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account2.id, vendor_id=new_vendor_id, subaccount_id=new_subaccount_id, ), ], ) ) @pytest.mark.db def test_assign_labels_with_ad_reporting_connection_only( self, handler: AssignLabelsToGoogleUserAdAccountsHandler, user_ad_account_repository: GoogleUserAdAccountRepository, create_model: CreateModel, identity_id: str, user_id: str, fake: FakerTyped, ) -> None: ad_account = create_model(GoogleAdAccount) ad_account2 = create_model(GoogleAdAccount) create_model( GoogleAdReportingConnection, identity_id=identity_id, user_id=user_id, ad_accounts=[ad_account, ad_account2], ) vendor_id = fake.integer() subaccount_id = fake.integer() handler.handle( AssignLabelsToGoogleUserAdAccountsRequest( identity_id=identity_id, assignments=[ AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account.id, vendor_id=vendor_id, subaccount_id=subaccount_id, ), ], ) ) user_ad_accounts = user_ad_account_repository.find_by_identity_id( identity_id=identity_id ) assert user_ad_accounts assert len(user_ad_accounts) == 1 assert user_ad_accounts[0].ad_account_id == ad_account.id assert user_ad_accounts[0].vendor_id == vendor_id assert user_ad_accounts[0].subaccount_id == subaccount_id @pytest.mark.db def test_assign_labels_with_both_user_and_ad_reporting_connections( self, handler: AssignLabelsToGoogleUserAdAccountsHandler, user_ad_account_repository: GoogleUserAdAccountRepository, create_model: CreateModel, identity_id: str, user_id: str, fake: FakerTyped, ) -> None: ad_account_user = create_model(GoogleAdAccount) ad_account_reporting = create_model(GoogleAdAccount) ad_account_both = create_model(GoogleAdAccount) create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account_user), GoogleUserConnectionAdAccount(ad_account=ad_account_both), ], ) create_model( GoogleAdReportingConnection, identity_id=identity_id, user_id=user_id, ad_accounts=[ad_account_reporting, ad_account_both], ) vendor_id_1 = fake.integer() subaccount_id_1 = fake.integer() vendor_id_2 = fake.integer() subaccount_id_2 = fake.integer() vendor_id_3 = fake.integer() subaccount_id_3 = fake.integer() handler.handle( AssignLabelsToGoogleUserAdAccountsRequest( identity_id=identity_id, assignments=[ AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account_user.id, vendor_id=vendor_id_1, subaccount_id=subaccount_id_1, ), AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account_reporting.id, vendor_id=vendor_id_2, subaccount_id=subaccount_id_2, ), AssignGoogleUserAdAccountLabelData( ad_account_id=ad_account_both.id, vendor_id=vendor_id_3, subaccount_id=subaccount_id_3, ), ], ) ) user_ad_accounts = user_ad_account_repository.find_by_identity_id( identity_id=identity_id ) assert user_ad_accounts assert len(user_ad_accounts) == 3 accounts_by_id = {uaa.ad_account_id: uaa for uaa in user_ad_accounts} assert ad_account_user.id in accounts_by_id assert accounts_by_id[ad_account_user.id].vendor_id == vendor_id_1 assert accounts_by_id[ad_account_user.id].subaccount_id == subaccount_id_1 assert ad_account_reporting.id in accounts_by_id assert accounts_by_id[ad_account_reporting.id].vendor_id == vendor_id_2 assert accounts_by_id[ad_account_reporting.id].subaccount_id == subaccount_id_2 assert ad_account_both.id in accounts_by_id assert accounts_by_id[ad_account_both.id].vendor_id == vendor_id_3 assert accounts_by_id[ad_account_both.id].subaccount_id == subaccount_id_3