Skip to content

feat: added unit tests for decomposer.py#1402

Open
madsysharma wants to merge 1 commit into
viru0909-dev:mainfrom
madsysharma:test/decomposer
Open

feat: added unit tests for decomposer.py#1402
madsysharma wants to merge 1 commit into
viru0909-dev:mainfrom
madsysharma:test/decomposer

Conversation

@madsysharma

@madsysharma madsysharma commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

feat: Add unit tests for decomposer.py

Closes #852

Description

The layer-1 query decomposer (nlp-orchestrator/decomposer.py) had only a thin smoke test. This PR replaces it with a comprehensive pytest suite that fully isolates the logic from the Groq LPU by mocking the async chat-completion call: no API key and no network are required to run it.

Cases covered:

  • Happy path: parses a JSON array into sub-questions; preserves order and a single-item result; strips surrounding whitespace/newlines.
  • 5-question cap: returns the first 5 when the model emits more; leaves exactly-5 untouched.
  • Domain guardrail: a non-legal query (model returns []) yields an empty list.
  • Fallback branches: every malformed/wrong-shape response falls back to [query]: invalid JSON, ```json markdown fences (the decomposer does not strip them), a JSON object, a JSON scalar string, a list of non-strings, and a mixed-type list.
  • Transport failures: generic Exception, TimeoutError, and RuntimeError from the client all fall back to [query]; the result is always a list.
  • Request construction: asserts the call uses GROQ_MODEL_FAST, temperature=0.3, max_tokens=512, the "user" role, and that the user's query is interpolated into DECOMPOSE_PROMPT.

A clean _mock_completion(content) helper replaces the previous nested type("obj", ...) mock construction. The module sets a dummy GROQ_API_KEY before import so the suite is runnable standalone (the config module calls sys.exit(1) when the key is absent).

Testing

PS C:\Users\madhu\Documents\nyay-setu-working> pytest -v nlp-orchestrator/tests/test_decomposer.py
================================================= test session starts =================================================
platform win32 -- Python 3.11.7, pytest-9.0.3, pluggy-1.6.0 -- C:\Program Files\Python311\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\madhu\Documents\nyay-setu-working
plugins: anyio-4.13.0, deepeval-4.0.5, Faker-40.19.1, langsmith-0.8.8, asyncio-1.4.0, cov-4.1.0, mock-3.15.1, repeat-0.9.4, rerunfailures-16.3, xdist-3.8.0
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 19 items

nlp-orchestrator/tests/test_decomposer.py::test_returns_parsed_sub_questions PASSED                              [  5%]
nlp-orchestrator/tests/test_decomposer.py::test_result_is_list_of_strings PASSED                                 [ 10%]
nlp-orchestrator/tests/test_decomposer.py::test_single_sub_question_preserved PASSED                             [ 15%]
nlp-orchestrator/tests/test_decomposer.py::test_surrounding_whitespace_is_stripped PASSED                        [ 21%]
nlp-orchestrator/tests/test_decomposer.py::test_caps_at_five_sub_questions PASSED                                [ 26%]
nlp-orchestrator/tests/test_decomposer.py::test_exactly_five_sub_questions_unchanged PASSED                      [ 31%]
nlp-orchestrator/tests/test_decomposer.py::test_non_legal_query_returns_empty_list PASSED                        [ 36%]
nlp-orchestrator/tests/test_decomposer.py::test_invalid_json_falls_back_to_original_query PASSED                 [ 42%]
nlp-orchestrator/tests/test_decomposer.py::test_markdown_fenced_json_falls_back PASSED                           [ 47%]
nlp-orchestrator/tests/test_decomposer.py::test_json_object_instead_of_list_falls_back PASSED                    [ 52%]
nlp-orchestrator/tests/test_decomposer.py::test_json_scalar_string_falls_back PASSED                             [ 57%]
nlp-orchestrator/tests/test_decomposer.py::test_list_with_non_string_items_falls_back PASSED                     [ 63%]
nlp-orchestrator/tests/test_decomposer.py::test_list_with_mixed_types_falls_back PASSED                          [ 68%]
nlp-orchestrator/tests/test_decomposer.py::test_api_exception_falls_back_to_original_query PASSED                [ 73%]
nlp-orchestrator/tests/test_decomposer.py::test_timeout_error_falls_back_to_original_query PASSED                [ 78%]
nlp-orchestrator/tests/test_decomposer.py::test_result_is_always_a_list_on_failure PASSED                        [ 84%]
nlp-orchestrator/tests/test_decomposer.py::test_calls_groq_once_with_expected_parameters PASSED                  [ 89%]
nlp-orchestrator/tests/test_decomposer.py::test_user_query_is_embedded_in_prompt PASSED                          [ 94%]
nlp-orchestrator/tests/test_decomposer.py::test_message_uses_user_role PASSED                                    [100%]Running teardown with pytest sessionfinish...


================================================= 19 passed in 0.65s ==================================================

Type of change

  • Tests — adds/expands test coverage (non-breaking)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • Documentation update — N/A (test-only change)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@vercel

vercel Bot commented Jun 15, 2026

Copy link
Copy Markdown

@madsysharma is attempting to deploy a commit to the CodeBlooded's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @madsysharma, thanks for contributing to Nyay Setu! 🎉

I have automatically:

  • 👤 Assigned this PR to you.
  • 🏷️ Applied the gssoc:approved label.

Our workflows will now analyze your changes to classify:

  • 📈 PR Difficulty: level:*
  • 🧩 PR Type: type:*
  • 🌟 PR Quality: quality:*

Tip

Ensure your PR description references the issue it resolves (e.g. Closes #123). This allows the bot to inherit any additional labels from that issue!

Happy coding! 🚀

@madsysharma

Copy link
Copy Markdown
Contributor Author

Hi @viru0909-dev , please review this PR. Thank you.

@viru0909-dev viru0909-dev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Review Summary

This PR has been reviewed and requires changes before it can be approved. The issues below must be resolved.

Issues to Resolve

Result Check Notes
FAIL Title Title should follow the conventional commit format (e.g., 'feat:', 'fix:', 'refactor:') or start with a capital letter.
FAIL Issue Link An issue number is referenced but without a closing keyword. Use 'Closes #NNN', 'Fixes #NNN', or 'Resolves #NNN' so the issue closes automatically on merge.

Passing Checks

Result Check Notes
PASS Description Description is complete and well-structured.
PASS Checklist All 8 checklist item(s) are checked.
PASS Merge Conflicts Conflict status is not yet computed by GitHub — skipping.
PASS Branch Freshness Branch was last updated 0.1 days ago.
PASS CI / Tests No CI checks are configured or results are not yet available.
PASS Code Quality No code quality issues detected in the diff.

To proceed: push the required fixes to test/decomposer and the PR will be re-evaluated on the next review run.

@madsysharma madsysharma changed the title test (nlp): added unit tests for decomposer.py feat: added unit tests for decomposer.py Jun 15, 2026
@madsysharma madsysharma requested a review from viru0909-dev June 15, 2026 17:48
@github-actions github-actions Bot added good first issue Good for newcomers Testing labels Jun 15, 2026
@madsysharma

Copy link
Copy Markdown
Contributor Author

Hi @viru0909-dev , have made the changes. Please review them and let me know if they're good to go. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NLP] Add unit tests for decomposer.py

2 participants