import decimal import pytest from dmp.artists.dtos import FansShareByGender from dmp.artists.handlers import ( GetArtistFansShareByGenderHandler, GetArtistFansShareByGenderRequest, ) from dmp.fandata.enums import Gender from dmp.fandata.models import ( FansByArtistAccountGenderCountryDbt, FansByArtistAccountGenderDbt, FansDataAvailabilityGenderByArtistAccountCountryDbt, FansDataAvailabilityGenderByArtistAccountDbt, GlobalFansByArtistGenderCountryDbt, GlobalFansByArtistGenderDbt, GlobalFansDataAvailabilityGenderByArtistCountryDbt, GlobalFansDataAvailabilityGenderByArtistDbt, ) from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel class TestGetArtistAccountFansShareByGenderHandler: @pytest.mark.db def test_get_artist_fans_share_by_gender_without_country( self, handler: GetArtistFansShareByGenderHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() fans_data_availability = create_reporting_model( FansDataAvailabilityGenderByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) fans_share_1 = create_reporting_model( FansByArtistAccountGenderDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, gender=Gender.MALE, fans_share=decimal.Decimal("0.1"), ) fans_share_2 = create_reporting_model( FansByArtistAccountGenderDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, gender=Gender.FEMALE, fans_share=decimal.Decimal("0.5"), ) fans_share_3 = create_reporting_model( FansByArtistAccountGenderDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, gender=Gender.PREFER_NOT_TO_ANSWER, fans_share=decimal.Decimal("0.1"), ) response = handler.handle( GetArtistFansShareByGenderRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=None, ), ) assert ( response.available_fans_share == fans_data_availability.available_fans_share ) assert response.items == [ FansShareByGender( gender=fans_share_1.gender, fans_share=fans_share_1.fans_share, ), FansShareByGender( gender=fans_share_2.gender, fans_share=fans_share_2.fans_share, ), FansShareByGender( gender=fans_share_3.gender, fans_share=fans_share_3.fans_share, ), ] @pytest.mark.db def test_get_artist_fans_share_by_gender_with_country( self, handler: GetArtistFansShareByGenderHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() country_us = "US" country_ee = "EE" create_reporting_model( FansDataAvailabilityGenderByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_us, available_fans_count=100, total_fans_count=317, ) create_reporting_model( FansDataAvailabilityGenderByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_ee, available_fans_count=400, total_fans_count=3480, ) create_reporting_model( FansByArtistAccountGenderCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, gender=Gender.MALE, country_iso2=country_us, fans_count=100, ) create_reporting_model( FansByArtistAccountGenderCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, gender=Gender.FEMALE, country_iso2=country_us, fans_count=200, ) create_reporting_model( FansByArtistAccountGenderCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, gender=Gender.MALE, country_iso2=country_ee, fans_count=50, ) response = handler.handle( GetArtistFansShareByGenderRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=[country_us, country_ee], ), ) assert response.available_fans_share == decimal.Decimal("0.132") assert response.items == [ FansShareByGender( gender=Gender.MALE, fans_share=decimal.Decimal("0.429"), ), FansShareByGender( gender=Gender.FEMALE, fans_share=decimal.Decimal("0.571"), ), ] @pytest.mark.db def test_get_artist_fans_share_by_gender__without_country_empty( self, handler: GetArtistFansShareByGenderHandler, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() response = handler.handle( GetArtistFansShareByGenderRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=None, ), ) assert response.available_fans_share == 0 assert response.items == [] @pytest.mark.db def test_get_artist_fans_share_by_gender_with_country_empty( self, handler: GetArtistFansShareByGenderHandler, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() response = handler.handle( GetArtistFansShareByGenderRequest( 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 TestGetArtistGlobalFansShareByGenderHandler: @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_share_by_gender_without_country( self, handler: GetArtistFansShareByGenderHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() fans_data_availability = create_reporting_model( GlobalFansDataAvailabilityGenderByArtistDbt, global_participant_id=global_participant_id, ) fans_share_1 = create_reporting_model( GlobalFansByArtistGenderDbt, global_participant_id=global_participant_id, gender=Gender.MALE, fans_share=decimal.Decimal("0.1"), ) fans_share_2 = create_reporting_model( GlobalFansByArtistGenderDbt, global_participant_id=global_participant_id, gender=Gender.FEMALE, fans_share=decimal.Decimal("0.5"), ) fans_share_3 = create_reporting_model( GlobalFansByArtistGenderDbt, global_participant_id=global_participant_id, gender=Gender.PREFER_NOT_TO_ANSWER, fans_share=decimal.Decimal("0.1"), ) response = handler.handle( GetArtistFansShareByGenderRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, countries=None, ), ) assert ( response.available_fans_share == fans_data_availability.available_fans_share ) assert response.items == [ FansShareByGender( gender=fans_share_1.gender, fans_share=fans_share_1.fans_share, ), FansShareByGender( gender=fans_share_2.gender, fans_share=fans_share_2.fans_share, ), FansShareByGender( gender=fans_share_3.gender, fans_share=fans_share_3.fans_share, ), ] @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_share_by_gender_with_country( self, handler: GetArtistFansShareByGenderHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() country_us = "US" country_ee = "EE" create_reporting_model( GlobalFansDataAvailabilityGenderByArtistCountryDbt, global_participant_id=global_participant_id, country_iso2=country_us, available_fans_count=100, total_fans_count=317, ) create_reporting_model( GlobalFansDataAvailabilityGenderByArtistCountryDbt, global_participant_id=global_participant_id, country_iso2=country_ee, available_fans_count=400, total_fans_count=3480, ) create_reporting_model( GlobalFansByArtistGenderCountryDbt, global_participant_id=global_participant_id, gender=Gender.MALE, country_iso2=country_us, fans_count=100, ) create_reporting_model( GlobalFansByArtistGenderCountryDbt, global_participant_id=global_participant_id, gender=Gender.FEMALE, country_iso2=country_us, fans_count=200, ) create_reporting_model( GlobalFansByArtistGenderCountryDbt, global_participant_id=global_participant_id, gender=Gender.MALE, country_iso2=country_ee, fans_count=50, ) response = handler.handle( GetArtistFansShareByGenderRequest( identity_id=identity_id, vendor_id=None, subaccount_id=None, global_participant_id=global_participant_id, countries=[country_us, country_ee], ), ) assert response.available_fans_share == decimal.Decimal("0.132") assert response.items == [ FansShareByGender( gender=Gender.MALE, fans_share=decimal.Decimal("0.429"), ), FansShareByGender( gender=Gender.FEMALE, fans_share=decimal.Decimal("0.571"), ), ]