"""Unit tests for Contract Mechanical Deduction model.""" from datetime import datetime from abacus_contract.constants.constants import MECHANICAL_DEDUCTION_ADMIN_TYPES from abacus_contract.constants.constants import MECHANICAL_DEDUCTION_TERRITORIES from abacus_contract.constants.constants import MECHANICAL_DEDUCTION_TYPES from abacus_contract.models import ContractMechanicalDeduction from tests.utils.factories import ContractFactory def test_create_contract_mechanical_deduction(): """Test creating a contract_mechanical_deduction.""" mock_contract = ContractFactory.create() mechanical_types = [ MECHANICAL_DEDUCTION_TYPES.DIGITAL, MECHANICAL_DEDUCTION_TYPES.PHYSICAL ] args = { 'contract_id': mock_contract.contract_id, 'territory': MECHANICAL_DEDUCTION_TERRITORIES.USA, 'mechanical_type': mechanical_types, 'admin_type': MECHANICAL_DEDUCTION_ADMIN_TYPES.BOTH, 'admin_fee': '20.20' } ContractMechanicalDeduction.create(**args) res = ContractMechanicalDeduction.query.all() assert len(res) == 1 assert res[0].contract_id == mock_contract.contract_id assert res[0].territory == MECHANICAL_DEDUCTION_TERRITORIES.USA assert all([mech_type in mechanical_types for mech_type in res[0].mechanical_type]) assert res[0].admin_type == MECHANICAL_DEDUCTION_ADMIN_TYPES.BOTH assert str(res[0].admin_fee) == '20.20' def test_get_active_contracts_with_mechanical_deductions(mocker): """Test raw SQL result for active contracts with mechanical deductions.""" mock_result = [ { 'account_id': 1, 'contract_id': 101, 'term_type': 'track', 'attachments': [], 'mechanical_type': ['digital'] } ] mocked_execute = mocker.patch( 'abacus_contract.models.contract_mechanical_deduction.db.session.execute' ) mocked_execute.return_value.fetchall.return_value = mock_result result = ContractMechanicalDeduction.\ get_active_contracts_with_mechanical_deductions( given_date=datetime(2024, 1, 1).date() ) assert result == mock_result assert isinstance(result, list) assert result[0]['contract_id'] == 101