import pytest from dmp.shopify.dtos import CollectionArtist from dmp.shopify.exceptions import ShopifyCollectionNotFoundError from dmp.shopify.handlers import ( UpdateCollectionArtistAssociationsHandler, UpdateCollectionArtistAssociationsRequest, ) from dmp.shopify.models import ( ShopifyCollection, ShopifyCollectionArtist, ShopifyStoreAssociation, ) from dmp.shopify.repositories import CollectionArtistRepository from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel class TestUpdateCollectionArtistAssociationsHandler: @pytest.mark.db def test_update_collection_artist_associations_unknown_collection_id( self, handler: UpdateCollectionArtistAssociationsHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, ) -> None: identity_id = fake.uuid4_string() collection_id = 1234 association = create_reporting_model( ShopifyStoreAssociation, created_by=identity_id ) create_reporting_model( ShopifyCollection, id=collection_id, source_schema=association.fivetran_schema, ) with pytest.raises(ShopifyCollectionNotFoundError): handler.handle( UpdateCollectionArtistAssociationsRequest( identity_id=identity_id, association_id=association.id, whole_store_global_participant_id=None, collections=[ CollectionArtist( collection_id=5678, global_participant_id=fake.uuid4_string(), ) ], ) ) @pytest.mark.db def test_update_collection_artist_associations_single_artist_to_single_artist( self, handler: UpdateCollectionArtistAssociationsHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, ) -> None: new_global_participant_id = fake.uuid4_string() identity_id = fake.uuid4_string() association = create_reporting_model( ShopifyStoreAssociation, global_participant_id=fake.uuid4_string(), created_by=identity_id, ) updated_store = handler.handle( UpdateCollectionArtistAssociationsRequest( identity_id=identity_id, association_id=association.id, whole_store_global_participant_id=new_global_participant_id, collections=[], ) ) assert updated_store.global_participant_id == new_global_participant_id assert updated_store.artists_count == 1 @pytest.mark.db def test_update_collection_artist_associations_single_artist_to_multi_artist( self, handler: UpdateCollectionArtistAssociationsHandler, collection_artist_repository: CollectionArtistRepository, create_reporting_model: CreateReportingModel, fake: FakerTyped, ) -> None: identity_id = fake.uuid4_string() association = create_reporting_model( ShopifyStoreAssociation, global_participant_id=fake.uuid4_string(), created_by=identity_id, ) collection = create_reporting_model( ShopifyCollection, source_schema=association.fivetran_schema ) collection_global_participant_id = fake.uuid4_string() updated_store = handler.handle( UpdateCollectionArtistAssociationsRequest( identity_id=identity_id, association_id=association.id, whole_store_global_participant_id=None, collections=[ CollectionArtist( collection_id=collection.id, global_participant_id=collection_global_participant_id, ) ], ) ) assert updated_store.global_participant_id is None assert updated_store.artists_count == 1 collection_artists = collection_artist_repository.all() assert collection_artists[0].collection_id == collection.id assert ( collection_artists[0].global_participant_id == collection_global_participant_id ) @pytest.mark.db def test_update_collection_artist_associations_multi_artist_to_single_artist( self, handler: UpdateCollectionArtistAssociationsHandler, collection_artist_repository: CollectionArtistRepository, fake: FakerTyped, create_reporting_model: CreateReportingModel, ) -> None: global_participant_id = fake.uuid4_string() identity_id = fake.uuid4_string() association = create_reporting_model( ShopifyStoreAssociation, global_participant_id=None, created_by=identity_id, ) collection = create_reporting_model( ShopifyCollection, source_schema=association.fivetran_schema ) create_reporting_model( ShopifyCollectionArtist, association_id=association.id, collection_id=collection.id, ) updated_store = handler.handle( UpdateCollectionArtistAssociationsRequest( identity_id=identity_id, association_id=association.id, whole_store_global_participant_id=global_participant_id, collections=[ CollectionArtist( collection_id=collection.id, global_participant_id=fake.uuid4_string(), ) ], ) ) assert updated_store.global_participant_id == global_participant_id assert updated_store.artists_count == 1 collection_artists = collection_artist_repository.all() assert not collection_artists @pytest.mark.db def test_update_collection_artist_associations_multi_artist_to_multi_artist( self, handler: UpdateCollectionArtistAssociationsHandler, collection_artist_repository: CollectionArtistRepository, fake: FakerTyped, create_reporting_model: CreateReportingModel, ) -> None: identity_id = fake.uuid4_string() association = create_reporting_model( ShopifyStoreAssociation, global_participant_id=None, created_by=identity_id, ) collection_1 = create_reporting_model( ShopifyCollection, source_schema=association.fivetran_schema ) collection_2 = create_reporting_model( ShopifyCollection, source_schema=association.fivetran_schema ) collection_2_global_participant_id = fake.uuid4_string() create_reporting_model( ShopifyCollectionArtist, association_id=association.id, collection_id=collection_1.id, ) updated_store = handler.handle( UpdateCollectionArtistAssociationsRequest( identity_id=identity_id, association_id=association.id, whole_store_global_participant_id=None, collections=[ CollectionArtist( collection_id=collection_2.id, global_participant_id=collection_2_global_participant_id, ) ], ) ) assert updated_store.global_participant_id is None assert updated_store.artists_count == 1 collection_artists = collection_artist_repository.all() assert collection_artists[0].collection_id == collection_2.id assert ( collection_artists[0].global_participant_id == collection_2_global_participant_id )