"""Unit tests for contract_flowthrough schemas.""" import pytest from marshmallow import ValidationError from abacus_contract.constants.constants import CONTRACT_FLOWTHROUGH_STATUSES from abacus_contract.schemas.contract_flowthrough import ( BaseContractFlowthroughSchema, ContractFlowthroughDetailSchema, ContractFlowthroughPostSchema, ContractFlowthroughPutSchema, ) from abacus_contract.tests.utils.factories import ContractFlowthroughFactory def test_base_contract_flowthrough_schema(): """Test contract_flowthrough base schema.""" mock_contract_flowthrough = ContractFlowthroughFactory.create() res = BaseContractFlowthroughSchema().dump(mock_contract_flowthrough) assert res == { 'contract_id': mock_contract_flowthrough.contract_id, 'reference_flowthrough_calculation_id': mock_contract_flowthrough.reference_flowthrough_calculation_id, 'flowthrough_rate': str(mock_contract_flowthrough.flowthrough_rate), 'flowthrough_status': mock_contract_flowthrough.flowthrough_status, 'has_automatic_shutoff': mock_contract_flowthrough.has_automatic_shutoff, 'recoupment_cap': mock_contract_flowthrough.recoupment_cap, 'calculation_comment': mock_contract_flowthrough.calculation_comment, 'previous_flowthrough_status': mock_contract_flowthrough.previous_flowthrough_status, 'status_last_modified_by': mock_contract_flowthrough.status_last_modified_by, 'status_last_modified': str( mock_contract_flowthrough.status_last_modified.date() ), } def test_contract_flowthrough_detail_schema(): """Test contract_flowthrough detail schema.""" mock_contract_flowthrough = ContractFlowthroughFactory.create() res = ContractFlowthroughDetailSchema().dump(mock_contract_flowthrough) assert res == { 'contract_id': mock_contract_flowthrough.contract_id, 'contract_flowthrough_id': mock_contract_flowthrough.contract_flowthrough_id, 'reference_flowthrough_calculation_id': mock_contract_flowthrough.reference_flowthrough_calculation_id, 'flowthrough_rate': str(mock_contract_flowthrough.flowthrough_rate), 'flowthrough_status': mock_contract_flowthrough.flowthrough_status, 'has_automatic_shutoff': mock_contract_flowthrough.has_automatic_shutoff, 'recoupment_cap': mock_contract_flowthrough.recoupment_cap, 'calculation_comment': mock_contract_flowthrough.calculation_comment, 'previous_flowthrough_status': mock_contract_flowthrough.previous_flowthrough_status, 'status_last_modified_by': mock_contract_flowthrough.status_last_modified_by, 'status_last_modified': str( mock_contract_flowthrough.status_last_modified.date() ), } def test_contract_flowthrough_put_schema(): """Test contract_flowthrough PUT schema.""" mock_put_request = { 'reference_flowthrough_calculation_id': 1, 'flowthrough_rate': 1.09, 'flowthrough_status': CONTRACT_FLOWTHROUGH_STATUSES.ACTIVE, 'recoupment_cap': 1290, 'calculation_comment': 'foo', } res = ContractFlowthroughPutSchema().dump(mock_put_request) assert res == { 'reference_flowthrough_calculation_id': mock_put_request[ 'reference_flowthrough_calculation_id' ], 'flowthrough_rate': str(mock_put_request['flowthrough_rate']), 'flowthrough_status': mock_put_request['flowthrough_status'], 'recoupment_cap': mock_put_request['recoupment_cap'], 'calculation_comment': mock_put_request['calculation_comment'], } @pytest.mark.parametrize( 'schema_cls', [ContractFlowthroughPutSchema, ContractFlowthroughPostSchema] ) @pytest.mark.parametrize('bad_value', [0, -1, 1500.123, 0.001]) def test_recoupment_cap_rejects_invalid_values(schema_cls, bad_value): """Test that recoupment_cap rejects zero, negatives, and >2 decimal places.""" with pytest.raises(ValidationError): schema_cls().fields['recoupment_cap'].deserialize(bad_value) @pytest.mark.parametrize( 'schema_cls', [ContractFlowthroughPutSchema, ContractFlowthroughPostSchema] ) @pytest.mark.parametrize('good_value', [1, 1500, 1500.7, 1500.75, 0.01]) def test_recoupment_cap_accepts_valid_values(schema_cls, good_value): """Test that recoupment_cap accepts positive values with at most 2 decimal places.""" result = schema_cls().fields['recoupment_cap'].deserialize(good_value) assert result == good_value def test_contract_flowthrough_post_schema(): """Test contract_flowthrough POST schema.""" mock_put_request = { 'reference_flowthrough_calculation_id': 1, 'flowthrough_rate': 1.09, 'recoupment_cap': 1290, 'calculation_comment': 'foo', } res = ContractFlowthroughPostSchema().dump(mock_put_request) assert res == { 'reference_flowthrough_calculation_id': mock_put_request[ 'reference_flowthrough_calculation_id' ], 'flowthrough_rate': str(mock_put_request['flowthrough_rate']), 'recoupment_cap': mock_put_request['recoupment_cap'], 'calculation_comment': mock_put_request['calculation_comment'], }