Skip to content

Commit

Permalink
added float data type for cognition schema (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
GMayank0310 authored Jul 19, 2024
1 parent 2a1b4dd commit 32ecad2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions kairon/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,8 +1116,8 @@ class ColumnMetadata(BaseModel):
def check(cls, values):
from kairon.shared.utils import Utility

if values.get('data_type') not in [CognitionMetadataType.str.value, CognitionMetadataType.int.value]:
raise ValueError("Only str and int data types are supported")
if values.get('data_type') not in [CognitionMetadataType.str.value, CognitionMetadataType.int.value, CognitionMetadataType.float.value]:
raise ValueError("Only str, int and float data types are supported")
if Utility.check_empty_string(values.get('column_name')):
raise ValueError("Column name cannot be empty")
return values
Expand Down
6 changes: 3 additions & 3 deletions kairon/shared/cognition/data_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class ColumnMetadata(EmbeddedDocument):
column_name = StringField(required=True)
data_type = StringField(required=True, default=CognitionMetadataType.str.value,
choices=[CognitionMetadataType.str.value, CognitionMetadataType.int.value])
choices=[CognitionMetadataType.str.value, CognitionMetadataType.int.value, CognitionMetadataType.float.value])
enable_search = BooleanField(default=True)
create_embeddings = BooleanField(default=True)

Expand All @@ -20,8 +20,8 @@ def validate(self, clean=True):

if clean:
self.clean()
if self.data_type not in [CognitionMetadataType.str.value, CognitionMetadataType.int.value]:
raise ValidationError("Only str and int data types are supported")
if self.data_type not in [CognitionMetadataType.str.value, CognitionMetadataType.int.value, CognitionMetadataType.float.value]:
raise ValidationError("Only str,int and float data types are supported")
if Utility.check_empty_string(self.column_name):
raise ValidationError("Column name cannot be empty")

Expand Down
5 changes: 5 additions & 0 deletions kairon/shared/cognition/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ def validate_column_values(data: Any, schema: Dict):
return int(data[column_name])
except ValueError:
raise AppException("Invalid data type!")
elif column_name in data and data[column_name] and data_type == CognitionMetadataType.float.value:
try:
return float(data[column_name])
except ValueError:
raise AppException("Invalid data type!")
else:
return data[column_name]

Expand Down
1 change: 1 addition & 0 deletions kairon/shared/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ class CognitionDataType(str, Enum):
class CognitionMetadataType(str, Enum):
str = "str"
int = "int"
float = "float"
6 changes: 3 additions & 3 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2714,10 +2714,10 @@ def test_metadata_upload_invalid_data_type():
)
actual = response.json()
assert actual["message"] == [{'loc': ['body', 'metadata', 0, 'data_type'],
'msg': "value is not a valid enumeration member; permitted: 'str', 'int'",
'type': 'type_error.enum', 'ctx': {'enum_values': ['str', 'int']}},
'msg': "value is not a valid enumeration member; permitted: 'str', 'int', 'float'",
'type': 'type_error.enum', 'ctx': {'enum_values': ['str', 'int', 'float']}},
{'loc': ['body', 'metadata', 0, '__root__'],
'msg': 'Only str and int data types are supported', 'type': 'value_error'}]
'msg': 'Only str, int and float data types are supported', 'type': 'value_error'}]
assert not actual["data"]
assert actual["error_code"] == 422

Expand Down
2 changes: 1 addition & 1 deletion tests/unit_test/data_processor/data_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15025,7 +15025,7 @@ def test_save_payload_metadata_data_type_invalid(self):
"bot": bot,
"user": user
}
with pytest.raises(ValidationError, match="Only str and int data types are supported"):
with pytest.raises(ValidationError, match="Only str,int and float data types are supported"):
CognitionSchema(**schema).save()

def test_get_payload_metadata(self):
Expand Down

0 comments on commit 32ecad2

Please sign in to comment.