import pytest from dmp.google.exceptions import GoogleUserConnectionNotFoundError from dmp.google.handlers import ( GetGoogleUserAdAccountsHandler, GetGoogleUserAdAccountsRequest, ) from dmp.google.models import ( GoogleAdAccount, GoogleUserConnection, GoogleUserConnectionAdAccount, ) from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel class TestGetGoogleUserAdAccountsHandler: @pytest.mark.db def test_get_user_ad_accounts_empty_list( self, handler: GetGoogleUserAdAccountsHandler, create_model: CreateModel, identity_id: str, user_id: str, ) -> None: create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[], ) ad_accounts = handler.handle( GetGoogleUserAdAccountsRequest(identity_id=identity_id, user_id=user_id) ) assert ad_accounts == [] @pytest.mark.db def test_get_user_ad_accounts_missed_connection( self, handler: GetGoogleUserAdAccountsHandler, identity_id: str, user_id: str ) -> None: with pytest.raises(GoogleUserConnectionNotFoundError): handler.handle( GetGoogleUserAdAccountsRequest( identity_id=identity_id, user_id=user_id ), ) @pytest.mark.db def test_get_user_ad_accounts_name_is_empty_string( self, handler: GetGoogleUserAdAccountsHandler, create_model: CreateModel, identity_id: str, user_id: str, ) -> None: ad_account = create_model(GoogleAdAccount, name="") create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account), ], ) ad_accounts = handler.handle( GetGoogleUserAdAccountsRequest(identity_id=identity_id, user_id=user_id) ) assert ad_accounts assert ad_accounts[0].external_id == ad_account.external_id assert ad_accounts[0].name == ad_account.external_id @pytest.mark.db def test_get_user_ad_accounts_name_is_not_empty_string( self, handler: GetGoogleUserAdAccountsHandler, create_model: CreateModel, identity_id: str, user_id: str, fake: FakerTyped, ) -> None: name = fake.pystr() ad_account = create_model(GoogleAdAccount, name=name) create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account), ], ) ad_accounts = handler.handle( GetGoogleUserAdAccountsRequest(identity_id=identity_id, user_id=user_id) ) assert ad_accounts assert ad_accounts[0].external_id == ad_account.external_id assert ad_accounts[0].name == name