"""Test for schema for worksheet_account_contract_payable_details.""" from decimal import Decimal from marshmallow import ValidationError import pytest from payment.constants.constants import DEFAULT_BULK_MAX_LIMIT from payment.constants.error import ERROR_BULK_MAX_LIMIT from payment.schemas.worksheet_account_contract_payable_details import ( FilterKeySchema, WorksheetAccountContractPayableDetailsBulkDeleteSchema, WorksheetAccountContractPayableDetailsCreateSchema, ) def test_worksheet_account_contract_payable_details_create_schema_load_success(): """Test WorksheetAccountContractPayableDetailsCreateSchema load success.""" # all filled test_data = { 'worksheet_account_contract_payable_after_tax_id': 1, 'account_id': 2, 'contract_id': 3, 'target_table': 'test_table', 'target_id': 4, 'reference_target_table': 'test_table_2', 'reference_target_id': 5, 'payable_detail_type_id': 6, 'amount_payable': '10.01', 'currency': 'USD', 'notes': 'test note', } res = WorksheetAccountContractPayableDetailsCreateSchema().load(test_data) assert res == {**test_data, 'amount_payable': Decimal(test_data['amount_payable'])} # nullable test_data = { 'worksheet_account_contract_payable_after_tax_id': 1, 'account_id': 2, 'contract_id': 3, 'target_table': 'test_table', 'target_id': 4, 'reference_target_table': None, 'reference_target_id': None, 'payable_detail_type_id': None, 'amount_payable': '10.01', 'currency': 'USD', 'notes': None, } res = WorksheetAccountContractPayableDetailsCreateSchema().load(test_data) assert res == {**test_data, 'amount_payable': Decimal(test_data['amount_payable'])} # optional test_data = { 'worksheet_account_contract_payable_after_tax_id': 1, 'account_id': 2, 'contract_id': 3, 'target_table': 'test_table', 'target_id': 4, 'amount_payable': '10.01', 'currency': 'USD', } res = WorksheetAccountContractPayableDetailsCreateSchema().load(test_data) assert res == {**test_data, 'amount_payable': Decimal(test_data['amount_payable'])} def test_worksheet_account_contract_payable_details_create_schema_load_failure(): """Test WorksheetAccountContractPayableDetailsCreateSchema load failure.""" with pytest.raises(ValidationError) as exc_info: WorksheetAccountContractPayableDetailsCreateSchema().load({}) assert { 'worksheet_account_contract_payable_after_tax_id', 'account_id', 'contract_id', 'target_table', 'target_id', 'amount_payable', 'currency', } == set(exc_info.value.messages_dict) with pytest.raises(ValidationError) as exc_info: WorksheetAccountContractPayableDetailsCreateSchema().load( { 'reference_target_table': None, 'reference_target_id': None, 'payable_detail_type_id': None, 'notes': None, } ) assert { 'worksheet_account_contract_payable_after_tax_id', 'account_id', 'contract_id', 'target_table', 'target_id', 'amount_payable', 'currency', } == set(exc_info.value.messages_dict) def test_filter_key_schema(): """Test filter key schema.""" worksheet_payable_ids_filter_key = {'worksheet_payable_after_tax_ids': [1, 3]} schema = FilterKeySchema() assert ( schema.dump(worksheet_payable_ids_filter_key) == worksheet_payable_ids_filter_key ) # noqa: E501 def test_bulk_delete_schema_load_success(): """Valid payload loads without error.""" data = {'worksheet_account_contract_payable_after_tax_ids': [1, 2, 3]} res = WorksheetAccountContractPayableDetailsBulkDeleteSchema().load(data) assert res == data def test_bulk_delete_schema_load_missing_field(): """Missing required ids field fails.""" with pytest.raises(ValidationError) as exc: WorksheetAccountContractPayableDetailsBulkDeleteSchema().load({}) assert 'worksheet_account_contract_payable_after_tax_ids' in exc.value.messages_dict def test_bulk_delete_schema_load_empty_list(): """Empty list fails validation.""" with pytest.raises(ValidationError): WorksheetAccountContractPayableDetailsBulkDeleteSchema().load( {'worksheet_account_contract_payable_after_tax_ids': []} ) def test_bulk_delete_schema_load_over_limit(): """Over the bulk limit fails validation.""" limit = DEFAULT_BULK_MAX_LIMIT with pytest.raises(ValidationError) as exc: WorksheetAccountContractPayableDetailsBulkDeleteSchema().load( { 'worksheet_account_contract_payable_after_tax_ids': list( range(1, limit + 2) ) } ) assert ERROR_BULK_MAX_LIMIT.format(limit) in exc.value.messages['_schema']