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

Components name validation #1785

Conversation

maheshsattala
Copy link
Contributor

@maheshsattala maheshsattala commented Feb 5, 2025

Added validations for all components names and added and fixed test cases for the same.

Summary by CodeRabbit

  • New Features

    • Enhanced input validations across various configuration areas to ensure names consist only of allowed characters. Users will now receive clear error notifications when entering names with unsupported special characters, leading to more consistent and error-free operations.
  • Bug Fixes

    • Introduced validation checks to prevent the addition of entities with invalid names, ensuring improved error handling and user experience.
  • Tests

    • Added new test functions to validate error handling for invalid names across various entity types, enhancing overall test coverage.

Copy link

coderabbitai bot commented Feb 5, 2025

Walkthrough

The pull request introduces validation checks in the MongoProcessor class to ensure that names for intents, actions, slots, synonyms, lookups, and regex patterns consist only of letters, numbers, and underscores. Corresponding tests are added to verify that an AppException is raised when invalid names are provided, enhancing the robustness of the code.

Changes

File Change Summary
kairon/shared/.../processor.py Added validation checks in methods (add_intent, add_http_action_config, add_pyscript_action, add_db_action, add_hubspot_forms_action, add_google_search_action, add_zendesk_action, add_pipedrive_action, add_slot, add_callback_action, add_schedule_action, add_two_stage_fallback_action, add_synonym, add_lookup, add_regex) to enforce naming rules.
tests/unit_test/.../data_processor2_test.py Added tests for invalid names in methods for intents, utterances, slots, lookups, synonyms, regex, and various actions, asserting that an AppException is raised with the correct error message.

Possibly related PRs

  • Agentic flow imp #1780: This PR introduces validation checks for various entity names in the MongoProcessor class, directly related to the new test functions added to validate the behavior of the MongoProcessor when handling invalid names.

Suggested reviewers

  • hiteshghuge
  • sfahad1414

Poem

I'm a hopping coder with a twitch in my ear,
Adding checks so our names stay clear.
No pesky symbols allowed in our code trail,
Validation errors now never fail.
With neat names in place, our code will soar—
A bunny's delight forevermore!

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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. (Beta)
  • @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

🧹 Nitpick comments (3)
tests/unit_test/data_processor/data_processor2_test.py (2)

51-580: Refactor tests to reduce code duplication and improve maintainability.

Consider the following improvements:

  1. Extract common setup code into fixtures
  2. Use pytest's parametrize to combine similar test cases

Here's an example of how to refactor the tests:

import pytest
from pytest import fixture

@fixture
def processor():
    return MongoProcessor()

@fixture
def bot():
    return 'test_bot'

@fixture
def user():
    return 'test_user'

@pytest.mark.parametrize("invalid_name", [
    "test-name",
    "test.name",
    "test@name",
    "test#name",
    "test/name",
    "test>name",
    "test name",
    "test:name",
    "test=name",
    "test~name"
])
def test_add_intent_with_invalid_name(processor, bot, user, invalid_name):
    with pytest.raises(AppException,
                      match=re.escape("Invalid name! Only letters, numbers, and underscores (_) are allowed.")):
        processor.add_intent(invalid_name, bot, user, is_integration=False)

This approach:

  • Reduces code duplication
  • Makes it easier to add new test cases
  • Improves maintainability

51-580: Add test cases for valid names and edge cases.

The current test suite focuses on invalid names but lacks coverage for:

  1. Valid names (positive test cases)
  2. Edge cases such as empty strings or very long names

Consider adding these test cases:

@pytest.mark.parametrize("valid_name", [
    "test_name",
    "test_name_123",
    "TEST_NAME",
    "test123",
    "_test_name"
])
def test_add_intent_with_valid_name(processor, bot, user, valid_name):
    # Test should pass without raising AppException
    processor.add_intent(valid_name, bot, user, is_integration=False)

@pytest.mark.parametrize("edge_case_name", [
    "",  # Empty string
    "a" * 1000,  # Very long name
    "_",  # Single underscore
    "123",  # Only numbers
])
def test_add_intent_with_edge_case_names(processor, bot, user, edge_case_name):
    # Define expected behavior for each edge case
    # Add appropriate assertions
    pass
kairon/shared/data/processor.py (1)

2531-2533: LGTM! Consider using a constant for the error message.

The validation for special characters in intent names is correctly implemented. However, consider defining the error message "Invalid name! Only letters, numbers, and underscores (_) are allowed." as a constant since it's used across multiple validations.

INVALID_NAME_CHARACTER_ERROR = "Invalid name! Only letters, numbers, and underscores (_) are allowed."
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f9f89a3 and ea669f8.

📒 Files selected for processing (2)
  • kairon/shared/data/processor.py (20 hunks)
  • tests/unit_test/data_processor/data_processor2_test.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Python CI
🔇 Additional comments (1)
kairon/shared/data/processor.py (1)

4193-4195: LGTM! Consistent validation implementation across components.

The validation for special characters is consistently implemented across all components (HTTP actions, pyscript actions, database actions, Google search actions, Zendesk actions, slots, synonyms, lookups, Pipedrive actions, Hubspot forms actions, callback actions, Razorpay actions, and schedule actions). This ensures uniform name validation throughout the system.

Also applies to: 4295-4297, 4421-4423, 4570-4572, 4704-4706, 4762-4764, 5615-5617, 6195-6197, 7126-7128, 7268-7270, 7387-7389, 8146-8148, 8502-8504

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

🧹 Nitpick comments (2)
kairon/shared/data/processor.py (2)

8014-8015: Consider moving regex import to top of file.

The regex import should be moved to the top of the file with other imports for better organization.

-import re

Move this import to the top of the file with other imports.


8037-8037: Consider extracting regex pattern to a constant.

The regex substitution pattern could be extracted to a named constant for better maintainability.

+NAME_VALIDATION_PATTERN = r"[^a-zA-Z0-9_]"

-intent = re.sub(r"[^a-zA-Z0-9_]", "_", intent) if Utility.special_match(intent) else intent
+intent = re.sub(NAME_VALIDATION_PATTERN, "_", intent) if Utility.special_match(intent) else intent
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ea669f8 and 7c74feb.

📒 Files selected for processing (2)
  • kairon/shared/data/processor.py (23 hunks)
  • tests/unit_test/data_processor/data_processor2_test.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/unit_test/data_processor/data_processor2_test.py
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Python CI
  • GitHub Check: Analyze (python)
🔇 Additional comments (3)
kairon/shared/data/processor.py (3)

2531-2533: Consistent validation for intent names.

The validation ensures intent names only contain letters, numbers and underscores.


4193-4195: Consistent validation pattern across all action types.

The validation pattern is consistently applied across all action types (HTTP, Pyscript, Database, Google Search, Hubspot Forms, Razorpay, Callback and Schedule actions) to ensure names only contain letters, numbers and underscores.

Also applies to: 4295-4297, 4421-4423, 4570-4572, 7271-7273, 8151-8153, 8392-8394, 8507-8509


4762-4764: Consistent validation for slot, synonym, lookup and regex names.

The validation ensures consistent naming rules across slots, synonyms, lookups and regex patterns.

Also applies to: 5615-5617, 6195-6197, 6138-6140

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