Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knowledge_vault_floating_point_fix #1643

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions kairon/shared/cognition/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ def validate_metadata_and_payload(bot, payload):
collection = payload.get('collection', None)
matched_metadata = CognitionDataProcessor.find_matching_metadata(bot, data, collection)
for metadata_dict in matched_metadata['metadata']:
CognitionDataProcessor.validate_column_values(data, metadata_dict)
column_name = metadata_dict['column_name']
if column_name in data:
data[column_name] = CognitionDataProcessor.validate_column_values(data, metadata_dict)
return data

def save_cognition_data(self, payload: Dict, user: Text, bot: Text):
from kairon import Utility
Expand All @@ -289,7 +292,7 @@ def save_cognition_data(self, payload: Dict, user: Text, bot: Text):
collection_name__iexact=payload.get('collection'), raise_error=False):
raise AppException('Text content type does not have schema!')
if payload.get('content_type') == CognitionDataType.json.value:
CognitionDataProcessor.validate_metadata_and_payload(bot, payload)
payload['data'] = CognitionDataProcessor.validate_metadata_and_payload(bot, payload)

payload_obj = CognitionData()
payload_obj.data = payload.get('data')
Expand All @@ -316,7 +319,7 @@ def update_cognition_data(self, row_id: str, payload: Dict, user: Text, bot: Tex
try:
payload_obj = CognitionData.objects(bot=bot, id=row_id).get()
if content_type == CognitionDataType.json.value:
CognitionDataProcessor.validate_metadata_and_payload(bot, payload)
payload['data'] = CognitionDataProcessor.validate_metadata_and_payload(bot, payload)
payload_obj.data = data
payload_obj.content_type = content_type
payload_obj.collection = payload.get('collection', None)
Expand Down
137 changes: 137 additions & 0 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29683,3 +29683,140 @@ def test_leave_non_existent_bot_1():
assert actual["message"] == "Access to bot is denied"
assert actual["error_code"] == 422
assert not actual["success"]

def test_payload_upload_api_with_float_field(monkeypatch):

def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "with_float_field",
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
payload = {
"data": {"item": "Box","price":54.02},
"content_type": "json",
"collection": "with_float_field"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0

cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
assert data_dict['data']['item'] == 'Box'
assert data_dict['data']['price'] == 54.02
CognitionData.objects(bot=pytest.bot, collection="with_float_field").delete()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add missing test validations and improve error handling.

The test function has several areas for improvement:

  1. The mock function is defined but not patched using monkeypatch.
  2. Missing cleanup in case of test failure.
  3. Missing validation for response status code.

Apply this diff to improve the test:

 def test_payload_upload_api_with_float_field(monkeypatch):
     def _mock_get_bot_settings(*args, **kwargs):
         return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
+    monkeypatch.setattr(Utility, "get_bot_settings", _mock_get_bot_settings)
     metadata = {
         "metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
                      {"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
         "collection_name": "with_float_field",
     }
-    response = client.post(
-        url=f"/api/bot/{pytest.bot}/data/cognition/schema",
-        json=metadata,
-        headers={"Authorization": pytest.token_type + " " + pytest.access_token}
-    )
+    try:
+        response = client.post(
+            url=f"/api/bot/{pytest.bot}/data/cognition/schema",
+            json=metadata,
+            headers={"Authorization": pytest.token_type + " " + pytest.access_token}
+        )
+        assert response.status_code == 200
+
+        payload = {
+            "data": {"item": "Box","price":54.02},
+            "content_type": "json",
+            "collection": "with_float_field"
+        }
+        response = client.post(
+            url=f"/api/bot/{pytest.bot}/data/cognition",
+            json=payload,
+            headers={"Authorization": pytest.token_type + " " + pytest.access_token},
+        )
+        assert response.status_code == 200
+        actual = response.json()
+        pytest.payload_id = actual["data"]["_id"]
+        assert actual["message"] == "Record saved!"
+        assert actual["data"]["_id"]
+        assert actual["error_code"] == 0
 
-    payload = {
-        "data": {"item": "Box","price":54.02},
-        "content_type": "json",
-        "collection": "with_float_field"
-    }
-    response = client.post(
-        url=f"/api/bot/{pytest.bot}/data/cognition",
-        json=payload,
-        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
-    )
-    actual = response.json()
-    pytest.payload_id = actual["data"]["_id"]
-    assert actual["message"] == "Record saved!"
-    assert actual["data"]["_id"]
-    assert actual["error_code"] == 0
-
-    cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field").first()
-    assert cognition_data is not None
-    data_dict = cognition_data.to_mongo().to_dict()
-    assert data_dict['data']['item'] == 'Box'
-    assert data_dict['data']['price'] == 54.02
-    CognitionData.objects(bot=pytest.bot, collection="with_float_field").delete()
+        cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field").first()
+        assert cognition_data is not None
+        data_dict = cognition_data.to_mongo().to_dict()
+        print(f"Database state: {data_dict}")  # Debug info for failed assertions
+        assert data_dict['data']['item'] == 'Box'
+        assert data_dict['data']['price'] == 54.02
+    finally:
+        CognitionData.objects(bot=pytest.bot, collection="with_float_field").delete()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_payload_upload_api_with_float_field(monkeypatch):
def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "with_float_field",
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
payload = {
"data": {"item": "Box","price":54.02},
"content_type": "json",
"collection": "with_float_field"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0
cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
assert data_dict['data']['item'] == 'Box'
assert data_dict['data']['price'] == 54.02
CognitionData.objects(bot=pytest.bot, collection="with_float_field").delete()
def test_payload_upload_api_with_float_field(monkeypatch):
def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
monkeypatch.setattr(Utility, "get_bot_settings", _mock_get_bot_settings)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "with_float_field",
}
try:
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
assert response.status_code == 200
payload = {
"data": {"item": "Box","price":54.02},
"content_type": "json",
"collection": "with_float_field"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
assert response.status_code == 200
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0
cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(f"Database state: {data_dict}") # Debug info for failed assertions
assert data_dict['data']['item'] == 'Box'
assert data_dict['data']['price'] == 54.02
finally:
CognitionData.objects(bot=pytest.bot, collection="with_float_field").delete()


def test_payload_upload_api_with_float_field_value_integer(monkeypatch):

def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "with_float_field_value_integer",
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
payload = {
"data": {"item": "Box","price":54},
"content_type": "json",
"collection": "with_float_field_value_integer"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0

cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(data_dict)
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 54.0
CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").delete()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove debug print and improve test robustness.

The test function needs similar improvements as the previous test, plus:

  1. Remove or improve the debug print statement.
  2. Add validation for schema creation response.

Apply this diff to improve the test:

 def test_payload_upload_api_with_float_field_value_integer(monkeypatch):
     def _mock_get_bot_settings(*args, **kwargs):
         return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
+    monkeypatch.setattr(Utility, "get_bot_settings", _mock_get_bot_settings)
     metadata = {
         "metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
                      {"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
         "collection_name": "with_float_field_value_integer",
     }
-    response = client.post(
-        url=f"/api/bot/{pytest.bot}/data/cognition/schema",
-        json=metadata,
-        headers={"Authorization": pytest.token_type + " " + pytest.access_token}
-    )
+    try:
+        response = client.post(
+            url=f"/api/bot/{pytest.bot}/data/cognition/schema",
+            json=metadata,
+            headers={"Authorization": pytest.token_type + " " + pytest.access_token}
+        )
+        assert response.status_code == 200
+        schema_response = response.json()
+        assert schema_response["success"] is True
+        assert schema_response["error_code"] == 0
 
-    payload = {
-        "data": {"item": "Box","price":54},
-        "content_type": "json",
-        "collection": "with_float_field_value_integer"
-    }
-    response = client.post(
-        url=f"/api/bot/{pytest.bot}/data/cognition",
-        json=payload,
-        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
-    )
-    actual = response.json()
-    pytest.payload_id = actual["data"]["_id"]
-    assert actual["message"] == "Record saved!"
-    assert actual["data"]["_id"]
-    assert actual["error_code"] == 0
+        payload = {
+            "data": {"item": "Box","price":54},
+            "content_type": "json",
+            "collection": "with_float_field_value_integer"
+        }
+        response = client.post(
+            url=f"/api/bot/{pytest.bot}/data/cognition",
+            json=payload,
+            headers={"Authorization": pytest.token_type + " " + pytest.access_token},
+        )
+        assert response.status_code == 200
+        actual = response.json()
+        pytest.payload_id = actual["data"]["_id"]
+        assert actual["message"] == "Record saved!"
+        assert actual["data"]["_id"]
+        assert actual["error_code"] == 0
 
-    cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").first()
-    assert cognition_data is not None
-    data_dict = cognition_data.to_mongo().to_dict()
-    print(data_dict)
-    assert data_dict['data']['item'] == 'Box'
-    assert isinstance(data_dict['data']['price'], float)
-    assert data_dict['data']['price'] == 54.0
-    CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").delete()
+        cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").first()
+        assert cognition_data is not None
+        data_dict = cognition_data.to_mongo().to_dict()
+        print(f"Verifying integer to float conversion: {data_dict['data']}")  # Improved debug message
+        assert data_dict['data']['item'] == 'Box'
+        assert isinstance(data_dict['data']['price'], float)
+        assert data_dict['data']['price'] == 54.0
+    finally:
+        CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").delete()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_payload_upload_api_with_float_field_value_integer(monkeypatch):
def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "with_float_field_value_integer",
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
payload = {
"data": {"item": "Box","price":54},
"content_type": "json",
"collection": "with_float_field_value_integer"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0
cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(data_dict)
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 54.0
CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").delete()
def test_payload_upload_api_with_float_field_value_integer(monkeypatch):
def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
monkeypatch.setattr(Utility, "get_bot_settings", _mock_get_bot_settings)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "with_float_field_value_integer",
}
try:
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
assert response.status_code == 200
schema_response = response.json()
assert schema_response["success"] is True
assert schema_response["error_code"] == 0
payload = {
"data": {"item": "Box","price":54},
"content_type": "json",
"collection": "with_float_field_value_integer"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
assert response.status_code == 200
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0
cognition_data = CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(f"Verifying integer to float conversion: {data_dict['data']}") # Improved debug message
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 54.0
finally:
CognitionData.objects(bot=pytest.bot, collection="with_float_field_value_integer").delete()


def test_update_payload_upload_api_with_float_field_value_integer(monkeypatch):
def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "update_with_float_field_value_integer",
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)

payload = {
"data": {"item": "Box","price":54.08},
"content_type": "json",
"collection": "update_with_float_field_value_integer"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0

cognition_data = CognitionData.objects(bot=pytest.bot, collection="update_with_float_field_value_integer").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(data_dict)
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 54.08

update_payload = {
"data": {"item": "Box", "price": 27},
"content_type": "json",
"collection": "update_with_float_field_value_integer"
}
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
json=update_payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["message"] == "Record updated!"
assert actual["error_code"] == 0

cognition_data = CognitionData.objects(bot=pytest.bot, collection="update_with_float_field_value_integer").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(data_dict)
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 27.0
CognitionData.objects(bot=pytest.bot, collection="update_with_float_field_value_integer").delete()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve test maintainability and error handling.

The test function needs similar improvements as previous tests, plus:

  1. Consider extracting common setup code to reduce duplication.
  2. Improve or remove debug print statements.
  3. Add validation for schema creation response.

Apply this diff to improve the test:

+@pytest.fixture
+def setup_cognition_schema(monkeypatch):
+    def _mock_get_bot_settings(*args, **kwargs):
+        return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
+    monkeypatch.setattr(Utility, "get_bot_settings", _mock_get_bot_settings)
+    
+    def _setup(collection_name):
+        metadata = {
+            "metadata": [
+                {"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
+                {"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}
+            ],
+            "collection_name": collection_name,
+        }
+        response = client.post(
+            url=f"/api/bot/{pytest.bot}/data/cognition/schema",
+            json=metadata,
+            headers={"Authorization": pytest.token_type + " " + pytest.access_token}
+        )
+        assert response.status_code == 200
+        schema_response = response.json()
+        assert schema_response["success"] is True
+        assert schema_response["error_code"] == 0
+    return _setup

-def test_update_payload_upload_api_with_float_field_value_integer(monkeypatch):
+def test_update_payload_upload_api_with_float_field_value_integer(setup_cognition_schema):
     collection_name = "update_with_float_field_value_integer"
-    def _mock_get_bot_settings(*args, **kwargs):
-        return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
-    metadata = {
-        "metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
-                     {"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
-        "collection_name": collection_name,
-    }
-    response = client.post(
-        url=f"/api/bot/{pytest.bot}/data/cognition/schema",
-        json=metadata,
-        headers={"Authorization": pytest.token_type + " " + pytest.access_token}
-    )
+    try:
+        setup_cognition_schema(collection_name)
 
-    payload = {
-        "data": {"item": "Box","price":54.08},
-        "content_type": "json",
-        "collection": collection_name
-    }
-    response = client.post(
-        url=f"/api/bot/{pytest.bot}/data/cognition",
-        json=payload,
-        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
-    )
-    actual = response.json()
-    pytest.payload_id = actual["data"]["_id"]
-    assert actual["message"] == "Record saved!"
-    assert actual["data"]["_id"]
-    assert actual["error_code"] == 0
+        # Create initial record with float value
+        payload = {
+            "data": {"item": "Box","price":54.08},
+            "content_type": "json",
+            "collection": collection_name
+        }
+        response = client.post(
+            url=f"/api/bot/{pytest.bot}/data/cognition",
+            json=payload,
+            headers={"Authorization": pytest.token_type + " " + pytest.access_token},
+        )
+        assert response.status_code == 200
+        actual = response.json()
+        pytest.payload_id = actual["data"]["_id"]
+        assert actual["message"] == "Record saved!"
+        assert actual["data"]["_id"]
+        assert actual["error_code"] == 0
 
-    cognition_data = CognitionData.objects(bot=pytest.bot, collection=collection_name).first()
-    assert cognition_data is not None
-    data_dict = cognition_data.to_mongo().to_dict()
-    print(data_dict)
-    assert data_dict['data']['item'] == 'Box'
-    assert isinstance(data_dict['data']['price'], float)
-    assert data_dict['data']['price'] == 54.08
+        # Verify initial state
+        cognition_data = CognitionData.objects(bot=pytest.bot, collection=collection_name).first()
+        assert cognition_data is not None
+        data_dict = cognition_data.to_mongo().to_dict()
+        print(f"Initial state - float value: {data_dict['data']}")
+        assert data_dict['data']['item'] == 'Box'
+        assert isinstance(data_dict['data']['price'], float)
+        assert data_dict['data']['price'] == 54.08
 
-    update_payload = {
-        "data": {"item": "Box", "price": 27},
-        "content_type": "json",
-        "collection": collection_name
-    }
-    response = client.put(
-        url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
-        json=update_payload,
-        headers={"Authorization": pytest.token_type + " " + pytest.access_token},
-    )
-    actual = response.json()
-    assert actual["message"] == "Record updated!"
-    assert actual["error_code"] == 0
+        # Update with integer value
+        update_payload = {
+            "data": {"item": "Box", "price": 27},
+            "content_type": "json",
+            "collection": collection_name
+        }
+        response = client.put(
+            url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
+            json=update_payload,
+            headers={"Authorization": pytest.token_type + " " + pytest.access_token},
+        )
+        assert response.status_code == 200
+        actual = response.json()
+        assert actual["message"] == "Record updated!"
+        assert actual["error_code"] == 0
 
-    cognition_data = CognitionData.objects(bot=pytest.bot, collection=collection_name).first()
-    assert cognition_data is not None
-    data_dict = cognition_data.to_mongo().to_dict()
-    print(data_dict)
-    assert data_dict['data']['item'] == 'Box'
-    assert isinstance(data_dict['data']['price'], float)
-    assert data_dict['data']['price'] == 27.0
-    CognitionData.objects(bot=pytest.bot, collection=collection_name).delete()
+        # Verify updated state
+        cognition_data = CognitionData.objects(bot=pytest.bot, collection=collection_name).first()
+        assert cognition_data is not None
+        data_dict = cognition_data.to_mongo().to_dict()
+        print(f"Updated state - integer converted to float: {data_dict['data']}")
+        assert data_dict['data']['item'] == 'Box'
+        assert isinstance(data_dict['data']['price'], float)
+        assert data_dict['data']['price'] == 27.0
+    finally:
+        CognitionData.objects(bot=pytest.bot, collection=collection_name).delete()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_update_payload_upload_api_with_float_field_value_integer(monkeypatch):
def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
metadata = {
"metadata": [{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}],
"collection_name": "update_with_float_field_value_integer",
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
payload = {
"data": {"item": "Box","price":54.08},
"content_type": "json",
"collection": "update_with_float_field_value_integer"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0
cognition_data = CognitionData.objects(bot=pytest.bot, collection="update_with_float_field_value_integer").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(data_dict)
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 54.08
update_payload = {
"data": {"item": "Box", "price": 27},
"content_type": "json",
"collection": "update_with_float_field_value_integer"
}
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
json=update_payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["message"] == "Record updated!"
assert actual["error_code"] == 0
cognition_data = CognitionData.objects(bot=pytest.bot, collection="update_with_float_field_value_integer").first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(data_dict)
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 27.0
CognitionData.objects(bot=pytest.bot, collection="update_with_float_field_value_integer").delete()
@pytest.fixture
def setup_cognition_schema(monkeypatch):
def _mock_get_bot_settings(*args, **kwargs):
return BotSettings(bot=pytest.bot, user="[email protected]", llm_settings=LLMSettings(enable_faq=True), cognition_collections_limit = 20)
monkeypatch.setattr(Utility, "get_bot_settings", _mock_get_bot_settings)
def _setup(collection_name):
metadata = {
"metadata": [
{"column_name": "item", "data_type": "str", "enable_search": True, "create_embeddings": True},
{"column_name": "price", "data_type": "float", "enable_search": True, "create_embeddings": True}
],
"collection_name": collection_name,
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition/schema",
json=metadata,
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
assert response.status_code == 200
schema_response = response.json()
assert schema_response["success"] is True
assert schema_response["error_code"] == 0
return _setup
def test_update_payload_upload_api_with_float_field_value_integer(setup_cognition_schema):
collection_name = "update_with_float_field_value_integer"
try:
setup_cognition_schema(collection_name)
# Create initial record with float value
payload = {
"data": {"item": "Box","price":54.08},
"content_type": "json",
"collection": collection_name
}
response = client.post(
url=f"/api/bot/{pytest.bot}/data/cognition",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
assert response.status_code == 200
actual = response.json()
pytest.payload_id = actual["data"]["_id"]
assert actual["message"] == "Record saved!"
assert actual["data"]["_id"]
assert actual["error_code"] == 0
# Verify initial state
cognition_data = CognitionData.objects(bot=pytest.bot, collection=collection_name).first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(f"Initial state - float value: {data_dict['data']}")
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 54.08
# Update with integer value
update_payload = {
"data": {"item": "Box", "price": 27},
"content_type": "json",
"collection": collection_name
}
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
json=update_payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
assert response.status_code == 200
actual = response.json()
assert actual["message"] == "Record updated!"
assert actual["error_code"] == 0
# Verify updated state
cognition_data = CognitionData.objects(bot=pytest.bot, collection=collection_name).first()
assert cognition_data is not None
data_dict = cognition_data.to_mongo().to_dict()
print(f"Updated state - integer converted to float: {data_dict['data']}")
assert data_dict['data']['item'] == 'Box'
assert isinstance(data_dict['data']['price'], float)
assert data_dict['data']['price'] == 27.0
finally:
CognitionData.objects(bot=pytest.bot, collection=collection_name).delete()

Loading