import pytest from dmp.adapters.features import AUDIENCE_SHOW_QA_FAN_SEGMENTATION_MODEL from dmp.artists.dtos import FansSegmentValue from dmp.artists.handlers import ( GetArtistFansCountBySegmentHandler, GetArtistFansCountBySegmentRequest, ) from dmp.fandata.enums import FanSegment from dmp.fandata.models import ( FansByArtistAccountSegmentCountryDbt, FansByArtistAccountSegmentDbt, GlobalFansByArtistSegmentCountryDbt, GlobalFansByArtistSegmentDbt, QaFansByArtistAccountSegmentCountryDbt, QaFansByArtistAccountSegmentDbt, ) from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel, EnableFeatures class TestGetArtistAccountFansCountBySegmentHandler: @pytest.mark.db def test_get_artist_fans_count_by_segment_without_country( self, handler: GetArtistFansCountBySegmentHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = 0 global_participant_id = fake.uuid4_string() create_reporting_model( FansByArtistAccountSegmentDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.NEW_FANS, fans_count=100, ) create_reporting_model( FansByArtistAccountSegmentDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, fans_count=200, ) create_reporting_model( FansByArtistAccountSegmentDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.CASUAL_FANS, fans_count=500, ) create_reporting_model( FansByArtistAccountSegmentDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.SECONDARY_FANS, fans_count=400, ) result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ), ) assert result == [ FansSegmentValue(label=FanSegment.SUPER_FANS, value=200), FansSegmentValue(label=FanSegment.ENGAGED_FANS, value=0), FansSegmentValue(label=FanSegment.CASUAL_FANS, value=500), FansSegmentValue(label=FanSegment.FANS_TO_WIN_BACK, value=0), FansSegmentValue(label=FanSegment.NEW_FANS, value=100), ] @pytest.mark.db def test_get_artist_fans_count_by_segment_without_country_empty( self, handler: GetArtistFansCountBySegmentHandler, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = 0 global_participant_id = fake.uuid4_string() result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ), ) assert result == [] @pytest.mark.db def test_get_artist_fans_count_by_segment_without_country_use_qa_model( self, handler: GetArtistFansCountBySegmentHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, enable_features: EnableFeatures, ) -> None: vendor_id = fake.integer() subaccount_id = 0 global_participant_id = fake.uuid4_string() create_reporting_model( QaFansByArtistAccountSegmentDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.NEW_FANS, fans_count=100, ) create_reporting_model( QaFansByArtistAccountSegmentDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, fans_count=200, ) create_reporting_model( QaFansByArtistAccountSegmentDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.CASUAL_FANS, fans_count=500, ) with enable_features([AUDIENCE_SHOW_QA_FAN_SEGMENTATION_MODEL]): result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ), ) assert result == [ FansSegmentValue(label=FanSegment.SUPER_FANS, value=200), FansSegmentValue(label=FanSegment.ENGAGED_FANS, value=0), FansSegmentValue(label=FanSegment.CASUAL_FANS, value=500), FansSegmentValue(label=FanSegment.FANS_TO_WIN_BACK, value=0), FansSegmentValue(label=FanSegment.NEW_FANS, value=100), ] @pytest.mark.parametrize( "countries, expected", [ ( ["GB"], [ FansSegmentValue(label=FanSegment.SUPER_FANS, value=0), FansSegmentValue(label=FanSegment.ENGAGED_FANS, value=0), FansSegmentValue(label=FanSegment.CASUAL_FANS, value=500), FansSegmentValue(label=FanSegment.FANS_TO_WIN_BACK, value=0), FansSegmentValue(label=FanSegment.NEW_FANS, value=0), ], ), ( ["US", "EE"], [ FansSegmentValue(label=FanSegment.SUPER_FANS, value=850), FansSegmentValue(label=FanSegment.ENGAGED_FANS, value=0), FansSegmentValue(label=FanSegment.CASUAL_FANS, value=0), FansSegmentValue(label=FanSegment.FANS_TO_WIN_BACK, value=0), FansSegmentValue(label=FanSegment.NEW_FANS, value=100), ], ), ], ) @pytest.mark.db def test_get_artist_fans_count_by_segment_with_country( self, handler: GetArtistFansCountBySegmentHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, countries: list[str], expected: list[FansSegmentValue], ) -> None: vendor_id = fake.integer() subaccount_id = 0 global_participant_id = fake.uuid4_string() country_1 = "US" country_2 = "EE" country_3 = "GB" create_reporting_model( FansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.NEW_FANS, country_iso2=country_1, fans_count=100, ) create_reporting_model( FansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, country_iso2=country_1, fans_count=200, ) create_reporting_model( FansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, country_iso2=country_2, fans_count=650, ) create_reporting_model( FansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.CASUAL_FANS, country_iso2=country_3, fans_count=500, ) result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=countries, ), ) assert result == expected @pytest.mark.db def test_get_artist_fans_count_by_segment_with_country_empty( self, handler: GetArtistFansCountBySegmentHandler, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = 0 global_participant_id = fake.uuid4_string() result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=["EE"], ), ) assert result == [] @pytest.mark.parametrize( "countries, expected", [ ( ["US", "EE"], [ FansSegmentValue(label=FanSegment.SUPER_FANS, value=850), FansSegmentValue(label=FanSegment.ENGAGED_FANS, value=0), FansSegmentValue(label=FanSegment.CASUAL_FANS, value=0), FansSegmentValue(label=FanSegment.FANS_TO_WIN_BACK, value=0), FansSegmentValue(label=FanSegment.NEW_FANS, value=100), ], ), ], ) @pytest.mark.db def test_get_artist_fans_count_by_segment_with_country_use_qa_model( self, handler: GetArtistFansCountBySegmentHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, countries: list[str], expected: list[FansSegmentValue], enable_features: EnableFeatures, ) -> None: vendor_id = fake.integer() subaccount_id = 0 global_participant_id = fake.uuid4_string() country_1 = "US" country_2 = "EE" country_3 = "GB" create_reporting_model( QaFansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.NEW_FANS, country_iso2=country_1, fans_count=100, ) create_reporting_model( QaFansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, country_iso2=country_1, fans_count=200, ) create_reporting_model( QaFansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, country_iso2=country_2, fans_count=650, ) create_reporting_model( QaFansByArtistAccountSegmentCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, segment_name=FanSegment.CASUAL_FANS, country_iso2=country_3, fans_count=500, ) with enable_features([AUDIENCE_SHOW_QA_FAN_SEGMENTATION_MODEL]): result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=countries, ), ) assert result == expected class TestGetArtistGlobalFansCountBySegmentHandler: @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_count_by_segment_without_country( self, handler: GetArtistFansCountBySegmentHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() create_reporting_model( GlobalFansByArtistSegmentDbt, global_participant_id=global_participant_id, segment_name=FanSegment.NEW_FANS, fans_count=100, ) create_reporting_model( GlobalFansByArtistSegmentDbt, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, fans_count=200, ) result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, ), ) assert result == [ FansSegmentValue(label=FanSegment.SUPER_FANS, value=200), FansSegmentValue(label=FanSegment.ENGAGED_FANS, value=0), FansSegmentValue(label=FanSegment.CASUAL_FANS, value=0), FansSegmentValue(label=FanSegment.FANS_TO_WIN_BACK, value=0), FansSegmentValue(label=FanSegment.NEW_FANS, value=100), ] @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_count_by_segment_without_country_empty( self, handler: GetArtistFansCountBySegmentHandler, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, ), ) assert result == [] @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_count_by_segment_with_country( self, handler: GetArtistFansCountBySegmentHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() country_1 = "EE" country_2 = "GB" create_reporting_model( GlobalFansByArtistSegmentCountryDbt, global_participant_id=global_participant_id, segment_name=FanSegment.SUPER_FANS, country_iso2=country_1, fans_count=200, ) create_reporting_model( GlobalFansByArtistSegmentCountryDbt, global_participant_id=global_participant_id, segment_name=FanSegment.CASUAL_FANS, country_iso2=country_2, fans_count=500, ) result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, countries=["GB"], ), ) assert result == [ FansSegmentValue(label=FanSegment.SUPER_FANS, value=0), FansSegmentValue(label=FanSegment.ENGAGED_FANS, value=0), FansSegmentValue(label=FanSegment.CASUAL_FANS, value=500), FansSegmentValue(label=FanSegment.FANS_TO_WIN_BACK, value=0), FansSegmentValue(label=FanSegment.NEW_FANS, value=0), ] @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_count_by_segment_with_country_empty( self, handler: GetArtistFansCountBySegmentHandler, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() result = handler.handle( GetArtistFansCountBySegmentRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, countries=["EE"], ), ) assert result == []