import decimal import pytest from dmp.artists.dtos import FansShareByHeavyRotation from dmp.artists.handlers import ( GetArtistFansShareByHeavyRotationHandler, GetArtistFansShareByHeavyRotationRequest, ) from dmp.fandata.enums import HeavyRotation from dmp.fandata.models import ( FansByArtistAccountHeavyRotationCountryDbt, FansByArtistAccountHeavyRotationDbt, FansPresenceHeavyRotationByArtistAccountCountryDbt, FansPresenceHeavyRotationByArtistAccountDbt, GlobalFansByArtistHeavyRotationCountryDbt, GlobalFansByArtistHeavyRotationDbt, GlobalFansPresenceHeavyRotationByArtistCountryDbt, GlobalFansPresenceHeavyRotationByArtistDbt, ) from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel class TestGetArtistAccountFansShareByHeavyRotationHandler: @pytest.mark.db def test_get_artist_fans_share_by_heavy_rotation_without_country( self, handler: GetArtistFansShareByHeavyRotationHandler, 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( FansPresenceHeavyRotationByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) fans_share = create_reporting_model( FansByArtistAccountHeavyRotationDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.1"), ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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 == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.NOT_IN, fans_share=1 - fans_share.fans_share, ), FansShareByHeavyRotation( heavy_rotation=HeavyRotation.IN, fans_share=fans_share.fans_share, ), ] @pytest.mark.db def test_get_artist_fans_share_by_heavy_rotation_all_fans_in( self, handler: GetArtistFansShareByHeavyRotationHandler, 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( FansPresenceHeavyRotationByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) create_reporting_model( FansByArtistAccountHeavyRotationDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, fans_share=decimal.Decimal("1.000"), ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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 == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.IN, fans_share=decimal.Decimal("1.000") ), ] @pytest.mark.db def test_get_artist_fans_share_by_heavy_rotation_all_fans_not_in( self, handler: GetArtistFansShareByHeavyRotationHandler, 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( FansPresenceHeavyRotationByArtistAccountDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) create_reporting_model( FansByArtistAccountHeavyRotationDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.000"), ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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 == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.NOT_IN, fans_share=decimal.Decimal(1) ), ] @pytest.mark.db def test_get_artist_fans_share_by_heavy_rotation_with_country( self, handler: GetArtistFansShareByHeavyRotationHandler, 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" country_br = "BR" create_reporting_model( FansPresenceHeavyRotationByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_us, available_fans_count=133, total_fans_count=500, ) create_reporting_model( FansPresenceHeavyRotationByArtistAccountCountryDbt, 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=2500, ) create_reporting_model( FansPresenceHeavyRotationByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_br, available_fans_count=300, total_fans_count=1500, ) create_reporting_model( FansByArtistAccountHeavyRotationCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_us, fans_count=100, total_fans_count=800, ) create_reporting_model( FansByArtistAccountHeavyRotationCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_ee, fans_count=50, total_fans_count=200, ) create_reporting_model( FansByArtistAccountHeavyRotationCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_br, fans_count=200, total_fans_count=700, ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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.178") assert response.items == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.NOT_IN, fans_share=decimal.Decimal("0.850"), ), FansShareByHeavyRotation( heavy_rotation=HeavyRotation.IN, fans_share=decimal.Decimal("0.150"), ), ] @pytest.mark.db def test_get_artist_fans_share_by_heavy_rotation_with_country_all_fans_in( self, handler: GetArtistFansShareByHeavyRotationHandler, 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" create_reporting_model( FansPresenceHeavyRotationByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_us, available_fans_count=133, total_fans_count=500, ) create_reporting_model( FansByArtistAccountHeavyRotationCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_us, fans_count=10, total_fans_count=10, ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=[country_us], ), ) assert response.available_fans_share == decimal.Decimal("0.266") assert response.items == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.IN, fans_share=decimal.Decimal("1.000"), ), ] @pytest.mark.db def test_get_artist_fans_share_by_heavy_rotation_with_country_all_fans_not_in( self, handler: GetArtistFansShareByHeavyRotationHandler, 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" create_reporting_model( FansPresenceHeavyRotationByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_us, available_fans_count=133, total_fans_count=500, ) create_reporting_model( FansByArtistAccountHeavyRotationCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2=country_us, fans_count=0, total_fans_count=10, ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, countries=[country_us], ), ) assert response.available_fans_share == decimal.Decimal("0.266") assert response.items == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.NOT_IN, fans_share=decimal.Decimal("1.000"), ), ] @pytest.mark.db def test_get_artist_fans_share_heavy_rotation_without_country_empty( self, handler: GetArtistFansShareByHeavyRotationHandler, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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_heavy_rotation_with_country_empty( self, handler: GetArtistFansShareByHeavyRotationHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = fake.integer() global_participant_id = fake.uuid4_string() create_reporting_model( FansPresenceHeavyRotationByArtistAccountCountryDbt, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, country_iso2="BR", ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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 TestGetArtistGlobalFansShareByHeavyRotationHandler: @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_share_by_heavy_rotation_without_country( self, handler: GetArtistFansShareByHeavyRotationHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() fans_data_availability = create_reporting_model( GlobalFansPresenceHeavyRotationByArtistDbt, global_participant_id=global_participant_id, ) fans_share = create_reporting_model( GlobalFansByArtistHeavyRotationDbt, global_participant_id=global_participant_id, fans_share=decimal.Decimal("0.1"), ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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 == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.NOT_IN, fans_share=1 - fans_share.fans_share, ), FansShareByHeavyRotation( heavy_rotation=HeavyRotation.IN, fans_share=fans_share.fans_share, ), ] @pytest.mark.db @pytest.mark.artist_access(is_global=True) def test_get_artist_fans_share_by_heavy_rotation_with_country( self, handler: GetArtistFansShareByHeavyRotationHandler, create_reporting_model: CreateReportingModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() country_us = "US" country_ee = "EE" country_br = "BR" create_reporting_model( GlobalFansPresenceHeavyRotationByArtistCountryDbt, global_participant_id=global_participant_id, country_iso2=country_us, available_fans_count=133, total_fans_count=500, ) create_reporting_model( GlobalFansPresenceHeavyRotationByArtistCountryDbt, global_participant_id=global_participant_id, country_iso2=country_ee, available_fans_count=400, total_fans_count=2500, ) create_reporting_model( GlobalFansPresenceHeavyRotationByArtistCountryDbt, global_participant_id=global_participant_id, country_iso2=country_br, available_fans_count=300, total_fans_count=1500, ) create_reporting_model( GlobalFansByArtistHeavyRotationCountryDbt, global_participant_id=global_participant_id, country_iso2=country_us, fans_count=100, total_fans_count=800, ) create_reporting_model( GlobalFansByArtistHeavyRotationCountryDbt, global_participant_id=global_participant_id, country_iso2=country_ee, fans_count=50, total_fans_count=200, ) create_reporting_model( GlobalFansByArtistHeavyRotationCountryDbt, global_participant_id=global_participant_id, country_iso2=country_br, fans_count=200, total_fans_count=700, ) response = handler.handle( GetArtistFansShareByHeavyRotationRequest( 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.178") assert response.items == [ FansShareByHeavyRotation( heavy_rotation=HeavyRotation.NOT_IN, fans_share=decimal.Decimal("0.850"), ), FansShareByHeavyRotation( heavy_rotation=HeavyRotation.IN, fans_share=decimal.Decimal("0.150"), ), ]