"""Functional tests for Ledger Adjustment Applied.""" from ledger.models.ledger_adjustment_applied import LedgerAdjustmentApplied from tests.utils.factories import ( LedgerAdjustmentDetailFactory, LedgerAdjustmentFactory, ) def test_bulk_create_ledger_adjustment_applied( fixture_client, mock_event_fixtures, mock_bulk_ledger_adjustment_with_details_applied_request_body, ): """Test bulk create Ledger Adjustment Applied.""" for record in mock_bulk_ledger_adjustment_with_details_applied_request_body: LedgerAdjustmentFactory.create() for _ in record.get('details', []): LedgerAdjustmentDetailFactory() post_body = mock_bulk_ledger_adjustment_with_details_applied_request_body res = fixture_client.post('/ledger-adjustment-applied/bulk', json=post_body) assert res.status_code == 201 assert res.json['created'] == 1 assert res.json['filtered'] == 0 def test_bulk_create_ledger_adjustment_applied_with_deduplication( fixture_client, mock_event_fixtures, mock_bulk_ledger_adjustment_with_details_applied_request_body, ): """Test bulk endpoint filters out duplicate adjustments on retry.""" for record in mock_bulk_ledger_adjustment_with_details_applied_request_body: LedgerAdjustmentFactory.create() for _ in record.get('details', []): LedgerAdjustmentDetailFactory() post_body = mock_bulk_ledger_adjustment_with_details_applied_request_body res = fixture_client.post('/ledger-adjustment-applied/bulk', json=post_body) assert res.status_code == 201 assert res.json['created'] >= 1 res_retry = fixture_client.post('/ledger-adjustment-applied/bulk', json=post_body) assert res_retry.status_code == 201 assert res_retry.json['created'] == 0 assert res_retry.json['filtered'] > 0 def test_bulk_create_ledger_adjustment_applied_with_apply_to_flowthrough_payment( fixture_client, mock_event_fixtures, ): """Test that apply_to_flowthrough_payment is stored end-to-end via the bulk endpoint.""" LedgerAdjustmentFactory.create() LedgerAdjustmentDetailFactory.create() LedgerAdjustmentDetailFactory.create() post_body = [ { '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': '3000.00', 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': '3026.55', 'apply_to_flowthrough_payment': True, } ] res = fixture_client.post('/ledger-adjustment-applied/bulk', json=post_body) assert res.status_code == 201 assert res.json['created'] == 1 record = LedgerAdjustmentApplied.query.filter_by(worksheet_adjustment_id=1).first() assert record is not None assert record.apply_to_flowthrough_payment is True def test_bulk_create_ledger_adjustment_applied_apply_to_flowthrough_payment_false( fixture_client, mock_event_fixtures, ): """Test that apply_to_flowthrough_payment=False is stored correctly.""" LedgerAdjustmentFactory.create() post_body = [ { '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': '1000.00', 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': '800.00', 'apply_to_flowthrough_payment': False, } ] res = fixture_client.post('/ledger-adjustment-applied/bulk', json=post_body) assert res.status_code == 201 record = LedgerAdjustmentApplied.query.filter_by(worksheet_adjustment_id=1).first() assert record is not None assert record.apply_to_flowthrough_payment is False def test_bulk_create_ledger_adjustment_applied_apply_to_flowthrough_payment_absent( fixture_client, mock_event_fixtures, ): """Test that apply_to_flowthrough_payment defaults to None when omitted from request.""" LedgerAdjustmentFactory.create() post_body = [ { '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': '1000.00', 'adjustment_payee_currency_code': 'GBP', 'adjustment_amount_payee_currency': '800.00', } ] res = fixture_client.post('/ledger-adjustment-applied/bulk', json=post_body) assert res.status_code == 201 record = LedgerAdjustmentApplied.query.filter_by(worksheet_adjustment_id=1).first() assert record is not None assert record.apply_to_flowthrough_payment is None