from unittest import mock import pytest from dirty_equals import IsPartialDataclass from dmp.google.exceptions import GoogleAdAccountNotFoundError from dmp.google.handlers import ( DeleteGoogleUserAdAccountHandler, DeleteGoogleUserAdAccountRequest, ) from dmp.google.models import ( GoogleAdAccount, GoogleAdReportingConnection, GoogleUserAdAccount, GoogleUserConnection, GoogleUserConnectionAdAccount, ) from dmp.google.repositories import ( GoogleAdReportingConnectionRepository, GoogleUserAdAccountRepository, GoogleUserConnectionRepository, ) from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel class TestDeleteGoogleUserAdAccountHandler: @pytest.mark.db def test_delete_user_ad_account( self, handler: DeleteGoogleUserAdAccountHandler, create_model: CreateModel, identity_id: str, user_id: str, fivetran_client_mock: mock.MagicMock, user_connection_repository: GoogleUserConnectionRepository, ad_reporting_connection_repository: GoogleAdReportingConnectionRepository, user_ad_account_repository: GoogleUserAdAccountRepository, ) -> None: ad_account_1 = create_model(GoogleAdAccount) ad_account_2 = create_model(GoogleAdAccount) create_model( GoogleUserConnection, is_valid=True, 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_1, ad_account_2], ) 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 ) handler.handle( DeleteGoogleUserAdAccountRequest( identity_id=identity_id, ad_account_id=ad_account_1.id ), ) user_ad_account = ( user_ad_account_repository.find_by_ad_accounts_ids_and_identity_id( identity_id=identity_id, ad_accounts_ids=[ad_account_1.id] ) ) user_connection = user_connection_repository.get_by_identity_id_and_user_id( identity_id, user_id ) ad_reporting_connection = ad_reporting_connection_repository.get_connection_by_identity_id_and_user_id( identity_id, user_id ) fivetran_client_mock.delete_connection.assert_not_called() assert not user_ad_account assert user_connection assert user_connection.connection_ad_accounts == [ IsPartialDataclass(ad_account_id=ad_account_2.id) ] assert ad_reporting_connection assert ad_reporting_connection.ad_accounts == [ad_account_2] @pytest.mark.db def test_delete_user_ad_account_last( self, handler: DeleteGoogleUserAdAccountHandler, create_model: CreateModel, identity_id: str, user_id: str, fake: FakerTyped, fivetran_client_mock: mock.MagicMock, user_connection_repository: GoogleUserConnectionRepository, ad_reporting_connection_repository: GoogleAdReportingConnectionRepository, user_ad_account_repository: GoogleUserAdAccountRepository, ) -> None: ad_account_1 = create_model(GoogleAdAccount) ad_account_2 = create_model(GoogleAdAccount) create_model( GoogleUserConnection, is_valid=True, identity_id=identity_id, user_id=user_id, connection_ad_accounts=[ GoogleUserConnectionAdAccount(ad_account=ad_account_1), ], ) ad_reporting_connection_to_delete = create_model( GoogleAdReportingConnection, identity_id=identity_id, user_id=user_id, ad_accounts=[ad_account_1], ) user_id_2 = fake.pystr() create_model( GoogleUserConnection, identity_id=identity_id, user_id=user_id_2, 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_2, ad_accounts=[ad_account_1, ad_account_2], ) create_model( GoogleUserAdAccount, identity_id=identity_id, ad_account_id=ad_account_1.id ) handler.handle( DeleteGoogleUserAdAccountRequest( identity_id=identity_id, ad_account_id=ad_account_1.id ), ) user_ad_account = ( user_ad_account_repository.find_by_ad_accounts_ids_and_identity_id( identity_id=identity_id, ad_accounts_ids=[ad_account_1.id] ) ) user_connection = user_connection_repository.get_by_identity_id_and_user_id( identity_id, user_id ) ad_reporting_connection = ad_reporting_connection_repository.get_connection_by_identity_id_and_user_id( identity_id, user_id ) user_connection_2 = user_connection_repository.get_by_identity_id_and_user_id( identity_id, user_id_2 ) ad_reporting_connection_2 = ad_reporting_connection_repository.get_connection_by_identity_id_and_user_id( identity_id, user_id_2 ) fivetran_client_mock.delete_connection.assert_called_once_with( ad_reporting_connection_to_delete.fivetran_connector_id ) assert not user_ad_account assert not user_connection assert not ad_reporting_connection assert user_connection_2 assert user_connection_2.ad_accounts_count == 1 assert ad_reporting_connection_2 assert ad_reporting_connection_2.ad_accounts_count == 1 @pytest.mark.db def test_delete_user_ad_account_wrong_ad_account_id( self, handler: DeleteGoogleUserAdAccountHandler, identity_id: str, fake: FakerTyped, ) -> None: ad_account_id = fake.uuid4_string() with pytest.raises(GoogleAdAccountNotFoundError): handler.handle( DeleteGoogleUserAdAccountRequest( identity_id=identity_id, ad_account_id=ad_account_id ), )