test: Add comprehensive backend unit test suite (Resolves #63)#104
test: Add comprehensive backend unit test suite (Resolves #63)#104shamsulalam1114 wants to merge 2 commits intoINCF:mainfrom
Conversation
- Added conftest with sys.modules mocking for offline testing - Added tests for main API endpoints, retrieval logic, and agents - Fixed VertexRetriever is_enabled abstract property implementation
There was a problem hiding this comment.
Pull request overview
Adds an initial pytest-based unit test suite for the FastAPI backend (addressing the lack of tests in #63) and fixes VertexRetriever.is_enabled to be a proper property implementation.
Changes:
- Introduces backend unit tests for core API routes, agent helpers, and retriever behavior.
- Adds a pytest
conftest.pyto provide a FastAPITestClientfixture and to isolate tests via environment/module mocking. - Fixes
VertexRetrieverto track enablement via a backing field (_is_enabled) and expose it via a@property.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/tests/conftest.py | Test scaffolding: sys.path adjustment, dependency/env mocking, and test_client fixture |
| backend/tests/test_main.py | Tests for root, health endpoints, chat endpoint, and session reset |
| backend/tests/test_retrieval.py | Tests for LocalRetriever, VertexRetriever init (mocked), and get_retriever fallback |
| backend/tests/test_agents.py | Tests for _is_more_query, fuse_results, and NeuroscienceAssistant.reset_session |
| backend/retrieval.py | Fixes VertexRetriever.is_enabled to be a property backed by _is_enabled |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for mod in mocked_modules: | ||
| sys.modules[mod] = MagicMock() | ||
|
|
||
| # agents.py imports END from langgraph.graph | ||
| sys.modules['langgraph.graph'].END = "END" | ||
|
|
There was a problem hiding this comment.
I tried moving this to a monkeypatch fixture as suggested, but because these heavy dependencies (langgraph, torch, google-cloud-aiplatform) are intentionally not installed in the testing environment, pytest crashes with ModuleNotFoundError during the initial collection phase before the fixture even executes. Leaving the mock at the module level is required so pytest can successfully collect the test files without those dependencies installed.
| # Add the backend directory to sys.path so tests can import from it | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | ||
|
|
There was a problem hiding this comment.
Thanks for catching the typo in the PR description! You are correct that pip install -e .[dev] should be run from the repository root, not the backend directory. However, I have intentionally kept the sys.path.insert in conftest.py as a fallback. This ensures the test suite still runs flawlessly even if a developer hasn't installed the package in editable mode.
| @@ -0,0 +1,58 @@ | |||
| import pytest | |||
There was a problem hiding this comment.
Done! I have removed the unused imports in the latest commit.
| import pytest | ||
| from unittest.mock import patch, AsyncMock, MagicMock | ||
|
|
There was a problem hiding this comment.
Done! I have removed the unused imports in the latest commit.
| @@ -0,0 +1,44 @@ | |||
| import pytest | |||
There was a problem hiding this comment.
Done! I have removed the unused imports in the latest commit.
Resolves #63
Description
This PR introduces a comprehensive, offline-first unit test suite for the FastAPI backend using
pytest.Changes Made:
conftest.pywhich isolates tests from live environments by mocking GCP credentials and heavy modules (torch,langgraph,google-cloud-aiplatform) viasys.modules. This ensures the test suite runs lightning-fast and safely on any developer's local machine without requiring API keys.test_main.pyto validate health checks, routing, and proper session logic via mocked endpoints.test_retrieval.pyto validateLocalRetrieverfallbacks and mockedVertexRetrieverinitializations.VertexRetrieverwhere the abstractis_enabledproperty was being instantiated as an attribute instead of a property getter.test_agents.pyto validate thefuse_resultsscoring logic and session state resets.Testing Instructions
From the
backenddirectory, run:pip install -e .[dev]pytest tests/ -vAll 11 tests pass successfully.