"""Tests for ledger_adjustment handlers.""" from collections import namedtuple from decimal import Decimal from unittest.mock import patch from moneyhub.schemas.ledger_adjustment import LedgerAdjustmentAppliedSchema from moneyhub.schemas.ledger_adjustment import LedgerAdjustmentBreakdownItemSchema from moneyhub.schemas.ledger_adjustment import LedgerAdjustmentSchema ExtendedLedgerAdjustment = namedtuple( 'ExtendedLedgerAdjustment', [ 'worksheet_adjustment_id', 'abacus_event_id', 'account_id', 'activity_statement_period_id', 'adjustment_amount', 'adjustment_currency_code', 'adjustment_payee_currency_code', 'adjustment_amount_payee_currency', 'apply_to_statement_period_id', 'contract_id', 'reference_adjustment_type_id', 'reference_adjustment_type_name', 'note', 'upc', 'distribution_type', ] ) @patch('moneyhub.handlers.ledger_adjustment.logic') def test_get_ledger_adjustments_by_account_id( mock_logic, fixture_client ): """Test to get adjustment info per statement period for a specified account.""" account_id = 1 expected_response = [ { 'account_id': account_id, 'activity_statement_period_id': 285, 'adjustment_amount_payee_currency': 1290.99, 'adjustment_payee_currency_code': 'AUD', 'apply_to_statement_period_id': 286, 'contract_id': None, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'note': 'mock adjustment', 'worksheet_adjustment_id': 1, } ] mock_logic.get_by_account_id.return_value = [ LedgerAdjustmentSchema( worksheet_adjustment_id=1, account_id=account_id, activity_statement_period_id=285, adjustment_payee_currency_code='AUD', adjustment_amount_payee_currency=Decimal('1290.99'), apply_to_statement_period_id=286, contract_id=None, reference_adjustment_type_id=1, reference_adjustment_type_name='Label Earnings', note='mock adjustment', ) ] res = fixture_client.get(f'/ledger-adjustments/account/{account_id}') assert res.status_code == 200 assert res.json() == expected_response mock_logic.get_by_account_id.assert_called_once_with( account_id, None, None, None, None) @patch('moneyhub.handlers.ledger_adjustment.logic') def test_get_ledger_adjustments_by_account_and_contract_id( mock_logic, fixture_client ): """Test to get adjustment info per statement period for an account/contract.""" account_id = 1 contract_id = 123 expected_response = [ { 'account_id': account_id, 'activity_statement_period_id': 285, 'adjustment_amount_payee_currency': 1290.99, 'adjustment_payee_currency_code': 'AUD', 'apply_to_statement_period_id': 286, 'contract_id': contract_id, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'note': 'mock adjustment', 'worksheet_adjustment_id': 1 } ] mock_logic.get_by_account_id.return_value = [ LedgerAdjustmentSchema( worksheet_adjustment_id=1, account_id=account_id, activity_statement_period_id=285, adjustment_payee_currency_code='AUD', adjustment_amount_payee_currency=Decimal('1290.99'), apply_to_statement_period_id=286, contract_id=contract_id, reference_adjustment_type_id=1, reference_adjustment_type_name='Label Earnings', note='mock adjustment', ) ] res = fixture_client.get( f'/ledger-adjustments/account/{account_id}?contract_id={contract_id}') assert res.status_code == 200 assert res.json() == expected_response mock_logic.get_by_account_id.assert_called_once_with( account_id, contract_id, None, None, None) @patch('moneyhub.handlers.ledger_adjustment.logic') def test_get_adjustments_by_account_and_statement_period( mock_logic, fixture_client ): """Test to get aggregate adjustment info per statement period for an account.""" account_id = 1 statement_period_id = 245 contract_id = 345 expected_response = [ LedgerAdjustmentAppliedSchema( adjustment_total_payee_currency=4581.98, adjustment_payee_currency_code='AUD', breakdown_items=[ LedgerAdjustmentBreakdownItemSchema( adjustment_total_payee_currency=4581.98, adjustment_payee_currency_code='AUD', contract_id=contract_id, reference_adjustment_type_id=1, reference_adjustment_type_name='Label Earnings' ) ], statement_period_id=statement_period_id ) ] mock_logic.get_adjustments_by_account_and_statement_periods \ .return_value = expected_response res = fixture_client.get(f'/ledger-adjustments/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_adjustments_by_account_and_statement_periods.assert_called_once_with( account_id, contract_id, [statement_period_id]) @patch('moneyhub.handlers.ledger_adjustment.logic') def test_get_get_ledger_adjustments_types_by_account_id(mock_logic, fixture_client): """Test to get associated adjustments types for adjustments for a given account.""" account_id = 24601 expected_response = [{ 'reference_adjustment_type_id': 1, 'type_name': 'VAT tax', 'oa_category_name': None }] mock_logic.get_ledger_adjustments_types_by_account_id.return_value = expected_response response = fixture_client.get(f'/ledger-adjustments/account/{account_id}/types') assert response.status_code == 200 assert response.json() == expected_response mock_logic.get_ledger_adjustments_types_by_account_id.assert_called_once_with(account_id)