from typing import get_args import pytest from faker import Faker from dmp.fandata.models import ( FansByArtistAccountDbt, FansByCustomListAccountDbt, GlobalFansByArtistDbt, ) from dmp.rosters.dtos import FanDataListCriteria, SearchFanDataListCriteria from dmp.rosters.models import ArtistRosterLocalRep, ArtistRosterMainRep from dmp.rosters.repositories import ( FanDataListRepository, ) from dmp.rosters.types import FanDataListOrderBy from tests.unit.types import CreateReportingModel, CreateReportingModelBatch class TestFanDataListRepository: @pytest.mark.db @pytest.mark.parametrize("order_by", get_args(FanDataListOrderBy)) def test_find_by_criteria_order_by( self, order_by: FanDataListOrderBy, repository: FanDataListRepository ) -> None: _ = repository.find_by_criteria( FanDataListCriteria( vendor_ids=[1], subaccount_ids=[1], ), order_by=[order_by], ) @pytest.mark.db def test_search_by_criteria_empty(self, repository: FanDataListRepository) -> None: result = repository.search_by_criteria( SearchFanDataListCriteria(vendor_ids=[1], subaccount_ids=[1]), limit=10, ) assert not result @pytest.mark.db def test_search_by_criteria( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() artist = create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) custom_list = create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], ), limit=10, ) assert len(result) == 2 assert result[0].name == artist.global_participant_name assert result[0].id == artist.global_participant_id assert result[1].name == custom_list.custom_list_name assert result[1].id == custom_list.custom_list_id @pytest.mark.db def test_search_by_criteria_name_match( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() artist = create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], search=artist.global_participant_name[:5], ), limit=10, ) assert len(result) == 1 assert result[0].id == artist.global_participant_id assert result[0].name == artist.global_participant_name @pytest.mark.db def test_search_rosters_limit( self, repository: FanDataListRepository, create_reporting_model_batch: CreateReportingModelBatch, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() create_reporting_model_batch( FansByArtistAccountDbt, size=5, vendor_id=vendor_id, subaccount_id=subaccount_id, ) create_reporting_model_batch( FansByCustomListAccountDbt, size=5, vendor_id=vendor_id, subaccount_id=subaccount_id, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], ), limit=3, ) assert len([item for item in result if item.type == "ARTIST"]) == 3 assert len([item for item in result if item.type == "CUSTOM_LIST"]) == 3 @pytest.mark.db def test_search_by_criteria_partial_name_match_ordering_artists( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() artist_1 = create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_name="foo", ) artist_2 = create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_name="food", ) create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_name="bar", ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], search="fo", ), limit=10, ) assert len(result) == 2 assert result[0].id == artist_1.global_participant_id assert result[0].name == artist_1.global_participant_name assert result[1].id == artist_2.global_participant_id assert result[1].name == artist_2.global_participant_name @pytest.mark.db def test_search_by_criteria_partial_name_match_ordering_custom_lists( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() custom_list_1 = create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, custom_list_name="foo", ) custom_list_2 = create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, custom_list_name="food", ) create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, custom_list_name="bar", ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], search="fo", ), limit=10, ) assert len(result) == 2 assert result[0].id == custom_list_1.custom_list_id assert result[0].name == custom_list_1.custom_list_name assert result[1].id == custom_list_2.custom_list_id assert result[1].name == custom_list_2.custom_list_name @pytest.mark.db def test_search_by_criteria_duplicated_artist( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() artist = create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id + 1, subaccount_id=subaccount_id + 1, global_participant_id=artist.global_participant_id, global_participant_name=artist.global_participant_name, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], ), limit=10, ) assert len(result) == 1 @pytest.mark.db def test_search_by_criteria_global_empty( self, repository: FanDataListRepository ) -> None: result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[1], subaccount_ids=[1], global_vendor_ids=[2], ), limit=10, ) assert not result @pytest.mark.db def test_search_by_criteria_global_main_rep( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() global_participant_id = faker.uuid4() global_participant_name = faker.name() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) global_artist = create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id, global_participant_name=global_participant_name, ) create_reporting_model( FansByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, global_participant_name=global_participant_name, ) custom_list = create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], global_vendor_ids=[vendor_id], ), limit=10, ) assert len(result) == 2 assert result[0].name == global_artist.global_participant_name assert result[0].id == global_artist.global_participant_id assert result[0].is_main_rep is True assert result[1].name == custom_list.custom_list_name assert result[1].id == custom_list.custom_list_id assert result[1].is_main_rep is None @pytest.mark.db def test_search_by_criteria_global_local_rep( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() global_participant_id = faker.uuid4() create_reporting_model( ArtistRosterLocalRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) artist = create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id, ) custom_list = create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], global_vendor_ids=[vendor_id], ), limit=10, ) assert len(result) == 2 assert result[0].name == artist.global_participant_name assert result[0].id == artist.global_participant_id assert result[0].is_main_rep is False assert result[1].name == custom_list.custom_list_name assert result[1].id == custom_list.custom_list_id assert result[1].is_main_rep is None @pytest.mark.db def test_search_by_criteria_global_name_match( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() global_participant_id = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) artist = create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id, ) create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], search=artist.global_participant_name[:5], global_vendor_ids=[vendor_id], ), limit=10, ) assert len(result) == 1 assert result[0].id == artist.global_participant_id assert result[0].name == artist.global_participant_name @pytest.mark.db def test_search_rosters_global_limit( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, create_reporting_model_batch: CreateReportingModelBatch, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() global_participant_id_1 = faker.uuid4() global_participant_id_2 = faker.uuid4() global_participant_id_3 = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_1, ) create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_2, ) create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_3, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_1, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_2, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_3, ) create_reporting_model_batch( FansByCustomListAccountDbt, size=3, vendor_id=vendor_id, subaccount_id=subaccount_id, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], global_vendor_ids=[vendor_id], ), limit=2, ) assert len([item for item in result if item.type == "ARTIST"]) == 2 assert len([item for item in result if item.type == "CUSTOM_LIST"]) == 2 @pytest.mark.db def test_search_by_criteria_global_partial_name_match_ordering_artists( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() global_participant_id_1 = faker.uuid4() global_participant_id_2 = faker.uuid4() global_participant_id_3 = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_1, ) create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_2, ) create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_3, ) artist_1 = create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_1, global_participant_name="foo", ) artist_2 = create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_2, global_participant_name="food", ) # Should not be included in the search results create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_3, global_participant_name="bar", ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], search="fo", global_vendor_ids=[vendor_id], ), limit=10, ) assert len(result) == 2 assert result[0].id == artist_1.global_participant_id assert result[0].name == artist_1.global_participant_name assert result[1].id == artist_2.global_participant_id assert result[1].name == artist_2.global_participant_name @pytest.mark.db def test_search_by_criteria_custom_list_name_match_ordering_custom_lists( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() custom_list_1 = create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, custom_list_name="foo", ) custom_list_2 = create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, custom_list_name="food", ) create_reporting_model( FansByCustomListAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, custom_list_name="bar", ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], search="fo", global_vendor_ids=[vendor_id], ), limit=10, ) assert len(result) == 2 assert result[0].id == custom_list_1.custom_list_id assert result[0].name == custom_list_1.custom_list_name assert result[1].id == custom_list_2.custom_list_id assert result[1].name == custom_list_2.custom_list_name @pytest.mark.db def test_search_by_criteria_main_rep_only( self, repository: FanDataListRepository, create_reporting_model: CreateReportingModel, faker: Faker, ) -> None: vendor_id = faker.pyint() subaccount_id = faker.pyint() global_participant_id_1 = faker.uuid4() global_participant_id_2 = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_1, ) # This should not be included in the search results create_reporting_model( ArtistRosterLocalRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id_2, ) artist_1 = create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_1, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id_2, ) result = repository.search_by_criteria( SearchFanDataListCriteria( vendor_ids=[vendor_id], subaccount_ids=[subaccount_id], global_vendor_ids=[vendor_id], exclude_local_reps=True, ), limit=10, ) assert len(result) == 1 assert result[0].id == artist_1.global_participant_id assert result[0].name == artist_1.global_participant_name