from unittest import mock import pytest from fansifter_common.adapters.ows_account import Subaccount, Vendor from dmp.adapters.fivetran.models import ConnectCard, ShopifyConnection from dmp.shopify.handlers import ConnectStoreHandler, ConnectStoreRequest from dmp.shopify.models import ShopifyStoreAssociation from tests.unit.faker import FakerTyped from tests.unit.types import BuildModel, CreateReportingModel class TestConnectStoreHandler: @pytest.mark.db def test_connect_store_by_vendor( self, handler: ConnectStoreHandler, fake: FakerTyped, build_model: BuildModel, create_reporting_model: CreateReportingModel, fivetran_client_mock: mock.MagicMock, ows_account_client_mock: mock.MagicMock, identity_id: str, ) -> None: vendor_id = 7123 subaccount_id = 0 global_participant_id = fake.uuid4_string() fivetran_connection = build_model(ShopifyConnection) connect_card = build_model(ConnectCard) # Existing store association by the same vendor create_reporting_model( ShopifyStoreAssociation, vendor_id=vendor_id, subaccount_id=subaccount_id ) fivetran_client_mock.create_shopify_connection.return_value = ( fivetran_connection ) fivetran_client_mock.get_connect_card.return_value = connect_card ows_account_client_mock.get_vendor.return_value = Vendor( vendor_id=vendor_id, account_name="Test vèndor", company_brand="sme", ) association, connect_card = handler.handle( ConnectStoreRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, shop_domain=fake.domain_name(), redirect_uri=fake.url(), ) ) assert association.fivetran_connector_id == fivetran_connection.id assert association.fivetran_schema == f"store_{vendor_id}_test_vendor_002" assert association.vendor_id == vendor_id assert association.subaccount_id == subaccount_id assert association.global_participant_id == global_participant_id assert association.created_by == identity_id @pytest.mark.db def test_connect_store_by_subaccount( self, handler: ConnectStoreHandler, fake: FakerTyped, build_model: BuildModel, create_reporting_model: CreateReportingModel, fivetran_client_mock: mock.MagicMock, ows_account_client_mock: mock.MagicMock, identity_id: str, ) -> None: vendor_id = 7123 subaccount_id = 8888 global_participant_id = fake.uuid4_string() fivetran_connection = build_model(ShopifyConnection) connect_card = build_model(ConnectCard) # Existing store association by the same vendor create_reporting_model( ShopifyStoreAssociation, vendor_id=vendor_id, subaccount_id=subaccount_id ) fivetran_client_mock.create_shopify_connection.return_value = ( fivetran_connection ) fivetran_client_mock.get_connect_card.return_value = connect_card ows_account_client_mock.get_subaccount.return_value = Subaccount( vendor_id=vendor_id, subaccount_id=subaccount_id, subaccount_name="Test Subaccóuñt", ) association, connect_card = handler.handle( ConnectStoreRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, shop_domain=fake.domain_name(), redirect_uri=fake.url(), ) ) assert association.fivetran_connector_id == fivetran_connection.id assert ( association.fivetran_schema == f"store_{subaccount_id}_test_subaccount_002" ) assert association.vendor_id == vendor_id assert association.subaccount_id == subaccount_id assert association.global_participant_id == global_participant_id assert association.created_by == identity_id