from unittest import mock import pytest from anydi import Container from faker import Faker from fansifter_common.auth.account import Account from dmp.fandata.models import ( FansByArtistAccountDbt, FansByCustomListAccountDbt, GlobalFansByArtistDbt, ) from dmp.rosters.handlers import SearchRostersHandler, SearchRostersRequest from dmp.rosters.models import ArtistRosterMainRep from dmp.rosters.services import GlobalFanDataAccessService from tests.unit.types import CreateReportingModel class TestSearchRosters: @pytest.mark.db def test_search_rosters_empty( self, handler: SearchRostersHandler, identity_id: str, account: Account ) -> None: response = handler.handle( SearchRostersRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, limit=10, ) ) assert response.artists == [] assert response.custom_lists == [] @pytest.mark.db def test_search_rosters( self, handler: SearchRostersHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: artist = create_reporting_model( FansByArtistAccountDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) custom_list = create_reporting_model( FansByCustomListAccountDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) response = handler.handle( SearchRostersRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, limit=10, ) ) assert len(response.artists) == 1 assert len(response.custom_lists) == 1 assert response.artists[0].name == artist.global_participant_name assert response.artists[0].id == artist.global_participant_id assert response.custom_lists[0].name == custom_list.custom_list_name assert response.custom_lists[0].id == custom_list.custom_list_id @pytest.mark.db def test_search_rosters_global( self, container: Container, handler: SearchRostersHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, faker: Faker, ) -> None: global_participant_id = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=account.vendor_id, subaccount_id=account.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=account.vendor_id, subaccount_id=account.subaccount_id, ) global_fandata_access_service_mock = mock.MagicMock( spec=GlobalFanDataAccessService, get_enabled_for_any_vendor=mock.Mock(return_value=[account.vendor_id]), ) with container.override( GlobalFanDataAccessService, global_fandata_access_service_mock ): response = handler.handle( SearchRostersRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, limit=10, ) ) assert len(response.artists) == 1 assert len(response.custom_lists) == 1 assert response.artists[0].name == artist.global_participant_name assert response.artists[0].id == artist.global_participant_id assert response.custom_lists[0].name == custom_list.custom_list_name assert response.custom_lists[0].id == custom_list.custom_list_id