Skip to content

test: Add comprehensive backend unit test suite (Resolves #63)#104

Open
shamsulalam1114 wants to merge 2 commits intoINCF:mainfrom
shamsulalam1114:main
Open

test: Add comprehensive backend unit test suite (Resolves #63)#104
shamsulalam1114 wants to merge 2 commits intoINCF:mainfrom
shamsulalam1114:main

Conversation

@shamsulalam1114
Copy link
Copy Markdown

Resolves #63

Description

This PR introduces a comprehensive, offline-first unit test suite for the FastAPI backend using pytest.

Changes Made:

  • Test Environment: Added conftest.py which isolates tests from live environments by mocking GCP credentials and heavy modules (torch, langgraph, google-cloud-aiplatform) via sys.modules. This ensures the test suite runs lightning-fast and safely on any developer's local machine without requiring API keys.
  • Endpoints: Added test_main.py to validate health checks, routing, and proper session logic via mocked endpoints.
  • Retrieval: Added test_retrieval.py to validate LocalRetriever fallbacks and mocked VertexRetriever initializations.
  • Bug Fix: Fixed a small bug in VertexRetriever where the abstract is_enabled property was being instantiated as an attribute instead of a property getter.
  • Agents: Added test_agents.py to validate the fuse_results scoring logic and session state resets.

Testing Instructions

From the backend directory, run:
pip install -e .[dev]
pytest tests/ -v

All 11 tests pass successfully.

- 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
Copilot AI review requested due to automatic review settings April 22, 2026 18:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.py to provide a FastAPI TestClient fixture and to isolate tests via environment/module mocking.
  • Fixes VertexRetriever to 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.

Comment thread backend/tests/conftest.py
Comment on lines +25 to +30
for mod in mocked_modules:
sys.modules[mod] = MagicMock()

# agents.py imports END from langgraph.graph
sys.modules['langgraph.graph'].END = "END"

Copy link
Copy Markdown
Author

@shamsulalam1114 shamsulalam1114 Apr 22, 2026

Choose a reason for hiding this comment

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

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.

Comment thread backend/tests/conftest.py
Comment on lines +6 to +8
# 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__), "..")))

Copy link
Copy Markdown
Author

@shamsulalam1114 shamsulalam1114 Apr 22, 2026

Choose a reason for hiding this comment

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

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.

Comment thread backend/tests/test_main.py Outdated
@@ -0,0 +1,58 @@
import pytest
Copy link
Copy Markdown
Author

@shamsulalam1114 shamsulalam1114 Apr 22, 2026

Choose a reason for hiding this comment

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

Done! I have removed the unused imports in the latest commit.

Comment thread backend/tests/test_agents.py Outdated
Comment on lines +1 to +3
import pytest
from unittest.mock import patch, AsyncMock, MagicMock

Copy link
Copy Markdown
Author

@shamsulalam1114 shamsulalam1114 Apr 22, 2026

Choose a reason for hiding this comment

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

Done! I have removed the unused imports in the latest commit.

Comment thread backend/tests/test_retrieval.py Outdated
@@ -0,0 +1,44 @@
import pytest
Copy link
Copy Markdown
Author

@shamsulalam1114 shamsulalam1114 Apr 22, 2026

Choose a reason for hiding this comment

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

Done! I have removed the unused imports in the latest commit.

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.

[Testing] Add unit test suite for backend API endpoints and core modules

2 participants