"""Tests for expense handlers.""" from unittest.mock import patch from moneyhub.constants.constants import DistributionType from moneyhub.schemas.ledger_adjustment import BreakdownItemSchema from moneyhub.schemas.ledger_adjustment import LedgerAdjustmentExpenseSchema @patch('moneyhub.handlers.expense.logic') def test_get_expenses_by_account_id( mock_logic, fixture_client ): """Test to get expenses for a specified account.""" account_id = 1 limit = 50 offset = 0 expected_response = { 'items': [ { 'account_id': account_id, 'apply_to_statement_period_id': 286, 'artist_id': None, 'artist_name': None, 'contract_id': None, 'distribution_type': 'digital', 'imprint': None, 'imprint_id': None, 'adjustment_amount_payee_currency': 100.00, 'adjustment_payee_currency_code': 'USD', 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Physical Rework', 'subaccount_id': None, 'subaccount_name': None, 'note': 'mock adjustment', 'upc': '10101010', 'worksheet_adjustment_detail_id': 1, } ], 'pagination': {'pagination_type': 'standard', 'total_records': 1} } mock_logic.get_expenses_by_account_id.return_value = expected_response res = fixture_client.get(f'/expenses/account/{account_id}?limit={limit}&offset={offset}') assert res.status_code == 200 assert res.json() == expected_response mock_logic.get_expenses_by_account_id.assert_called_once_with( account_id, 50, 0, None, None, None, None, None, None, None, None, None) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_by_account_id_with_query_params( mock_logic, fixture_client ): """Test to get expenses for a specified account with query parameters.""" account_id = 1 contract_id = 1001 statement_period_id_start = 280 statement_period_id_end = 290 upc = '10101010' expense_type_id = 1 artist_id = 1 subaccount_id = 7116 expected_response = { 'items': [ { 'account_id': account_id, 'apply_to_statement_period_id': 286, 'artist_id': None, 'artist_name': None, 'contract_id': 1001, 'distribution_type': 'digital', 'imprint': None, 'imprint_id': None, 'adjustment_amount_payee_currency': 100.00, 'adjustment_payee_currency_code': 'USD', 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Physical Rework', 'subaccount_id': 7116, 'subaccount_name': 'Subaccount Name', 'note': 'mock adjustment', 'upc': '10101010', 'worksheet_adjustment_detail_id': 1, } ], 'pagination': {'pagination_type': 'standard', 'total_records': 1} } mock_logic.get_expenses_by_account_id.return_value = expected_response query_params = { 'artist_id': artist_id, 'contract_id': contract_id, 'statement_period_id_start': statement_period_id_start, 'statement_period_id_end': statement_period_id_end, 'upc': upc, 'expense_type_id': expense_type_id, 'distribution_type': DistributionType.DIGITAL.value, 'subaccount_id': subaccount_id, 'limit': 50, 'offset': 0 } request_url = f'/expenses/account/{account_id}?' + '&'.join( [f'{key}={value}' for key, value in query_params.items()]) res = fixture_client.get(request_url) assert res.status_code == 200 assert res.json() == expected_response mock_logic.get_expenses_by_account_id.assert_called_once_with( account_id, 50, 0, contract_id, statement_period_id_start, statement_period_id_end, upc, expense_type_id, DistributionType.DIGITAL, artist_id, subaccount_id, None) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_by_account_id_grouped_by_expense_type_id( mock_logic, fixture_client ): """Test to get expenses for a specified account grouped by expense_type_id.""" account_id = 1 group_by = 'expense_type_id' limit = 50 offset = 0 expected_response = { 'items': [ { 'account_id': account_id, 'apply_to_statement_period_id': None, 'artist_id': None, 'artist_name': None, 'contract_id': None, 'distribution_type': None, 'imprint': None, 'imprint_id': None, 'adjustment_amount_payee_currency': 100.00, 'adjustment_payee_currency_code': 'USD', 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Physical Rework', 'subaccount_id': None, 'subaccount_name': None, 'note': None, 'upc': None, 'worksheet_adjustment_detail_id': None, } ], 'pagination': {'pagination_type': 'standard', 'total_records': 1} } mock_logic.get_expenses_by_account_id.return_value = expected_response res = fixture_client.get(f'/expenses/account/{account_id}?group_by={group_by}&limit={limit}&offset={offset}') # noqa: E501 assert res.status_code == 200 assert res.json() == expected_response mock_logic.get_expenses_by_account_id.assert_called_once_with( account_id, limit, offset, None, None, None, None, None, None, None, None, group_by) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_by_account_id_grouped_by_subaccount_id( mock_logic, fixture_client ): """Test to get expenses for a specified account grouped by subaccount_id.""" account_id = 1 group_by = 'subaccount_id' limit = 50 offset = 0 expected_response = { 'items': [ { 'account_id': account_id, 'apply_to_statement_period_id': None, 'artist_id': None, 'artist_name': None, 'contract_id': None, 'distribution_type': None, 'imprint': None, 'imprint_id': None, 'adjustment_amount_payee_currency': 100.00, 'adjustment_payee_currency_code': 'USD', 'reference_adjustment_type_id': None, 'reference_adjustment_type_name': None, 'subaccount_id': 34752, 'subaccount_name': None, 'note': None, 'upc': None, 'worksheet_adjustment_detail_id': None, } ], 'pagination': {'pagination_type': 'standard', 'total_records': 1} } mock_logic.get_expenses_by_account_id.return_value = expected_response res = fixture_client.get(f'/expenses/account/{account_id}?group_by={group_by}&limit={limit}&offset={offset}') # noqa: E501 assert res.status_code == 200 assert res.json() == expected_response mock_logic.get_expenses_by_account_id.assert_called_once_with( account_id, limit, offset, None, None, None, None, None, None, None, None, group_by) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_types_by_account_id(mock_logic, fixture_client): """Test to get associated expenses types for expenses for a given account.""" account_id = 24601 expected_response = [{ 'reference_adjustment_type_id': 100, 'type_name': 'Mechanicals', 'oa_category_name': 'Mechanicals' }] mock_logic.get_expenses_types_by_account_id.return_value = expected_response response = fixture_client.get(f'/expenses/account/{account_id}/types') assert response.status_code == 200 assert response.json() == expected_response mock_logic.get_expenses_types_by_account_id.assert_called_once_with(account_id) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_artists_by_account_id(mock_logic, fixture_client): """Test to get artists associated with expenses for a given account.""" account_id = 24601 expected_response = [ {'artist_id': 111111, 'artist_name': 'Cool Guy'}, {'artist_id': 222222, 'artist_name': 'Cool Gal'}, {'artist_id': 333333, 'artist_name': 'Cool Person'}, ] mock_logic.get_expenses_artists_by_account_id.return_value = expected_response response = fixture_client.get(f'/expenses/account/{account_id}/artists') assert response.status_code == 200 assert response.json() == expected_response mock_logic.get_expenses_artists_by_account_id.assert_called_once_with(account_id) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_subaccounts_by_account_id(mock_logic, fixture_client): """Test to get subaccounts associated with expenses for a given account.""" account_id = 24601 expected_response = [ { 'account_id': account_id, 'subaccount_id': 7111, 'subaccount_name': 'Another Subaccount Name', }, { 'account_id': account_id, 'subaccount_id': 7116, 'subaccount_name': 'Subaccount Name' } ] mock_logic.get_expenses_subaccounts_by_account_id.return_value = expected_response response = fixture_client.get(f'/expenses/account/{account_id}/subaccounts') assert response.status_code == 200 assert response.json() == expected_response mock_logic.get_expenses_subaccounts_by_account_id.assert_called_once_with(account_id) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_upcs_by_account_id(mock_logic, fixture_client): """Test to get UPCs associated with expenses for a given account.""" account_id = 24601 expected_response = [ {'account_id': 24601, 'upc': '111111', 'distribution_type': DistributionType.DIGITAL}, {'account_id': 24601, 'upc': '222222', 'distribution_type': DistributionType.DIGITAL}, {'account_id': 24601, 'upc': '333333', 'distribution_type': DistributionType.PHYSICAL}, ] mock_logic.get_expenses_upcs_by_account_id.return_value = expected_response response = fixture_client.get(f'/expenses/account/{account_id}/upcs') assert response.status_code == 200 assert response.json() == expected_response mock_logic.get_expenses_upcs_by_account_id.assert_called_once_with(account_id) @patch('moneyhub.handlers.expense.logic') def test_get_expenses_by_account_and_statement_period( mock_logic, fixture_client ): """Test to get aggregate expense info per statement period for an account.""" account_id = 1 statement_period_id = 285 contract_id = 123 expected_response = [ LedgerAdjustmentExpenseSchema( amount=4581.98, currency_code='AUD', breakdown_items=[ BreakdownItemSchema( adjustment_total_payee_currency=4581.98, adjustment_payee_currency_code='AUD', reference_adjustment_type_id=1, reference_adjustment_type_name='Label Earnings' ) ], statement_period_id=statement_period_id ) ] mock_logic.get_expenses_by_account_and_statement_periods \ .return_value = expected_response res = fixture_client.get(f'/expenses/account/{account_id}/statement-period/{statement_period_id}?contract_id={contract_id}') # noqa: E501 assert res.status_code == 200 assert res.json() == expected_response[0].model_dump() mock_logic.get_expenses_by_account_and_statement_periods.assert_called_once_with( account_id, [statement_period_id], contract_id)