From 2a6ae4ae1769f22a702bcb2983a6ff3f1c51a1fb Mon Sep 17 00:00:00 2001 From: himanshu_gupta Date: Wed, 11 Dec 2024 16:30:25 +0530 Subject: [PATCH] Fixed floating point data type check in knowledge vault --- kairon/shared/cognition/processor.py | 10 +++++++--- .../data_processor/data_processor2_test.py | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/kairon/shared/cognition/processor.py b/kairon/shared/cognition/processor.py index 88443e28b..93cb2a53a 100644 --- a/kairon/shared/cognition/processor.py +++ b/kairon/shared/cognition/processor.py @@ -390,9 +390,13 @@ def validate_column_values(data: Any, schema: Dict): raise AppException( f"Invalid data type for '{column_name}': Expected integer value") - if data_type == CognitionMetadataType.float.value and not isinstance(value, float): - raise AppException( - f"Invalid data type for '{column_name}': Expected float value") + if data_type == CognitionMetadataType.float.value: + try: + return float(value) + except (ValueError, TypeError): + raise AppException( + f"Invalid data type for '{column_name}': Expected float value" + ) if data_type == CognitionMetadataType.str.value and not isinstance(value, str): raise AppException( diff --git a/tests/unit_test/data_processor/data_processor2_test.py b/tests/unit_test/data_processor/data_processor2_test.py index 5abb2781a..0a18cb22e 100644 --- a/tests/unit_test/data_processor/data_processor2_test.py +++ b/tests/unit_test/data_processor/data_processor2_test.py @@ -114,13 +114,28 @@ def test_validate_metadata_and_payload_invalid_float(): data = { "id": 1, "item": "Cover", - "price": "cheap", # Invalid: should be a float + "price": "cheap", "quantity": 20 } with pytest.raises(AppException, match="Invalid data type for 'price': Expected float value"): CognitionDataProcessor.validate_column_values(data, schema) +def test_validate_metadata_and_payload_int_value_in_float_field(): + schema = { + "column_name": "price", + "data_type": "float", + "enable_search": True, + "create_embeddings": True + } + data = { + "id": 1, + "item": "Cover", + "price": 231, + "quantity": 20 + } + CognitionDataProcessor.validate_column_values(data, schema) + def test_validate_metadata_and_payload_missing_column(): schema = { "column_name": "quantity",