-
Notifications
You must be signed in to change notification settings - Fork 35
Fix Issue(title=...) TypeError in test_field_officer.py and restore detection.py startup fix #588
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,96 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||||||||
| from unittest.mock import MagicMock | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # Mock heavy optional dependencies before importing backend.main | ||||||||||||||||||||||||||||||||||||||||||||||||
| for _mock_module in [ | ||||||||||||||||||||||||||||||||||||||||||||||||
| "google", "google.generativeai", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "magic", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "telegram", "telegram.ext", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "anthropic", "openai", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "cv2", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "numpy", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "sklearn", "sklearn.cluster", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "transformers", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "torch", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "PIL", "PIL.Image", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "speech_recognition", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "googletrans", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "langdetect", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "pydub", | ||||||||||||||||||||||||||||||||||||||||||||||||
| ]: | ||||||||||||||||||||||||||||||||||||||||||||||||
| sys.modules.setdefault(_mock_module, MagicMock()) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Module-level Prompt for AI agents
Comment on lines
+4
to
+21
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||
| os.environ.setdefault("FRONTEND_URL", "http://localhost:5173") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||||||||||||
| from fastapi.testclient import TestClient | ||||||||||||||||||||||||||||||||||||||||||||||||
| from sqlalchemy import create_engine | ||||||||||||||||||||||||||||||||||||||||||||||||
| from sqlalchemy.orm import sessionmaker | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| from backend.database import Base, get_db | ||||||||||||||||||||||||||||||||||||||||||||||||
| from backend.main import app | ||||||||||||||||||||||||||||||||||||||||||||||||
| from backend.models import FieldOfficerVisit, Issue | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # Use an in-memory SQLite database for tests | ||||||||||||||||||||||||||||||||||||||||||||||||
| TEST_DATABASE_URL = "sqlite://" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| engine = create_engine(TEST_DATABASE_URL, connect_args={"check_same_thread": False}) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+38
|
||||||||||||||||||||||||||||||||||||||||||||||||
| from backend.database import Base, get_db | |
| from backend.main import app | |
| from backend.models import FieldOfficerVisit, Issue | |
| # Use an in-memory SQLite database for tests | |
| TEST_DATABASE_URL = "sqlite://" | |
| engine = create_engine(TEST_DATABASE_URL, connect_args={"check_same_thread": False}) | |
| from sqlalchemy.pool import StaticPool | |
| from backend.database import Base, get_db | |
| from backend.main import app | |
| from backend.models import FieldOfficerVisit, Issue | |
| # Use an in-memory SQLite database for tests | |
| TEST_DATABASE_URL = "sqlite+pysqlite:///:memory:" | |
| engine = create_engine( | |
| TEST_DATABASE_URL, | |
| connect_args={"check_same_thread": False}, | |
| poolclass=StaticPool, | |
| ) |
Copilot
AI
Mar 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
override_get_db yields the db_session fixture’s Session instance, which is created in the test thread but then used inside the FastAPI request handler running in the TestClient thread. SQLAlchemy Sessions are not thread-safe, so this can cause intermittent failures. Prefer creating a new Session inside override_get_db (per request) and closing it there; with a StaticPool in-memory engine you can still share the same DB state across sessions for the test.
Copilot
AI
Mar 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app.dependency_overrides.clear() removes all overrides on the global app, which can interfere with other tests if they set overrides too. It’s safer to remove only the key you added (e.g., app.dependency_overrides.pop(get_db, None)) in the fixture teardown.
| app.dependency_overrides.clear() | |
| app.dependency_overrides.pop(get_db, None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the
httpx.AsyncClientannotation avoids the runtimeNameError, but it also drops useful typing. Consider keeping the type safely by importinghttpxin aTYPE_CHECKINGblock and using a forward reference (e.g.,client: "httpx.AsyncClient" = Depends(...)), or just importinghttpxin this module if that’s acceptable.