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

Conversation

himanshugt16
Copy link
Contributor

@himanshugt16 himanshugt16 commented Dec 12, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced validation logic for metadata and payloads in the data processing workflow.
    • Improved error handling with clearer messages for invalid data types.
  • Bug Fixes

    • Ensured that validated data is accurately reflected in the payload during save and update operations.
  • Tests

    • Added new tests for the payload upload API focusing on float fields, including creation and update scenarios, to improve test coverage.

Copy link

coderabbitai bot commented Dec 12, 2024

Walkthrough

The pull request introduces enhancements to the CognitionDataProcessor class in kairon/shared/cognition/processor.py, specifically improving the validation logic for metadata and payloads. Key modifications include updates to the validate_metadata_and_payload, save_cognition_data, and update_cognition_data methods to ensure validated data is accurately reflected in the payload. Additionally, the validate_column_values method now raises specific exceptions for invalid data types. New test functions are added in tests/integration_test/services_test.py to enhance test coverage for handling float fields in the payload upload API.

Changes

File Path Change Summary
kairon/shared/cognition/processor.py - Enhanced CognitionDataProcessor validation logic.
- Updated method signatures for validate_metadata_and_payload, save_cognition_data, and update_cognition_data.
- Improved error handling in validate_column_values.
tests/integration_test/services_test.py - Added three new test functions for payload upload API focusing on float fields and integer conversions.

Possibly related PRs

Suggested reviewers

  • hiteshghuge
  • sfahad1414

🐇 In the code, we hop and play,
Validations brightening our day.
With floats and integers, we ensure,
Data integrity, strong and pure!
Tests added, our coverage grows,
In the realm of code, the rabbit knows! 🐰


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dd20324 and 3e21ba3.

📒 Files selected for processing (1)
  • tests/integration_test/services_test.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/integration_test/services_test.py

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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:

  1. Validation for response status code (e.g., assert response.status_code == 200).
  2. Negative test cases for invalid float values (e.g., strings, NaN, Infinity).
  3. 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:

  1. Test with very large/small float values to verify precision handling.
  2. Test with scientific notation (e.g., 1.23e-4).
  3. Test with floating-point arithmetic results (e.g., 0.1 + 0.2).
  4. 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

📥 Commits

Reviewing files that changed from the base of the PR and between ebcdd78 and 909aa25.

📒 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

Copy link

@coderabbitai coderabbitai bot left a 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:

  1. Negative test cases:

    • Invalid float values (e.g., "not_a_float")
    • Out of range values
    • NaN or Infinity
  2. Boundary value test cases:

    • Very large/small float values
    • Values with many decimal places
    • Zero and negative values
  3. 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?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 909aa25 and dd20324.

📒 Files selected for processing (1)
  • tests/integration_test/services_test.py (1 hunks)

Comment on lines 29687 to 29722
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()

Comment on lines 29724 to 29761
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()

Comment on lines 29763 to 29822
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()

Copy link
Collaborator

@hiteshghuge hiteshghuge left a comment

Choose a reason for hiding this comment

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

reviewed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants