"""Tests for Worksheet Account ContractClosing Balance schemas.""" from marshmallow import ValidationError import pytest from payment.schemas.worksheet_account_contract_closing_balance import ( WorksheetAccountContractClosingBalanceBulkFilterKeySchema, WorksheetAccountContractClosingBalanceBulkFilterParamsPostSchema, WorksheetAccountContractClosingBalanceDataloaderItemSchema, WorksheetAccountContractClosingBalanceDataloaderSchema, WorksheetAccountContractClosingBalanceInputSchema, WorksheetAccountContractClosingBalanceSchema, ) MOCK_CLOSING_BALANCE_DATA = { 'worksheet_account_contract_closing_balance_id': 1, 'account_id': 12, 'contract_id': 123, 'reference_payment_entity_id': 1234, 'abacus_event_id': 1234, 'ledger_account_contract_id': 1234, 'statement_period_id': 1234, 'currency_code': 'USD', 'amount': '100.00', 'includes_tax_adjustments': False, } def test_worksheet_account_contract_closing_balance_schema(): """Test WorksheetAccountContractClosingBalanceSchema schema.""" body = MOCK_CLOSING_BALANCE_DATA res = WorksheetAccountContractClosingBalanceSchema().dump(body) assert res assert res.get('worksheet_account_contract_closing_balance_id') == body.get( 'worksheet_account_contract_closing_balance_id' ) assert res.get('account_id') == body.get('account_id') assert res.get('contract_id') == body.get('contract_id') assert res.get('reference_payment_entity_id') == body.get( 'reference_payment_entity_id' ) assert res.get('abacus_event_id') == body.get('abacus_event_id') assert res.get('ledger_account_contract_id') == body.get( 'ledger_account_contract_id' ) assert res.get('statement_period_id') == body.get('statement_period_id') assert res.get('currency_code') == body.get('currency_code') assert res.get('amount') == str(body.get('amount')) assert res.get('includes_tax_adjustments') == body.get('includes_tax_adjustments') def test_worksheet_account_contract_closing_balance_dataloader_item_schema(): """Test dataloader item schema.""" body = MOCK_CLOSING_BALANCE_DATA res = WorksheetAccountContractClosingBalanceDataloaderItemSchema().dump( {'data': body} ) res_data = res.get('data') assert res_data assert res_data.get('worksheet_account_contract_closing_balance_id') == body.get( 'worksheet_account_contract_closing_balance_id' ) assert res_data.get('account_id') == body.get('account_id') assert res_data.get('contract_id') == body.get('contract_id') assert res_data.get('reference_payment_entity_id') == body.get( 'reference_payment_entity_id' ) assert res_data.get('abacus_event_id') == body.get('abacus_event_id') assert res_data.get('ledger_account_contract_id') == body.get( 'ledger_account_contract_id' ) assert res_data.get('statement_period_id') == body.get('statement_period_id') assert res_data.get('currency_code') == body.get('currency_code') assert res_data.get('amount') == str(body.get('amount')) assert res_data.get('includes_tax_adjustments') == body.get( 'includes_tax_adjustments' ) def test_payment_group_payment_account_dataloader_schema(): """Test dataloader schema.""" body = MOCK_CLOSING_BALANCE_DATA res = WorksheetAccountContractClosingBalanceDataloaderSchema().dump( {'items': [{'data': body}]} ) res_data = res.get('items')[0].get('data') assert res_data assert res_data.get('worksheet_account_contract_closing_balance_id') == body.get( 'worksheet_account_contract_closing_balance_id' ) assert res_data.get('account_id') == body.get('account_id') assert res_data.get('contract_id') == body.get('contract_id') assert res_data.get('reference_payment_entity_id') == body.get( 'reference_payment_entity_id' ) assert res_data.get('abacus_event_id') == body.get('abacus_event_id') assert res_data.get('ledger_account_contract_id') == body.get( 'ledger_account_contract_id' ) assert res_data.get('statement_period_id') == body.get('statement_period_id') assert res_data.get('currency_code') == body.get('currency_code') assert res_data.get('amount') == str(body.get('amount')) assert res_data.get('includes_tax_adjustments') == body.get( 'includes_tax_adjustments' ) def test_worksheet_account_contract_closing_balance_bulk_filter_key_schema_load(): """Test BulkFilterKeySchema loads valid worksheet_closing_balance_ids.""" result = WorksheetAccountContractClosingBalanceBulkFilterKeySchema().load( {'worksheet_closing_balance_ids': [1, 2, 3]} ) assert result == {'worksheet_closing_balance_ids': [1, 2, 3]} def test_worksheet_account_contract_closing_balance_bulk_filter_key_schema_missing_field(): """Test that omitting worksheet_closing_balance_ids is valid (field is optional).""" result = WorksheetAccountContractClosingBalanceBulkFilterKeySchema().load({}) assert result == {} def test_worksheet_account_contract_closing_balance_bulk_filter_key_schema_empty_list(): """Test that an empty worksheet_closing_balance_ids list raises ValidationError.""" with pytest.raises(ValidationError): WorksheetAccountContractClosingBalanceBulkFilterKeySchema().load( {'worksheet_closing_balance_ids': []} ) def test_worksheet_account_contract_closing_balance_bulk_filter_key_schema_zero_id(): """Test that 0 is rejected (min value is 1).""" with pytest.raises(ValidationError): WorksheetAccountContractClosingBalanceBulkFilterKeySchema().load( {'worksheet_closing_balance_ids': [0, 1]} ) def test_worksheet_account_contract_closing_balance_bulk_filter_params_schema_load(): """Test BulkFilterParamsPostSchema loads valid nested filters.""" result = WorksheetAccountContractClosingBalanceBulkFilterParamsPostSchema().load( {'filters': {'worksheet_closing_balance_ids': [1, 2]}} ) assert result == {'filters': {'worksheet_closing_balance_ids': [1, 2]}} def test_worksheet_account_contract_closing_balance_bulk_filter_params_schema_missing_filters(): """Test that omitting filters is valid (field is optional).""" result = WorksheetAccountContractClosingBalanceBulkFilterParamsPostSchema().load({}) assert result == {} @pytest.mark.parametrize( 'missing_field', [ 'account_id', 'contract_id', 'ledger_account_contract_id', 'currency_code', 'amount', 'includes_tax_adjustments', ], ) def test_worksheet_account_contract_closing_balance_input_schema_load_missing_required_field_raises( missing_field: str, ) -> None: """Test WorksheetAccountContractClosingBalanceInputSchema load requires all fields.""" payload = { 'account_id': 12, 'contract_id': 123, 'ledger_account_contract_id': 1234, 'currency_code': 'USD', 'amount': '100.00', 'includes_tax_adjustments': False, } payload.pop(missing_field) with pytest.raises(ValidationError): WorksheetAccountContractClosingBalanceInputSchema().load(payload)