-
Notifications
You must be signed in to change notification settings - Fork 79
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
Knowledge_vault_floating_point_fix #1643
Conversation
WalkthroughThe pull request introduces enhancements to the Changes
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (6)
tests/integration_test/services_test.py (6)
29687-29726
: Consider enhancing test coverage for float field validation.While the test covers the basic flow, consider adding:
- Validation for response status code (e.g.,
assert response.status_code == 200
).- Negative test cases for invalid float values (e.g., strings, NaN, Infinity).
- Add a print statement for data_dict to aid debugging, similar to other tests.
29763-29763
: Remove debug print statement.The print statement should be removed or replaced with proper logging for production code.
- print(data_dict) + logging.debug(f"Cognition data: {data_dict}")
29728-29770
: Consider refactoring common setup code.The bot settings setup code is duplicated across all test functions. Consider moving it to a fixture or setup method.
Example:
@pytest.fixture def setup_bot_settings(): bot_settings = BotSettings.objects(bot=pytest.bot).get() bot_settings.cognition_collections_limit = 20 bot_settings.llm_settings['enable_faq'] = True bot_settings.save() yield bot_settings = BotSettings.objects(bot=pytest.bot).get() bot_settings.llm_settings['enable_faq'] = False bot_settings.save()
29808-29808
: Remove debug print statements.Multiple debug print statements should be removed or replaced with proper logging.
- print(data_dict) + logging.debug(f"Cognition data before update: {data_dict}")- print(data_dict) + logging.debug(f"Cognition data after update: {data_dict}")Also applies to: 29832-29832
29813-29815
: Remove excessive blank lines.Multiple consecutive blank lines should be reduced to one or two at most.
29687-29839
: Consider adding more test cases for comprehensive coverage.While the current tests cover basic scenarios, consider adding:
- Test with very large/small float values to verify precision handling.
- Test with scientific notation (e.g., 1.23e-4).
- Test with floating-point arithmetic results (e.g., 0.1 + 0.2).
- Test error cases:
- Invalid float strings ("abc", "12.34.56")
- Special values (NaN, Infinity)
- Out of range values
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
kairon/shared/cognition/processor.py
(3 hunks)tests/integration_test/services_test.py
(1 hunks)
🔇 Additional comments (4)
kairon/shared/cognition/processor.py (3)
271-274
: Correctly validating and updating data based on metadata
The code appropriately iterates over the metadata and updates the data
dictionary with validated column values. This ensures that the data conforms to the expected schema before further processing.
295-295
: Validating and updating payload data in save_cognition_data
The addition of payload['data'] = CognitionDataProcessor.validate_metadata_and_payload(bot, payload)
ensures that the payload data is validated and any necessary transformations are applied before saving. This enhances data integrity.
322-322
: Validating and updating payload data in update_cognition_data
By validating and updating payload['data']
when the content type is JSON, the code ensures that any updates to the cognition data adhere to the defined schema, maintaining consistency and preventing invalid data from being saved.
tests/integration_test/services_test.py (1)
29687-29839
: Tests effectively validate the floating-point fix.
The test suite provides good coverage of the floating-point handling improvements:
- Validates direct float value storage
- Verifies integer to float conversion
- Confirms type preservation during updates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (1)
tests/integration_test/services_test.py (1)
29687-29822
: Consider adding more test cases for comprehensive coverage.The current test suite covers basic float field handling, but consider adding these test cases for better coverage:
Negative test cases:
- Invalid float values (e.g., "not_a_float")
- Out of range values
- NaN or Infinity
Boundary value test cases:
- Very large/small float values
- Values with many decimal places
- Zero and negative values
Format variations:
- Scientific notation (e.g., 1.23e-4)
- Different decimal separators
- Leading/trailing zeros
Would you like me to provide example implementations for these additional test cases?
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() |
There was a problem hiding this comment.
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:
- The mock function is defined but not patched using
monkeypatch
. - Missing cleanup in case of test failure.
- 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.
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() |
There was a problem hiding this comment.
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:
- Remove or improve the debug print statement.
- 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.
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() |
There was a problem hiding this comment.
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:
- Consider extracting common setup code to reduce duplication.
- Improve or remove debug print statements.
- 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.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewed
Summary by CodeRabbit
New Features
Bug Fixes
Tests