Skip to content

Commit

Permalink
Fixed floating point data type check in knowledge vault (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshugt16 authored Dec 11, 2024
1 parent cbcccc2 commit 8220bdd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 7 additions & 3 deletions kairon/shared/cognition/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 16 additions & 1 deletion tests/unit_test/data_processor/data_processor2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8220bdd

Please sign in to comment.