import pytest from dirty_equals import IsList from dmp.ad_accounts.enums import AdAccountPlatform from dmp.ad_accounts.models import AdAccountDbt from dmp.adapters.tiktok.models import AdAccount from dmp.tiktok.dtos import TikTokUserAdAccountLabel from dmp.tiktok.models import TikTokAdAccount, TikTokUserConnection from dmp.tiktok.services import TikTokAdAccountService from tests.unit.faker import FakerTyped from tests.unit.tiktok.equals import IsUserAdAccount from tests.unit.types import BuildModel, CreateModel, CreateReportingModel class TestTikTokAdAccountService: @pytest.mark.db def test_get_user_ad_accounts_for_assignment( self, service: TikTokAdAccountService, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_accounts = service.get_user_ad_accounts_for_assignment(identity_id, user_id) assert ad_accounts == [] @pytest.mark.db def test_get_user_ad_accounts_for_assignment_fill_empty_data( self, service: TikTokAdAccountService, create_model: CreateModel, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_account = create_model(TikTokAdAccount, name=None, campaigns_count=0) ad_reporting_account = create_reporting_model( AdAccountDbt, id=ad_account.external_id, platform=AdAccountPlatform.TIKTOK, ) create_model( TikTokUserConnection, identity_id=identity_id, user_id=user_id, ad_accounts=[ad_account], ) ad_accounts = service.get_user_ad_accounts_for_assignment(identity_id, user_id) assert ad_accounts == [ TikTokUserAdAccountLabel.model_construct( id=ad_account.id, external_id=ad_account.external_id, name=ad_reporting_account.name, business_center_name=ad_account.business_center_name, campaigns_count=max( ad_account.campaigns_count or 0, ad_reporting_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: TikTokAdAccountService, create_model: CreateModel, identity_id: str, fake: FakerTyped, ) -> None: user_id = fake.pystr() ad_account = create_model( TikTokAdAccount, name=None, ) create_model( TikTokUserConnection, identity_id=identity_id, user_id=user_id, ad_accounts=[ad_account], ) ad_accounts = service.get_user_ad_accounts_for_assignment(identity_id, user_id) assert ad_accounts == [ TikTokUserAdAccountLabel.model_construct( id=ad_account.id, external_id=ad_account.external_id, name=ad_account.external_id, business_center_name=ad_account.business_center_name, 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: TikTokAdAccountService, create_model: CreateModel ) -> None: connection = create_model(TikTokUserConnection) 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: TikTokAdAccountService, create_model: CreateModel, build_model: BuildModel, ) -> None: api_ad_account_1 = build_model(AdAccount) api_ad_account_2 = build_model(AdAccount) connection = create_model(TikTokUserConnection) ad_accounts = service.get_ad_accounts_for_connection( connection, external_ad_accounts=[api_ad_account_1, api_ad_account_2] ) assert ad_accounts assert len(ad_accounts) == 2 assert ad_accounts == IsList( IsUserAdAccount(api_ad_account_1), IsUserAdAccount(api_ad_account_2), check_order=False, )