Skip to content

Conversation

@sahitya-chandra
Copy link
Contributor

@sahitya-chandra sahitya-chandra commented Feb 10, 2026

fixes #91 (follow up pr after #89)

Summary

Adds validation for natural language query input and unit tests to cover the validation logic used by QueryGenerator::generateQuery().

Changes

  • Validation in utils: Introduced validate_natural_language_query() in utils to enforce:
    • Non-empty input (rejects empty and whitespace-only strings)
    • Maximum length (configurable via max_query_length)
  • QueryGenerator integration: QueryGenerator::generateQuery() now calls this validator before proceeding and returns a QueryResult with success = false and a clear error message when validation fails.
  • Unit tests: New tests in tests/unit/test_utils.cpp for:
    • Empty query → "Query cannot be empty."
    • Whitespace-only query → "Query cannot be empty."
    • Query exceeding max length → error message with limit and actual length
    • Valid short query → no error
    • Query exactly at max length → no error

Testing

  • New unit tests added; run make test-setup && ./build_tests/tests/pg_ai_query_tests --gtest_filter="UtilsTest.ValidateNaturalLanguageQuery_*"
Note: Google Test filter = UtilsTest.ValidateNaturalLanguageQuery_*
[==========] Running 5 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 5 tests from UtilsTest
[ RUN      ] UtilsTest.ValidateNaturalLanguageQuery_Empty
[       OK ] UtilsTest.ValidateNaturalLanguageQuery_Empty (0 ms)
[ RUN      ] UtilsTest.ValidateNaturalLanguageQuery_WhitespaceOnly
[       OK ] UtilsTest.ValidateNaturalLanguageQuery_WhitespaceOnly (0 ms)
[ RUN      ] UtilsTest.ValidateNaturalLanguageQuery_TooLong
[       OK ] UtilsTest.ValidateNaturalLanguageQuery_TooLong (0 ms)
[ RUN      ] UtilsTest.ValidateNaturalLanguageQuery_ValidShort
[       OK ] UtilsTest.ValidateNaturalLanguageQuery_ValidShort (0 ms)
[ RUN      ] UtilsTest.ValidateNaturalLanguageQuery_ExactlyMaxLength
[       OK ] UtilsTest.ValidateNaturalLanguageQuery_ExactlyMaxLength (0 ms)
[----------] 5 tests from UtilsTest (1 ms total)

[----------] Global test environment tear-down
[==========] 5 tests from 1 test suite ran. (1 ms total)
[  PASSED  ] 5 tests.

- Added `validate_natural_language_query` function to check for empty, whitespace-only, and overly long queries.
- Updated `QueryGenerator` to utilize the new validation function, returning appropriate error messages.
- Added unit tests for various validation scenarios to ensure correctness.
@sahitya-chandra sahitya-chandra marked this pull request as ready for review February 11, 2026 07:31
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.

Add unit tests for validation in QueryGenerator::generateQuery()

1 participant