import decimal import pytest from dmp.artists.dtos import FansShareByAgeRange from dmp.artists.handlers.get_artist_fans_share_by_age_range import ( GetArtistFansShareByAgeRangeHandler, GetArtistFansShareByAgeRangeRequest, ) from dmp.fandata.enums import AgeRange from dmp.fandata.models import ( FansByArtistAccountAgeRangeCountryDbt, FansByArtistAccountAgeRangeDbt, FansDataAvailabilityAgeRangeByArtistAccountCountryDbt, FansDataAvailabilityAgeRangeByArtistAccountDbt, GlobalFansByArtistAgeRangeCountryDbt, GlobalFansByArtistAgeRangeDbt, GlobalFansDataAvailabilityAgeRangeByArtistCountryDbt, GlobalFansDataAvailabilityAgeRangeByArtistDbt, ) from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel class TestGetArtistAccountFansShareByAgeRangeHandler: @pytest.mark.db def test_get_artist_fans_share_by_age_range_without_country( self, handler: GetArtistFansShareByAgeRangeHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() age_range_1 = AgeRange.FROM_13_TO_17 age_range_2 = AgeRange.FROM_41_TO_51 age_range_3 = AgeRange.FROM_52_TO_60 fans_data_availability = create_reporting_model( FansDataAvailabilityAgeRangeByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) fans_share_1 = create_reporting_model( FansByArtistAccountAgeRangeDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.1"), age_range=age_range_1, ) fans_share_2 = create_reporting_model( FansByArtistAccountAgeRangeDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.5"), age_range=age_range_2, ) fans_share_3 = create_reporting_model( FansByArtistAccountAgeRangeDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.2"), age_range=age_range_3, ) response = handler.handle( GetArtistFansShareByAgeRangeRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ), ) assert ( response.available_fans_share == fans_data_availability.available_fans_share ) assert response.items == [ FansShareByAgeRange( age_range=fans_share_1.age_range, fans_share=fans_share_1.fans_share ), FansShareByAgeRange( age_range=AgeRange.FROM_18_TO_22, fans_share=decimal.Decimal(0) ), FansShareByAgeRange( age_range=AgeRange.FROM_23_TO_30, fans_share=decimal.Decimal(0) ), FansShareByAgeRange( age_range=AgeRange.FROM_31_TO_40, fans_share=decimal.Decimal(0) ), FansShareByAgeRange( age_range=fans_share_2.age_range, fans_share=fans_share_2.fans_share ), FansShareByAgeRange( age_range=fans_share_3.age_range, fans_share=fans_share_3.fans_share ), FansShareByAgeRange( age_range=AgeRange.FROM_61, fans_share=decimal.Decimal(0) ), ] @pytest.mark.db def test_get_artist_fans_share_by_age_range_without_country_empty( self, handler: GetArtistFansShareByAgeRangeHandler, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() response = handler.handle( GetArtistFansShareByAgeRangeRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ), ) assert response.available_fans_share == 0 assert response.items == [] @pytest.mark.parametrize( "countries, expected_availability, expected_age_ranges", [ ( ["GB", "US"], decimal.Decimal("0.053"), [ FansShareByAgeRange( age_range=AgeRange.FROM_13_TO_17, fans_share=decimal.Decimal("0.333"), ), FansShareByAgeRange( age_range=AgeRange.FROM_18_TO_22, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_23_TO_30, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_31_TO_40, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_41_TO_51, fans_share=decimal.Decimal("0.667"), ), FansShareByAgeRange( age_range=AgeRange.FROM_52_TO_60, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_61, fans_share=decimal.Decimal("0.000") ), ], ), ( ["GB", "US", "EE", "AU"], decimal.Decimal("0.069"), [ FansShareByAgeRange( age_range=AgeRange.FROM_13_TO_17, fans_share=decimal.Decimal("0.143"), ), FansShareByAgeRange( age_range=AgeRange.FROM_18_TO_22, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_23_TO_30, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_31_TO_40, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_41_TO_51, fans_share=decimal.Decimal("0.286"), ), FansShareByAgeRange( age_range=AgeRange.FROM_52_TO_60, fans_share=decimal.Decimal("0.571"), ), FansShareByAgeRange( age_range=AgeRange.FROM_61, fans_share=decimal.Decimal("0.000") ), ], ), ], ) @pytest.mark.db def test_get_artist_fans_share_by_age_range_with_country( self, handler: GetArtistFansShareByAgeRangeHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, countries: list[str], expected_availability: decimal.Decimal, expected_age_ranges: list[FansShareByAgeRange], ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() country_1 = "GB" country_2 = "US" country_3 = "EE" country_4 = "AU" fans_count_1 = 100 fans_count_2 = 200 fans_count_3 = 300 age_range_1 = AgeRange.FROM_13_TO_17 age_range_2 = AgeRange.FROM_41_TO_51 age_range_3 = AgeRange.FROM_52_TO_60 create_reporting_model( FansDataAvailabilityAgeRangeByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, available_fans_count=200, total_fans_count=4000, country_iso2=country_1, ) create_reporting_model( FansDataAvailabilityAgeRangeByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, available_fans_count=100, total_fans_count=1666, country_iso2=country_2, ) create_reporting_model( FansDataAvailabilityAgeRangeByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, available_fans_count=300, total_fans_count=3000, country_iso2=country_3, ) create_reporting_model( FansDataAvailabilityAgeRangeByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, available_fans_count=150, total_fans_count=2142, country_iso2=country_4, ) create_reporting_model( FansByArtistAccountAgeRangeCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_1, age_range=age_range_1, fans_count=fans_count_1, ) create_reporting_model( FansByArtistAccountAgeRangeCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_2, age_range=age_range_2, fans_count=fans_count_2, ) create_reporting_model( FansByArtistAccountAgeRangeCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_3, age_range=age_range_3, fans_count=fans_count_3, ) create_reporting_model( FansByArtistAccountAgeRangeCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_4, age_range=age_range_3, fans_count=fans_count_1, ) response = handler.handle( GetArtistFansShareByAgeRangeRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=countries, ), ) assert response.available_fans_share == expected_availability assert response.items == expected_age_ranges @pytest.mark.db def test_get_artist_fans_share_by_age_range_with_country_empty( self, handler: GetArtistFansShareByAgeRangeHandler, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() response = handler.handle( GetArtistFansShareByAgeRangeRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=["GB"], ), ) assert response.available_fans_share == 0 assert response.items == [] class TestGetArtistGlobalFansShareByAgeRangeHandler: @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_share_by_age_range_without_country( self, handler: GetArtistFansShareByAgeRangeHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() age_range_1 = AgeRange.FROM_13_TO_17 age_range_2 = AgeRange.FROM_41_TO_51 age_range_3 = AgeRange.FROM_52_TO_60 fans_data_availability = create_reporting_model( GlobalFansDataAvailabilityAgeRangeByArtistDbt, global_participant_id=global_participant_id, ) fans_share_1 = create_reporting_model( GlobalFansByArtistAgeRangeDbt, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.1"), age_range=age_range_1, ) fans_share_2 = create_reporting_model( GlobalFansByArtistAgeRangeDbt, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.5"), age_range=age_range_2, ) fans_share_3 = create_reporting_model( GlobalFansByArtistAgeRangeDbt, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.2"), age_range=age_range_3, ) response = handler.handle( GetArtistFansShareByAgeRangeRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, ), ) assert ( response.available_fans_share == fans_data_availability.available_fans_share ) assert response.items == [ FansShareByAgeRange( age_range=fans_share_1.age_range, fans_share=fans_share_1.fans_share ), FansShareByAgeRange( age_range=AgeRange.FROM_18_TO_22, fans_share=decimal.Decimal(0) ), FansShareByAgeRange( age_range=AgeRange.FROM_23_TO_30, fans_share=decimal.Decimal(0) ), FansShareByAgeRange( age_range=AgeRange.FROM_31_TO_40, fans_share=decimal.Decimal(0) ), FansShareByAgeRange( age_range=fans_share_2.age_range, fans_share=fans_share_2.fans_share ), FansShareByAgeRange( age_range=fans_share_3.age_range, fans_share=fans_share_3.fans_share ), FansShareByAgeRange( age_range=AgeRange.FROM_61, fans_share=decimal.Decimal(0) ), ] @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_share_by_age_range_with_country( self, handler: GetArtistFansShareByAgeRangeHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() country_1 = "GB" country_2 = "US" country_3 = "EE" country_4 = "AU" fans_count_1 = 100 fans_count_2 = 200 fans_count_3 = 300 age_range_1 = AgeRange.FROM_13_TO_17 age_range_2 = AgeRange.FROM_41_TO_51 age_range_3 = AgeRange.FROM_52_TO_60 create_reporting_model( GlobalFansDataAvailabilityAgeRangeByArtistCountryDbt, global_participant_id=global_participant_id, available_fans_count=200, total_fans_count=4000, country_iso2=country_1, ) create_reporting_model( GlobalFansDataAvailabilityAgeRangeByArtistCountryDbt, global_participant_id=global_participant_id, available_fans_count=100, total_fans_count=1666, country_iso2=country_2, ) create_reporting_model( GlobalFansDataAvailabilityAgeRangeByArtistCountryDbt, global_participant_id=global_participant_id, available_fans_count=300, total_fans_count=3000, country_iso2=country_3, ) create_reporting_model( GlobalFansDataAvailabilityAgeRangeByArtistCountryDbt, global_participant_id=global_participant_id, available_fans_count=150, total_fans_count=2142, country_iso2=country_4, ) create_reporting_model( GlobalFansByArtistAgeRangeCountryDbt, global_participant_id=global_participant_id, country_iso2=country_1, age_range=age_range_1, fans_count=fans_count_1, ) create_reporting_model( GlobalFansByArtistAgeRangeCountryDbt, global_participant_id=global_participant_id, country_iso2=country_2, age_range=age_range_2, fans_count=fans_count_2, ) create_reporting_model( GlobalFansByArtistAgeRangeCountryDbt, global_participant_id=global_participant_id, country_iso2=country_3, age_range=age_range_3, fans_count=fans_count_3, ) create_reporting_model( GlobalFansByArtistAgeRangeCountryDbt, global_participant_id=global_participant_id, country_iso2=country_4, age_range=age_range_3, fans_count=fans_count_1, ) response = handler.handle( GetArtistFansShareByAgeRangeRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, countries=["GB", "US"], ), ) assert response.available_fans_share == decimal.Decimal("0.053") assert response.items == [ FansShareByAgeRange( age_range=AgeRange.FROM_13_TO_17, fans_share=decimal.Decimal("0.333"), ), FansShareByAgeRange( age_range=AgeRange.FROM_18_TO_22, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_23_TO_30, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_31_TO_40, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_41_TO_51, fans_share=decimal.Decimal("0.667"), ), FansShareByAgeRange( age_range=AgeRange.FROM_52_TO_60, fans_share=decimal.Decimal("0.000"), ), FansShareByAgeRange( age_range=AgeRange.FROM_61, fans_share=decimal.Decimal("0.000") ), ]