"""Unit tests for Ledger Adjustment Applied model.""" from decimal import Decimal from ledger.models.ledger_adjustment_applied import LedgerAdjustmentApplied from ledger.models.ledger_adjustment_detail_applied import LedgerAdjustmentDetailApplied from tests.conftest import mock_worksheet_adjustment, mock_worksheet_adjustment_detail from tests.utils.factories import LedgerAdjustmentDetailFactory, LedgerAdjustmentFactory def test_create_adjustment_applied(mock_event_fixtures): """Test to create a Ledger Adjustment Applied.""" ledger_adjustment = LedgerAdjustmentFactory.create() assert len(LedgerAdjustmentApplied.query.all()) == 0 for i in range(3): mock_worksheet_adjustment(worksheet_adjustment_id=i + 2) LedgerAdjustmentApplied.create( abacus_event_id=1, account_id=1, contract_id=1, statement_period_id=1, ledger_adjustment_id=ledger_adjustment.ledger_adjustment_id, worksheet_adjustment_id=i + 2, adjustment_currency_code='USD', adjustment_amount=Decimal('500'), adjustment_amount_payee_currency=Decimal('431.20'), adjustment_payee_currency_code='GBP', ) records = LedgerAdjustmentApplied.query.all() assert len(records) == 3 assert ledger_adjustment.ledger_adjustment_applied == records assert all([record.ledger_adjustment == ledger_adjustment for record in records]) def test_create_adjustment_applied_stores_apply_to_flowthrough_payment( mock_event_fixtures, ): """Test that apply_to_flowthrough_payment is persisted on LedgerAdjustmentApplied.""" ledger_adjustment = LedgerAdjustmentFactory.create() mock_worksheet_adjustment(worksheet_adjustment_id=10) record = LedgerAdjustmentApplied.create( abacus_event_id=1, account_id=1, contract_id=1, statement_period_id=1, ledger_adjustment_id=ledger_adjustment.ledger_adjustment_id, worksheet_adjustment_id=10, adjustment_currency_code='USD', adjustment_amount=Decimal('500'), adjustment_amount_payee_currency=Decimal('431.20'), adjustment_payee_currency_code='GBP', apply_to_flowthrough_payment=True, ) fetched = LedgerAdjustmentApplied.get_by_id(record.ledger_adjustment_applied_id) assert fetched.apply_to_flowthrough_payment is True def test_create_adjustment_applied_apply_to_flowthrough_payment_defaults_to_none( mock_event_fixtures, ): """Test that apply_to_flowthrough_payment defaults to None when not provided.""" ledger_adjustment = LedgerAdjustmentFactory.create() mock_worksheet_adjustment(worksheet_adjustment_id=11) record = LedgerAdjustmentApplied.create( abacus_event_id=1, account_id=1, contract_id=1, statement_period_id=1, ledger_adjustment_id=ledger_adjustment.ledger_adjustment_id, worksheet_adjustment_id=11, adjustment_currency_code='USD', adjustment_amount=Decimal('500'), adjustment_amount_payee_currency=Decimal('431.20'), adjustment_payee_currency_code='GBP', ) fetched = LedgerAdjustmentApplied.get_by_id(record.ledger_adjustment_applied_id) assert fetched.apply_to_flowthrough_payment is None def test_create_adjustment_detail_applied_stores_apply_to_flowthrough_payment( mock_event_fixtures, ): """Test that apply_to_flowthrough_payment is persisted on LedgerAdjustmentDetailApplied.""" mock_worksheet_adjustment_detail(worksheet_adjustment_detail_id=10) detail = LedgerAdjustmentDetailFactory.create() record = LedgerAdjustmentDetailApplied.create( ledger_adjustment_detail_id=detail.ledger_adjustment_detail_id, worksheet_adjustment_detail_id=10, adjustment_currency_code='USD', adjustment_amount=Decimal('750'), adjustment_amount_payee_currency=Decimal('600.00'), adjustment_payee_currency_code='GBP', apply_to_flowthrough_payment=False, ) fetched = LedgerAdjustmentDetailApplied.get_by_id( record.ledger_adjustment_detail_applied_id ) assert fetched.apply_to_flowthrough_payment is False