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.handlers import GetPresavePagesHandler, GetPresavePagesRequest from dmp.songwhip.models import SongwhipPresavePageDbt from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel class TestGetPresavePagesHandler: @pytest.mark.db def test_get_songwhip_presave_page_empty( self, handler: GetPresavePagesHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, ) -> None: create_reporting_model(SongwhipPresavePageDbt) result = handler.handle( GetPresavePagesRequest( identity_id=fake.uuid4_string(), ) ) assert result == [] @pytest.mark.db def test_get_songwhip_presave_page( self, handler: GetPresavePagesHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, account: Account, settings: Settings, ) -> None: songwhip_presave_page = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model(SongwhipPresavePageDbt) result = handler.handle( GetPresavePagesRequest( identity_id=fake.uuid4_string(), ) ) assert len(result) == 1 songwhip_presave_page_result = result[0] assert songwhip_presave_page_result.id == songwhip_presave_page.id assert songwhip_presave_page_result.name == songwhip_presave_page.name assert songwhip_presave_page_result.url == urljoin( settings.songwhip_url, songwhip_presave_page.path ) assert songwhip_presave_page_result.vendor_id == songwhip_presave_page.vendor_id assert ( songwhip_presave_page_result.subaccount_id == songwhip_presave_page.subaccount_id ) assert ( songwhip_presave_page_result.global_participant_id == songwhip_presave_page.global_participant_id ) assert ( songwhip_presave_page_result.created_at == songwhip_presave_page.created_at ) @pytest.mark.db def test_get_songwhip_presave_page_ids_filter( self, handler: GetPresavePagesHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: songwhip_presave_page_1 = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) songwhip_presave_page_2 = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, created_at=songwhip_presave_page_1.created_at + timedelta(seconds=1), ) create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) result = handler.handle( GetPresavePagesRequest( identity_id=identity_id, presave_page_ids=[ songwhip_presave_page_1.id, songwhip_presave_page_2.id, ], ) ) assert len(result) == 2 assert result[0].id == songwhip_presave_page_2.id assert result[1].id == songwhip_presave_page_1.id @pytest.mark.db def test_get_songwhip_presave_page_global_participant_ids_filter( self, handler: GetPresavePagesHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, settings: Settings, ) -> None: songwhip_presave_page = create_reporting_model( SongwhipPresavePageDbt, 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( GetPresavePagesRequest( identity_id=identity_id, global_participant_ids=[songwhip_presave_page.global_participant_id], ) ) assert len(result) == 1 songwhip_presave_page_result = result[0] assert songwhip_presave_page_result.id == songwhip_presave_page.id assert songwhip_presave_page_result.name == songwhip_presave_page.name assert songwhip_presave_page_result.url == urljoin( settings.songwhip_url, songwhip_presave_page.path ) assert songwhip_presave_page_result.vendor_id == songwhip_presave_page.vendor_id assert ( songwhip_presave_page_result.subaccount_id == songwhip_presave_page.subaccount_id ) assert ( songwhip_presave_page_result.global_participant_id == songwhip_presave_page.global_participant_id ) assert ( songwhip_presave_page_result.created_at == songwhip_presave_page.created_at ) @pytest.mark.db def test_get_songwhip_presave_page_account_filter( self, handler: GetPresavePagesHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: songwhip_presave_page = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model( SongwhipPresavePageDbt, ) result = handler.handle( GetPresavePagesRequest( identity_id=identity_id, vendor_id=songwhip_presave_page.vendor_id, subaccount_id=songwhip_presave_page.subaccount_id, ) ) assert len(result) == 1 songwhip_presave_page_result = result[0] assert songwhip_presave_page_result.id == songwhip_presave_page.id