"""Unit tests for contract_flowthrough model.""" import datetime from decimal import Decimal from abacus_contract.constants.constants import ( CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES, CONTRACT_FLOWTHROUGH_STATUSES, ) from abacus_contract.models import ContractFlowthrough from abacus_contract.tests.utils.factories import ( ContractFactory, ContractFlowthroughFactory, ReferenceFlowthroughCalculationFactory, ) def test_create_contract_flowthrough(): """Test creating a contract_flowthrough.""" mock_contract = ContractFactory.create() mock_reference_flowthrough_calculation = ( ReferenceFlowthroughCalculationFactory.create() ) reference_flowthrough_calculation_id = ( mock_reference_flowthrough_calculation.reference_flowthrough_calculation_id ) args = { 'contract_id': mock_contract.contract_id, 'reference_flowthrough_calculation_id': reference_flowthrough_calculation_id, 'flowthrough_rate': 0.51, 'flowthrough_status': CONTRACT_FLOWTHROUGH_STATUSES.ACTIVE, 'has_automatic_shutoff': 1, 'recoupment_cap': 1234231, 'previous_flowthrough_status': CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES.PAUSED, 'status_last_modified_by': 'Test User', 'status_last_modified': '2025-02-28 00:00:00', } ContractFlowthrough.create(**args) res = ContractFlowthrough.query.all() assert len(res) == 1 assert res[0].contract_id == mock_contract.contract_id assert ( res[0].reference_flowthrough_calculation_id == reference_flowthrough_calculation_id ) assert res[0].flowthrough_rate == Decimal('0.51') assert res[0].flowthrough_status == CONTRACT_FLOWTHROUGH_STATUSES.ACTIVE assert res[0].has_automatic_shutoff == 1 assert res[0].recoupment_cap == 1234231 assert ( res[0].previous_flowthrough_status == CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES.PAUSED ) assert res[0].status_last_modified_by == 'Test User' assert res[0].status_last_modified == datetime.datetime(2025, 2, 28, 0, 0) def test_get_by_contract_id(): """Test to get contract_flowthrough by contract_id.""" mock_contract = ContractFactory.create() mock_flowthroughs = [ ContractFlowthroughFactory.create( contract=mock_contract, deleted_by='Test User', deleted_at=datetime.datetime(2025, 2, 28, 0, 0), ), ContractFlowthroughFactory.create(contract=mock_contract), ] result = ContractFlowthrough.get_by_contract_id(mock_contract.contract_id) assert result == mock_flowthroughs[1] def test_get_by_contract_ids(): """Batch fetch across contract ids, excluding soft-deleted flowthroughs.""" contract_a = ContractFactory.create() contract_b = ContractFactory.create() contract_unrequested = ContractFactory.create() active_a = ContractFlowthroughFactory.create(contract=contract_a) active_b = ContractFlowthroughFactory.create(contract=contract_b) # Soft-deleted (both deleted_at and deleted_by) must be excluded. ContractFlowthroughFactory.create( contract=contract_a, deleted_by='Test User', deleted_at=datetime.datetime(2025, 2, 28, 0, 0), ) # Flowthrough for an unrequested contract must not be returned. ContractFlowthroughFactory.create(contract=contract_unrequested) result = ContractFlowthrough.get_by_contract_ids( [contract_a.contract_id, contract_b.contract_id] ) assert {flowthrough.contract_flowthrough_id for flowthrough in result} == { active_a.contract_flowthrough_id, active_b.contract_flowthrough_id, } assert {flowthrough.contract_id for flowthrough in result} == { contract_a.contract_id, contract_b.contract_id, }