"""Account logic tests.""" from unittest.mock import patch from moneyhub.constants.constants import OrderDirection from moneyhub.logic import account as logic from tests.utils.factories import CombinedPaymentsFactory from tests.utils.factories import ContractAdvanceFactory from tests.utils.factories import ExpensesFactory from tests.utils.factories import LedgerReserveReleaseScheduleFactory from tests.utils.factories import LedgerVatSummaryFactory from tests.utils.factories import RevenueByActivityMonthFactory from tests.utils.factories import RevenueByArtistFactory from tests.utils.factories import RevenueByImprintFactory from tests.utils.factories import RevenueByStoreFactory from tests.utils.factories import RevenueByTransactionTypeFactory from tests.utils.factories import RevenueDistro from tests.utils.factories import RevenueNr from tests.utils.factories import StatementPeriodFactory from tests.utils.factories import VendorFactory from tests.utils.factories import WorksheetAdjustmentFactory def _map_result_fields(item, fields): """Extract specified fields from an item.""" return {field: item[field] for field in fields} @patch('moneyhub.logic.account.models') def test_get_statement_periods_by_account( mock_models): """Test getting account statement periods.""" account_id = 24601 contract_id = 10001 statement_period_1 = StatementPeriodFactory.build( statement_period_id=101 ) statement_period_2 = StatementPeriodFactory.build( statement_period_id=102 ) statement_period_3 = StatementPeriodFactory.build( statement_period_id=103 ) mock_models.AccountStatementPeriods.get_by_account_id.return_value = ([ statement_period_1, statement_period_2, statement_period_3 ], 3) mock_models.Vendor.get_by_account_id.return_value =\ VendorFactory.build(vendor_id=account_id) result = logic.get_statement_periods_by_account(account_id, contract_id) assert result == [statement_period_1, statement_period_2, statement_period_3] mock_models.AccountStatementPeriods.get_by_account_id.assert_called_once_with( account_id, contract_id, None, None, OrderDirection.ASC ) @patch('moneyhub.logic.account.models') def test_get_account_activity(mock_models): """Test get account activity.""" account_id = 24601 mock_models.CombinedAdvances.get_account_advance_activity.return_value =\ ContractAdvanceFactory.build() mock_models.CombinedAdjustments.get_account_adjustments_activity.return_value =\ WorksheetAdjustmentFactory.build(account_id=account_id) mock_models.CombinedAdjustments.get_publishing_revenue_activity.return_value =\ WorksheetAdjustmentFactory.build(account_id=account_id) mock_models.Expenses.get_account_adjustment_detail_activity.return_value =\ ExpensesFactory.build(account_id=account_id) mock_models.LedgerReserveReleaseSchedule.get_account_reserves_activity.return_value =\ LedgerReserveReleaseScheduleFactory.build(account_id=account_id) mock_models.RevenueDistro.get_by_account_id.return_value = None mock_models.RevenueNr.get_by_account_id.return_value = RevenueNr.build() mock_models.LedgerVatSummary.get_activity_by_account_id.return_value = \ LedgerVatSummaryFactory.build() mock_models.CombinedPayments.get_withholding_tax_payment_by_account.return_value = \ CombinedPaymentsFactory.build() mock_models.LedgerSummary.check_account_mechanicals.return_value = True result = logic.get_account_activity(account_id, None, False) assert result == { 'adjustments': True, 'advances': True, 'expenses': True, 'mechanicals': True, 'physical_reserves': True, 'publishing_revenue': True, 'revenue': True, 'taxes': True } mock_models.RevenueDistro.get_by_account_id.assert_called() mock_models.RevenueNr.get_by_account_id.assert_called() @patch('moneyhub.logic.account.models') def test_get_account_activity_with_subaccount(mock_models): """Test get account activity.""" account_id = 24601 subaccount_id = 1 is_subaccount = True mock_models.CombinedAdvances.get_account_advance_activity.return_value =\ ContractAdvanceFactory.build() mock_models.CombinedAdjustments.get_account_adjustments_activity.return_value =\ WorksheetAdjustmentFactory.build(account_id=account_id) mock_models.Expenses.get_account_adjustment_detail_activity.return_value =\ ExpensesFactory.build(account_id=account_id) mock_models.LedgerReserveReleaseSchedule.get_account_reserves_activity.return_value =\ LedgerReserveReleaseScheduleFactory.build(account_id=account_id) mock_models.RevenueDistro.get_by_account_id.return_value = RevenueDistro.build() mock_models.RevenueNr.get_by_account_id.return_value = None mock_models.LedgerVatSummary.get_activity_by_account_id.return_value = \ LedgerVatSummaryFactory.build() mock_models.CombinedPayments.get_withholding_tax_payment_by_account.return_value = None mock_models.LedgerSummary.check_account_mechanicals.return_value = True result = logic.get_account_activity(account_id, subaccount_id, is_subaccount) assert result == { 'adjustments': True, 'advances': True, 'expenses': True, 'mechanicals': True, 'physical_reserves': True, 'publishing_revenue': False, 'revenue': True, 'taxes': True } mock_models.RevenueDistro.get_by_account_id.assert_called_with( account_id, subaccount_id, is_subaccount) mock_models.RevenueNr.get_by_account_id.assert_called() mock_models.CombinedAdjustments.get_publishing_revenue_activity.assert_not_called() @patch('moneyhub.logic.account.models') def test_get_account_activity_distro_revenue(mock_models): """Test get account activity.""" account_id = 24601 mock_models.CombinedAdvances.get_account_advance_activity.return_value =\ ContractAdvanceFactory.build() mock_models.CombinedAdjustments.get_account_adjustments_activity.return_value =\ WorksheetAdjustmentFactory.build(account_id=account_id) mock_models.CombinedAdjustments.get_publishing_revenue_activity.return_value = None mock_models.Expenses.get_account_adjustment_detail_activity.return_value =\ ExpensesFactory.build(account_id=account_id) mock_models.LedgerReserveReleaseSchedule.get_account_reserves_activity.return_value =\ LedgerReserveReleaseScheduleFactory.build(account_id=account_id) mock_models.RevenueDistro.get_by_account_id.return_value = RevenueDistro.build() mock_models.RevenueNr.get_by_account_id.return_value = None mock_models.LedgerVatSummary.get_activity_by_account_id.return_value = None mock_models.CombinedPayments.get_withholding_tax_payment_by_account.return_value =\ CombinedPaymentsFactory.build() mock_models.LedgerSummary.check_account_mechanicals.return_value = True result = logic.get_account_activity(account_id, None, False) assert result == { 'adjustments': True, 'advances': True, 'expenses': True, 'mechanicals': True, 'physical_reserves': True, 'publishing_revenue': False, 'revenue': True, 'taxes': True } mock_models.RevenueDistro.get_by_account_id.assert_called() mock_models.RevenueNr.get_by_account_id.assert_called() @patch('moneyhub.logic.account.models') def test_get_account_activity_publishing_revenue(mock_models): """Test get account activity when account has publishing revenue (adjustment type ID 2).""" account_id = 24601 mock_models.CombinedAdvances.get_account_advance_activity.return_value = None mock_models.CombinedAdjustments.get_account_adjustments_activity.return_value =\ WorksheetAdjustmentFactory.build(account_id=account_id) mock_models.CombinedAdjustments.get_publishing_revenue_activity.return_value =\ WorksheetAdjustmentFactory.build(account_id=account_id) mock_models.Expenses.get_account_adjustment_detail_activity.return_value = None mock_models.LedgerReserveReleaseSchedule.get_account_reserves_activity.return_value = None mock_models.RevenueDistro.get_by_account_id.return_value = None mock_models.RevenueNr.get_by_account_id.return_value = None mock_models.LedgerVatSummary.get_activity_by_account_id.return_value = None mock_models.CombinedPayments.get_withholding_tax_payment_by_account.return_value = None mock_models.LedgerSummary.check_account_mechanicals.return_value = False result = logic.get_account_activity(account_id, None, False) assert result == { 'adjustments': True, 'advances': False, 'expenses': False, 'mechanicals': False, 'physical_reserves': False, 'publishing_revenue': True, 'revenue': False, 'taxes': False } mock_models.CombinedAdjustments.get_publishing_revenue_activity.assert_called_once_with( account_id) @patch('moneyhub.logic.account.models') def test_get_account_activity_no_data(mock_models): """Test get account activity when there is no data.""" mock_models.CombinedAdvances.get_account_advance_activity.return_value = None mock_models.CombinedAdjustments.get_account_adjustments_activity.return_value = None mock_models.CombinedAdjustments.get_publishing_revenue_activity.return_value = None mock_models.Expenses.get_account_adjustment_detail_activity.return_value = None mock_models.LedgerReserveReleaseSchedule.get_account_reserves_activity.return_value = None mock_models.Vendor.get_by_account_id.return_value = VendorFactory.build() mock_models.RevenueDistro.get_by_account_id.return_value = None mock_models.RevenueNr.get_by_account_id.return_value = None mock_models.LedgerVatSummary.get_activity_by_account_id.return_value = None mock_models.CombinedPayments.get_withholding_tax_payment_by_account.return_value = None mock_models.LedgerSummary.check_account_mechanicals.return_value = False result = logic.get_account_activity(24601, None, False) assert result == { 'adjustments': False, 'advances': False, 'expenses': False, 'mechanicals': False, 'physical_reserves': False, 'publishing_revenue': False, 'revenue': False, 'taxes': False } mock_models.RevenueDistro.get_by_account_id.assert_called() mock_models.RevenueNr.get_by_account_id.assert_called() @patch('moneyhub.logic.account.models.RevenueByActivityMonth') def test_get_activity_periods_by_account(mock_RevenueByActivityMonth): """Test getting activity periods by account.""" account_id = 24601 subaccount_id = 10001 activity_period_ids = [1, 999] expected = [ RevenueByActivityMonthFactory.build( activity_period_id=1, activity_month_name='First', ), RevenueByActivityMonthFactory.build( activity_period_id=999, activity_month_name='Last', ) ] mock_RevenueByActivityMonth.get_activity_periods_by_account_id.return_value = expected result = logic.get_activity_periods_by_account(account_id, subaccount_id, activity_period_ids) assert result == expected mock_RevenueByActivityMonth.get_activity_periods_by_account_id.assert_called_once_with( account_id, subaccount_id, activity_period_ids) @patch('moneyhub.logic.account.models') def test_get_contracts_info(mock_models): """Test get account activity.""" account_id = 123 contract_ids = [1] mock_models.Contracts.get_by_account_id.return_value = [ { 'account_id': 123, 'contract_id': 1, 'contract_name': 'Contract 1' } ] result = logic.get_contracts_info(account_id, contract_ids) assert result == [ { 'account_id': 123, 'contract_id': 1, 'contract_name': 'Contract 1' } ] @patch('moneyhub.logic.account.models.RevenueByImprint') def test_get_imprints_by_account_id(mock_RevenueByImprint): """Test getting imprints by account ID.""" account_id = 24601 limit = 50 search_term = 'test' subaccount_id = None imprint_ids = [3, 8] mock_model1 = RevenueByImprintFactory.build( imprintid=3, imprint='Sumerian Records', account_id=24601, subaccount_id=10001 ) mock_model2 = RevenueByImprintFactory.build( imprintid=8, imprint='Dine Alone Records', account_id=24601, subaccount_id=None ) mock_RevenueByImprint.get_imprints_by_account_id.return_value = [ mock_model1.to_dict(), mock_model2.to_dict()] expected = [ { 'imprintid': 3, 'imprint': 'Sumerian Records', 'account_id': 24601, 'subaccount_id': 10001 }, { 'imprintid': 8, 'imprint': 'Dine Alone Records', 'account_id': 24601, 'subaccount_id': None } ] result = logic.get_imprints_by_account_id( account_id, limit, search_term, subaccount_id, imprint_ids) fields = ['imprintid', 'imprint', 'account_id', 'subaccount_id'] result = [_map_result_fields(item, fields) for item in result] assert result == expected mock_RevenueByImprint.get_imprints_by_account_id.assert_called_once_with( account_id, limit, search_term, subaccount_id, imprint_ids) @patch('moneyhub.logic.account.models.RevenueByArtist') def test_get_artists_by_account_id(mock_RevenueByArtist): """Test getting artists by account ID.""" account_id = 24601 mock_model1 = RevenueByArtistFactory.build( artist_id=123, artist_name='Taylor Swift' ) mock_model2 = RevenueByArtistFactory.build( artist_id=456, artist_name='Ed Sheeran' ) mock_RevenueByArtist.get_artists_by_account_id.return_value = [ mock_model1.to_dict(), mock_model2.to_dict()] expected = [ { 'artist_id': 123, 'artist_name': 'Taylor Swift' }, { 'artist_id': 456, 'artist_name': 'Ed Sheeran' } ] result = logic.get_artists_by_account_id(account_id, 50, None, None) fields = ['artist_id', 'artist_name'] result = [_map_result_fields(item, fields) for item in result] assert result == expected mock_RevenueByArtist.get_artists_by_account_id.assert_called_once_with( account_id, 50, None, None ) @patch('moneyhub.logic.account.models.RevenueByTrack') def test_get_tracks_by_account_id(mock_RevenueByTrack): """Test getting tracks by account ID.""" account_id = 24601 expected = [ {'track_unique_id': 1001, 'track_name': 'Bohemian Rhapsody', 'isrc': 'GBUM71029604'}, {'track_unique_id': 1002, 'track_name': 'Hotel California', 'isrc': None}, ] mock_RevenueByTrack.get_tracks_by_account_id.return_value = expected result = logic.get_tracks_by_account_id(account_id, 50, None, None) assert result == expected mock_RevenueByTrack.get_tracks_by_account_id.assert_called_once_with( account_id, 50, None, None ) @patch('moneyhub.logic.account.models.RevenueByStore') def test_get_stores_by_account_id(mock_RevenueByStore): """Test to get stores by account id.""" mock_model1 = RevenueByStoreFactory.build( store_id=123, store_name='Spotify') mock_model2 = RevenueByStoreFactory.build( store_id=456, store_name='Deezer') expected = [ { 'store_id': 123, 'store_name': 'Spotify' }, { 'store_id': 456, 'store_name': 'Deezer' }, ] mock_RevenueByStore.get_stores_by_account_id.return_value = [ mock_model1.to_dict(), mock_model2.to_dict()] result = logic.get_stores_by_account_id(123456, None) def _map_result(item): return { 'store_id': item['store_id'], 'store_name': item['store_name'], } result = list(map(_map_result, result)) assert result == expected @patch('moneyhub.logic.account.models') def test_get_transaction_types_by_account(mock_models): """Test getting transaction types by account.""" account_id = 24601 item1 = RevenueByTransactionTypeFactory.build( transaction_type_id=1, transaction_type_desc='Downloaded Ringtones', transaction_type_group_name='Ringtone Group', ) item2 = RevenueByTransactionTypeFactory.build( transaction_type_id=2, transaction_type_desc='Downloaded Ringtones UGC', transaction_type_group_name='Ringtone Group' ) items = [item1.to_dict(), item2.to_dict()] mock_models.RevenueByTransactionType.get_transaction_types_by_account_id.return_value = items expected = [ { 'transaction_type_id': 1, 'transaction_type_desc': 'Downloaded Ringtones', 'transaction_type_group_name': 'Ringtone Group', }, { 'transaction_type_id': 2, 'transaction_type_desc': 'Downloaded Ringtones UGC', 'transaction_type_group_name': 'Ringtone Group', }, ] result = logic.get_transaction_types_by_account_id(account_id, None) def _map_result(item): return { 'transaction_type_id': item['transaction_type_id'], 'transaction_type_desc': item['transaction_type_desc'], 'transaction_type_group_name': item['transaction_type_group_name'], } result = list(map(_map_result, result)) assert result == expected @patch('moneyhub.logic.account.models.RevenueByProject') def test_get_projects_by_account_id(mock_RevenueByProject): """Test getting projects by account ID.""" account_id = 24601 mock_projects = [ {'project_id': 501}, {'project_id': 502} ] mock_RevenueByProject.get_projects_by_account_id.return_value = mock_projects result = logic.get_projects_by_account_id(account_id, 50, None, None) assert result == mock_projects mock_RevenueByProject.get_projects_by_account_id.assert_called_once_with( account_id, 50, None, None ) @patch('moneyhub.logic.account.models.RevenueByProductDistro') def test_get_products_by_account_id(mock_RevenueByProductDistro): """Test getting products by account ID.""" account_id = 24601 mock_products = [ {'product_id': 1234, 'product_name': 'Best Name', 'display_upc': '1111111111111'}, {'product_id': 1235, 'product_name': 'Push Sky', 'display_upc': '1111111111112'}, ] mock_RevenueByProductDistro.get_products_by_account_id.return_value = mock_products result = logic.get_products_by_account_id(account_id, 50, None, None) assert result == mock_products mock_RevenueByProductDistro.get_products_by_account_id.assert_called_once_with( account_id, 50, None, None ) @patch('moneyhub.logic.account.models') def test_get_recording_names_distro(mock_models): """Test get recording names for a distro account.""" account_id = 24601 recording_ids = ['41497259', '99999999'] mock_models.Contract.get_contract_type_by_account.return_value.contract_type = \ 'distribution' mock_models.DimTrack.get_recording_names_by_ids.return_value = [ { 'recording_id': '41497259', 'recording_title': 'Distro Track' }, { 'recording_id': '99999999', 'recording_title': 'Another Distro Track' } ] result = logic.get_recording_names(account_id, recording_ids) assert result == [ { 'recording_id': '41497259', 'recording_title': 'Distro Track' }, { 'recording_id': '99999999', 'recording_title': 'Another Distro Track' } ] mock_models.Contract.get_contract_type_by_account.assert_called_once_with( account_id ) mock_models.DimTrack.get_recording_names_by_ids.assert_called_once_with( recording_ids ) mock_models.PerformanceNrSoundRecording.get_recording_names_by_ids.assert_not_called() @patch('moneyhub.logic.account.models') def test_get_recording_names_nr(mock_models): """Test get recording names for an NR account.""" account_id = 24601 recording_ids = ['aaa-bbb-ccc-001'] mock_models.Contract.get_contract_type_by_account.return_value.contract_type = \ 'neighbouring_rights' mock_models.PerformanceNrSoundRecording.get_recording_names_by_ids.return_value = [ { 'recording_id': 'aaa-bbb-ccc-001', 'recording_title': 'NR Track' } ] result = logic.get_recording_names(account_id, recording_ids) assert result == [ { 'recording_id': 'aaa-bbb-ccc-001', 'recording_title': 'NR Track' } ] mock_models.Contract.get_contract_type_by_account.assert_called_once_with( account_id ) mock_models.PerformanceNrSoundRecording.get_recording_names_by_ids.assert_called_once_with( recording_ids ) mock_models.DimTrack.get_recording_names_by_ids.assert_not_called() @patch('moneyhub.logic.account.models') def test_get_artist_by_ids(mock_models): """Test getting artist names by IDs.""" artist_ids = [123, 456] expected = [ { 'artist_id': 123, 'artist_name': 'Taylor Swift' }, { 'artist_id': 456, 'artist_name': 'Ed Sheeran' } ] mock_models.RevenueByArtist.get_artist_names_by_ids.return_value = expected result = logic.get_artists_by_ids(artist_ids) assert result == expected mock_models.RevenueByArtist.get_artist_names_by_ids.assert_called_once_with(artist_ids) @patch('moneyhub.logic.account.models') def test_get_recordings_by_account_id_nr(mock_models): """Test get recordings by account ID for an NR account.""" account_id = 24601 mock_models.Contract.get_contract_type_by_account.return_value.contract_type = \ 'neighbouring_rights' mock_recordings = [ {'recording_id': 'aaa-bbb-001', 'recording_title': 'NR Track One'}, {'recording_id': 'aaa-bbb-002', 'recording_title': 'NR Track Two'}, ] mock_models.RevenueByRecordingNr.get_recordings_by_account_id.return_value = mock_recordings result = logic.get_recordings_by_account_id(account_id, 50, None) assert result == mock_recordings mock_models.Contract.get_contract_type_by_account.assert_called_once_with(account_id) mock_models.RevenueByRecordingNr.get_recordings_by_account_id.assert_called_once_with( account_id, 50, None, None ) mock_models.RevenueByRecordingDistro.get_recordings_by_account_id.assert_not_called() @patch('moneyhub.logic.account.models') def test_get_recordings_by_account_id_distro(mock_models): """Test get recordings by account ID for a distro account.""" account_id = 24601 mock_models.Contract.get_contract_type_by_account.return_value.contract_type = \ 'distribution' mock_recordings = [ {'recording_id': '37736998', 'recording_title': 'Thrift Shop'}, {'recording_id': '37736999', 'recording_title': "Can't Hold Us"}, ] mock_models.RevenueByRecordingDistro.get_recordings_by_account_id.return_value = \ mock_recordings result = logic.get_recordings_by_account_id(account_id, 50, None) assert result == mock_recordings mock_models.Contract.get_contract_type_by_account.assert_called_once_with(account_id) mock_models.RevenueByRecordingDistro.get_recordings_by_account_id.assert_called_once_with( account_id, 50, None, None ) mock_models.RevenueByRecordingNr.get_recordings_by_account_id.assert_not_called() @patch('moneyhub.logic.account.models') def test_get_recordings_by_account_id_with_search(mock_models): """Test get recordings by account ID with a search term.""" account_id = 24601 search_term = 'Thrift' mock_models.Contract.get_contract_type_by_account.return_value.contract_type = \ 'distribution' mock_recordings = [{'recording_id': '37736998', 'recording_title': 'Thrift Shop'}] mock_models.RevenueByRecordingDistro.get_recordings_by_account_id.return_value = \ mock_recordings result = logic.get_recordings_by_account_id(account_id, 50, search_term) assert result == mock_recordings mock_models.RevenueByRecordingDistro.get_recordings_by_account_id.assert_called_once_with( account_id, 50, search_term, None ) @patch('moneyhub.logic.account.models') def test_get_recordings_by_account_id_with_contract_id(mock_models): """Test get recordings by account ID with a contract id filter.""" account_id = 24601 contract_id = 535866 mock_models.Contract.get_contract_type_by_account.return_value.contract_type = \ 'distribution' mock_recordings = [{'recording_id': '37736998', 'recording_title': 'Thrift Shop'}] mock_models.RevenueByRecordingDistro.get_recordings_by_account_id.return_value = \ mock_recordings result = logic.get_recordings_by_account_id(account_id, 50, None, contract_id) assert result == mock_recordings mock_models.RevenueByRecordingDistro.get_recordings_by_account_id.assert_called_once_with( account_id, 50, None, contract_id )