"""Test Ledger Adjustment Applied schemas.""" from decimal import Decimal import pytest from ledger.schemas.ledger_adjustment_applied import ( LedgerAdjustmentAppliedPostSchema, LedgerAdjustmentDetailAppliedPostSchema, ) from tests.utils.factories import ( LedgerAdjustmentAppliedFactory, LedgerAdjustmentDetailAppliedFactory, ) def test_ledger_adjustment_detail_applied_post_data_load( mock_ledger_adjustment_detail_applied_post_data, ): """Test Ledger Adjustment Detail Applied POST schema loading.""" entries = LedgerAdjustmentDetailAppliedPostSchema(many=True).load( mock_ledger_adjustment_detail_applied_post_data ) assert entries == [ { 'ledger_adjustment_detail_id': 1, 'worksheet_adjustment_id': 1, 'worksheet_adjustment_detail_id': 1, 'adjustment_currency_code': 'USD', 'adjustment_amount': Decimal('1500.00'), 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': Decimal('1395.20'), }, { 'ledger_adjustment_detail_id': 2, 'worksheet_adjustment_id': 1, 'worksheet_adjustment_detail_id': 2, 'adjustment_currency_code': 'GBP', 'adjustment_amount': Decimal('1500.00'), 'adjustment_payee_currency_code': 'USD', 'adjustment_amount_payee_currency': Decimal('1631.35'), }, ] def test_ledger_adjustment_detail_applied_post_data_dump(mock_event_fixtures): """Test Ledger Adjustment Detail Applied POST schema dumping.""" entity = LedgerAdjustmentDetailAppliedFactory.create() res = LedgerAdjustmentDetailAppliedPostSchema().dump(entity) assert res == { 'ledger_adjustment_detail_id': entity.ledger_adjustment_detail_id, 'worksheet_adjustment_id': entity.worksheet_adjustment_id, 'worksheet_adjustment_detail_id': entity.worksheet_adjustment_detail_id, 'adjustment_currency_code': entity.adjustment_currency_code, 'adjustment_amount': str(entity.adjustment_amount), 'adjustment_payee_currency_code': entity.adjustment_payee_currency_code, 'adjustment_amount_payee_currency': str( entity.adjustment_amount_payee_currency ), 'apply_to_flowthrough_payment': None, } def test_ledger_adjustment_with_details_applied_post_load( mock_bulk_ledger_adjustment_with_details_applied_request_body, ): """Test Ledger Adjustment With Details Applied POST schema loading.""" entries = LedgerAdjustmentAppliedPostSchema(many=True).load( mock_bulk_ledger_adjustment_with_details_applied_request_body ) print('---entries---', entries) assert entries == [ { 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'statement_period_id': 1, 'ledger_adjustment_id': 1, 'worksheet_adjustment_id': 1, 'adjustment_currency_code': 'USD', 'adjustment_amount': Decimal('3000.00'), 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': Decimal('3026.55'), 'details': [ { 'ledger_adjustment_detail_id': 1, 'worksheet_adjustment_id': 1, 'worksheet_adjustment_detail_id': 1, 'adjustment_currency_code': 'USD', 'adjustment_amount': Decimal('1500.00'), 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': Decimal('1395.20'), }, { 'ledger_adjustment_detail_id': 2, 'worksheet_adjustment_id': 1, 'worksheet_adjustment_detail_id': 2, 'adjustment_currency_code': 'GBP', 'adjustment_amount': Decimal('1500.00'), 'adjustment_payee_currency_code': 'USD', 'adjustment_amount_payee_currency': Decimal('1631.35'), }, ], } ] def test_ledger_adjustment_with_details_applied_post_dump(mock_event_fixtures): """Test Ledger Adjustment With Details Applied POST schema dumping.""" entity = LedgerAdjustmentAppliedFactory.create() detail1 = LedgerAdjustmentDetailAppliedFactory.create() detail2 = LedgerAdjustmentDetailAppliedFactory.create( worksheet_adjustment_detail_id=2 ) entity.details.append(detail1) entity.details.append(detail2) res = LedgerAdjustmentAppliedPostSchema().dump(entity) assert res == { 'abacus_event_id': entity.abacus_event_id, 'account_id': entity.account_id, 'contract_id': entity.contract_id, 'statement_period_id': entity.statement_period_id, 'ledger_adjustment_id': entity.ledger_adjustment_id, 'worksheet_adjustment_id': entity.worksheet_adjustment_id, 'adjustment_currency_code': entity.adjustment_currency_code, 'adjustment_amount': str(entity.adjustment_amount), 'adjustment_payee_currency_code': entity.adjustment_payee_currency_code, 'adjustment_amount_payee_currency': str( entity.adjustment_amount_payee_currency ), 'apply_to_flowthrough_payment': None, 'details': [ { 'ledger_adjustment_detail_id': detail1.ledger_adjustment_detail_id, 'worksheet_adjustment_id': detail1.worksheet_adjustment_id, 'worksheet_adjustment_detail_id': detail1.worksheet_adjustment_detail_id, 'adjustment_currency_code': detail1.adjustment_currency_code, 'adjustment_amount': str(detail1.adjustment_amount), 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': str( detail1.adjustment_amount_payee_currency ), 'apply_to_flowthrough_payment': None, }, { 'ledger_adjustment_detail_id': detail2.ledger_adjustment_detail_id, 'worksheet_adjustment_id': detail2.worksheet_adjustment_id, 'worksheet_adjustment_detail_id': detail2.worksheet_adjustment_detail_id, 'adjustment_currency_code': detail2.adjustment_currency_code, 'adjustment_amount': str(detail2.adjustment_amount), 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': str( detail2.adjustment_amount_payee_currency ), 'apply_to_flowthrough_payment': None, }, ], } @pytest.mark.parametrize('value', [True, False, None]) def test_ledger_adjustment_applied_post_load_apply_to_flowthrough_payment(value): """Test LedgerAdjustmentAppliedPostSchema loads apply_to_flowthrough_payment correctly.""" data = [ { 'abacus_event_id': 1, 'account_id': 1, 'statement_period_id': 1, 'adjustment_currency_code': 'USD', 'adjustment_amount': '3000.00', 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': '3026.55', 'apply_to_flowthrough_payment': value, } ] entries = LedgerAdjustmentAppliedPostSchema(many=True).load(data) assert entries[0]['apply_to_flowthrough_payment'] == value @pytest.mark.parametrize('value', [True, False, None]) def test_ledger_adjustment_detail_applied_post_load_apply_to_flowthrough_payment(value): """Test LedgerAdjustmentDetailAppliedPostSchema loads apply_to_flowthrough_payment correctly.""" data = [ { 'adjustment_currency_code': 'USD', 'adjustment_amount': '1500.00', 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': '1395.20', 'apply_to_flowthrough_payment': value, } ] entries = LedgerAdjustmentDetailAppliedPostSchema(many=True).load(data) assert entries[0]['apply_to_flowthrough_payment'] == value def test_ledger_adjustment_applied_post_load_excludes_apply_to_flowthrough_payment_when_absent(): """Test apply_to_flowthrough_payment is omitted from loaded data when not provided.""" data = [ { 'abacus_event_id': 1, 'account_id': 1, 'statement_period_id': 1, 'adjustment_currency_code': 'USD', 'adjustment_amount': '3000.00', 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': '3026.55', } ] entries = LedgerAdjustmentAppliedPostSchema(many=True).load(data) assert 'apply_to_flowthrough_payment' not in entries[0] @pytest.mark.parametrize('value', [True, False]) def test_ledger_adjustment_applied_post_dump_apply_to_flowthrough_payment( value, mock_event_fixtures ): """Test LedgerAdjustmentAppliedPostSchema dumps apply_to_flowthrough_payment correctly.""" entity = LedgerAdjustmentAppliedFactory.create() entity.apply_to_flowthrough_payment = value res = LedgerAdjustmentAppliedPostSchema().dump(entity) assert res['apply_to_flowthrough_payment'] == value @pytest.mark.parametrize('value', [True, False]) def test_ledger_adjustment_detail_applied_post_dump_apply_to_flowthrough_payment( value, mock_event_fixtures ): """Test LedgerAdjustmentDetailAppliedPostSchema dumps apply_to_flowthrough_payment correctly.""" entity = LedgerAdjustmentDetailAppliedFactory.create() entity.apply_to_flowthrough_payment = value res = LedgerAdjustmentDetailAppliedPostSchema().dump(entity) assert res['apply_to_flowthrough_payment'] == value