import pytest from dirty_equals import IsList from dmp.ad_accounts.enums import AdAccountPlatform from dmp.ad_accounts.models import AdAccountDbt from dmp.adapters.meta.models import Business, UserAdAccount, UserAdAccountTask from dmp.meta.dtos import MetaUserAdAccountLabel from dmp.meta.models import MetaAdAccount, MetaAdReportingConnection, MetaUserConnection from dmp.meta.services import MetaAdAccountService from tests.unit.faker import FakerTyped from tests.unit.meta.equals import IsEmptyUserAdAccount, IsUserAdAccount from tests.unit.types import BuildModel, CreateModel, CreateReportingModel class TestMetaAdAccountService: @pytest.mark.db def test_get_user_ad_accounts_for_assignment( self, service: MetaAdAccountService, identity_id: str ) -> None: ad_accounts = service.get_user_ad_accounts_for_assignment(identity_id) assert ad_accounts == [] @pytest.mark.db def test_get_user_ad_accounts_for_assignment_fill_empty_data( self, service: MetaAdAccountService, create_model: CreateModel, create_reporting_model: CreateReportingModel, identity_id: str, ) -> None: ad_account = create_model( MetaAdAccount, name=None, business_account_name=None, business_account_picture_key=None, ) ad_reporting_account = create_reporting_model( AdAccountDbt, id=ad_account.external_id, platform=AdAccountPlatform.META, ) create_model( MetaUserConnection, identity_id=identity_id, ad_accounts=[ad_account], ) ad_accounts = service.get_user_ad_accounts_for_assignment(identity_id) assert ad_accounts == [ MetaUserAdAccountLabel.model_construct( id=ad_account.id, external_id=ad_account.external_id, name=ad_reporting_account.name, business_account_name=ad_reporting_account.business_account_name, business_account_picture=ad_account.business_account_picture, campaigns_count=max( ad_reporting_account.campaigns_count or 0, ad_account.campaigns_count or 0, ), vendor_id=None, subaccount_id=None, ), ] @pytest.mark.db def test_get_user_ad_accounts_for_assignment_fill_empty_data_name( self, service: MetaAdAccountService, create_model: CreateModel, identity_id: str ) -> None: ad_account = create_model( MetaAdAccount, name=None, business_account_name=None, business_account_picture_key=None, ) create_model( MetaUserConnection, identity_id=identity_id, ad_accounts=[ad_account], ) ad_accounts = service.get_user_ad_accounts_for_assignment(identity_id) assert ad_accounts == [ MetaUserAdAccountLabel.model_construct( id=ad_account.id, external_id=ad_account.external_id, name=ad_account.external_id, business_account_name=ad_account.business_account_name, business_account_picture_key=None, campaigns_count=ad_account.campaigns_count, vendor_id=None, subaccount_id=None, ), ] @pytest.mark.db def test_get_ad_accounts_for_user_connection_empty( self, service: MetaAdAccountService, create_model: CreateModel ) -> None: connection = create_model(MetaUserConnection) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[] ) assert ad_accounts == [] @pytest.mark.db def test_get_ad_accounts_for_user_connection_new_ad_accounts( self, service: MetaAdAccountService, create_model: CreateModel, build_model: BuildModel, ) -> None: api_ad_account_1 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.MANAGE], business=build_model(Business), ) api_ad_account_2 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.OTHER], ) connection = create_model(MetaUserConnection) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_1, api_ad_account_2] ) assert ad_accounts == [IsUserAdAccount(api_ad_account_1)] @pytest.mark.db def test_get_ad_accounts_for_user_connection_update_ad_accounts( self, service: MetaAdAccountService, create_model: CreateModel, build_model: BuildModel, ) -> None: api_ad_account_1 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.MANAGE], business=build_model(Business), ) api_ad_account_2 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.MANAGE], business=build_model(Business), ) connection = create_model( MetaUserConnection, ad_accounts=[build_model(MetaAdAccount, external_id=api_ad_account_1.id)], ) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_1, api_ad_account_2] ) assert ad_accounts == IsList( IsUserAdAccount(api_ad_account_1), IsUserAdAccount(api_ad_account_2), check_order=False, ) @pytest.mark.db def test_get_ad_accounts_for_user_connection_update_api_ad_accounts( self, service: MetaAdAccountService, create_model: CreateModel, build_model: BuildModel, ) -> None: api_ad_account_1 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.MANAGE], business=build_model(Business), ) api_ad_account_2 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.MANAGE], business=build_model(Business), ) create_model(MetaAdAccount, external_id=api_ad_account_2.id) connection = create_model( MetaUserConnection, ad_accounts=[build_model(MetaAdAccount, external_id=api_ad_account_1.id)], ) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_1, api_ad_account_2] ) assert ad_accounts == IsList( IsUserAdAccount(api_ad_account_1), IsUserAdAccount(api_ad_account_2), check_order=False, ) @pytest.mark.db def test_get_ad_accounts_for_user_connection_delete_ad_accounts( self, service: MetaAdAccountService, create_model: CreateModel, build_model: BuildModel, ) -> None: api_ad_account_1 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.MANAGE], business=build_model(Business), ) # business_management permission for this ad_account's business # was not granted by user api_ad_account_2 = build_model( UserAdAccount, user_tasks=[UserAdAccountTask.MANAGE], business=None, ) connection = create_model( MetaUserConnection, ad_accounts=[ build_model(MetaAdAccount, external_id=api_ad_account_1.id), build_model(MetaAdAccount, external_id=api_ad_account_2.id), build_model(MetaAdAccount), ], ) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_1, api_ad_account_2] ) assert ad_accounts == [IsUserAdAccount(api_ad_account_1)] @pytest.mark.db def test_get_ad_accounts_for_ad_reporting_connection_empty( self, service: MetaAdAccountService, create_model: CreateModel ) -> None: connection = create_model(MetaAdReportingConnection) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[] ) assert ad_accounts == [] @pytest.mark.db def test_get_ad_accounts_for_ad_reporting_connection_new_ad_account_ids( self, service: MetaAdAccountService, create_model: CreateModel, fake: FakerTyped ) -> None: api_ad_account_id_1 = fake.pystr() api_ad_account_id_2 = fake.pystr() connection = create_model(MetaAdReportingConnection) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_id_1, api_ad_account_id_2] ) assert ad_accounts == IsList( IsEmptyUserAdAccount(api_ad_account_id_1), IsEmptyUserAdAccount(api_ad_account_id_2), check_order=False, ) @pytest.mark.db def test_get_ad_accounts_for_ad_reporting_connection_update_ad_accounts( self, service: MetaAdAccountService, create_model: CreateModel, fake: FakerTyped ) -> None: api_ad_account_id_1 = fake.pystr() api_ad_account_id_2 = fake.pystr() ad_account_1 = create_model(MetaAdAccount, external_id=api_ad_account_id_1) connection = create_model( MetaAdReportingConnection, ad_accounts=[ad_account_1], ) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_id_1, api_ad_account_id_2] ) assert ad_accounts == IsList( ad_account_1, IsEmptyUserAdAccount(api_ad_account_id_2), check_order=False, ) @pytest.mark.db def test_get_ad_accounts_for_ad_reporting_connection_update_api_ad_accounts( self, service: MetaAdAccountService, create_model: CreateModel, fake: FakerTyped ) -> None: api_ad_account_1_id = fake.pystr() api_ad_account_2_id = fake.pystr() ad_account_1 = create_model(MetaAdAccount, external_id=api_ad_account_1_id) ad_account_2 = create_model(MetaAdAccount, external_id=api_ad_account_2_id) connection = create_model( MetaAdReportingConnection, ad_accounts=[ad_account_1], ) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[ad_account_1.external_id, ad_account_2.external_id], ) assert ad_accounts == IsList( ad_account_1, ad_account_2, check_order=False, ) @pytest.mark.db def test_get_ad_accounts_for_ad_reporting_connection_delete_ad_accounts( self, service: MetaAdAccountService, create_model: CreateModel, fake: FakerTyped ) -> None: api_ad_account_id = fake.pystr() ad_account_1 = create_model(MetaAdAccount, external_id=api_ad_account_id) ad_account_2 = create_model(MetaAdAccount) connection = create_model( MetaAdReportingConnection, ad_accounts=[ad_account_1, ad_account_2], ) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_id] ) assert ad_accounts == [ad_account_1]