from datetime import timedelta from urllib.parse import urljoin import pytest from fansifter_common.auth.account import Account from dmp.config import Settings from dmp.songwhip.dtos import SongwhipCampaignType from dmp.songwhip.handlers import GetCampaignsHandler, GetCampaignsRequest from dmp.songwhip.models import SongwhipCustomPageDbt, SongwhipPresavePageDbt from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel class TestGetCampaignsHandler: @pytest.mark.db def test_returns_empty_when_no_matching_campaigns( self, handler: GetCampaignsHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, ) -> None: create_reporting_model(SongwhipPresavePageDbt) create_reporting_model(SongwhipCustomPageDbt) result = handler.handle( GetCampaignsRequest( identity_id=fake.uuid4_string(), ) ) assert result == [] @pytest.mark.db def test_returns_presave_and_custom_pages_with_correct_types( self, handler: GetCampaignsHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, account: Account, settings: Settings, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) custom = create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, created_at=presave.created_at + timedelta(seconds=1), ) create_reporting_model(SongwhipPresavePageDbt) create_reporting_model(SongwhipCustomPageDbt) result = handler.handle( GetCampaignsRequest( identity_id=fake.uuid4_string(), ) ) assert len(result) == 2 assert result[0].id == custom.id assert result[0].type == SongwhipCampaignType.SONGWHIP_EXCLUSIVE_CONTENT assert result[0].url == urljoin(settings.songwhip_url, custom.path) assert result[1].id == presave.id assert result[1].type == SongwhipCampaignType.SONGWHIP_PRESAVE assert result[1].url == urljoin(settings.songwhip_url, presave.path) @pytest.mark.db def test_filters_by_campaign_ids( self, handler: GetCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) custom = create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) result = handler.handle( GetCampaignsRequest( identity_id=identity_id, campaign_ids=[presave.id, custom.id], ) ) assert len(result) == 2 result_ids = {r.id for r in result} assert result_ids == {presave.id, custom.id} @pytest.mark.db def test_filters_by_global_participant_ids( self, handler: GetCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: global_participant_id = "gp-123" presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, ) custom = create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, ) create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) result = handler.handle( GetCampaignsRequest( identity_id=identity_id, global_participant_ids=[global_participant_id], ) ) assert len(result) == 2 result_ids = {r.id for r in result} assert result_ids == {presave.id, custom.id} @pytest.mark.db def test_filters_by_account( self, handler: GetCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) custom = create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model(SongwhipPresavePageDbt) create_reporting_model(SongwhipCustomPageDbt) result = handler.handle( GetCampaignsRequest( identity_id=identity_id, vendor_id=presave.vendor_id, subaccount_id=presave.subaccount_id, ) ) assert len(result) == 2 result_ids = {r.id for r in result} assert result_ids == {presave.id, custom.id} @pytest.mark.db def test_search_filters_by_name( self, handler: GetCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, name="My Artist Tour", ) custom = create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, name="Artist Exclusive", ) create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, name="Unrelated Page", ) result = handler.handle( GetCampaignsRequest( identity_id=identity_id, search="artist", ) ) assert len(result) == 2 result_ids = {r.id for r in result} assert result_ids == {presave.id, custom.id} @pytest.mark.db def test_respects_limit( self, handler: GetCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: for _ in range(3): create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) result = handler.handle( GetCampaignsRequest( identity_id=identity_id, limit=2, ) ) assert len(result) == 2