Skip to content

Commit 03c3a2d

Browse files
authored
Merge pull request #54 from trustyai-explainability/main
[sync] upstream main to midstream incubation
2 parents 2a803d7 + fd4591f commit 03c3a2d

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

detectors/common/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fastapi==0.112.0
1+
fastapi==0.121.2
22
uvicorn==0.30.5
33
httpx==0.27.0
44
prometheus_client >= 0.18.0
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Integration tests for HF detector FastAPI lifespan context
3+
"""
4+
5+
import os
6+
import sys
7+
import pytest
8+
from fastapi.testclient import TestClient
9+
10+
# Set up paths and MODEL_DIR before app import
11+
_current_dir = os.path.dirname(__file__)
12+
_tests_dir = os.path.dirname(os.path.dirname(_current_dir))
13+
_project_root = os.path.dirname(_tests_dir)
14+
_detectors_path = os.path.join(_project_root, "detectors")
15+
_huggingface_path = os.path.join(_detectors_path, "huggingface")
16+
17+
for path in [_huggingface_path, _detectors_path, _project_root]:
18+
if path not in sys.path:
19+
sys.path.insert(0, path)
20+
21+
os.environ["MODEL_DIR"] = os.path.join(
22+
_tests_dir, "dummy_models", "bert/BertForSequenceClassification"
23+
)
24+
25+
from app import app # noqa: E402
26+
27+
28+
class TestLifespanIntegration:
29+
"""Test FastAPI lifespan context for HF detector."""
30+
31+
@pytest.fixture
32+
def client(self):
33+
"""Create test client with lifespan-initialized detector."""
34+
with TestClient(app) as test_client:
35+
yield test_client
36+
37+
def test_lifespan_loads_detector(self, client):
38+
"""Verify lifespan initializes detector on startup."""
39+
detectors = app.get_all_detectors()
40+
assert len(detectors) > 0
41+
detector = list(detectors.values())[0]
42+
assert detector.model is not None
43+
assert detector.tokenizer is not None
44+
45+
def test_lifespan_handles_requests(self, client):
46+
"""Verify requests work through lifespan-initialized app."""
47+
response = client.post(
48+
"/api/v1/text/contents",
49+
json={"contents": ["Test message"], "detector_params": {}},
50+
)
51+
assert response.status_code == 200
52+
assert isinstance(response.json(), list)
53+
54+
def test_multiple_requests(self, client):
55+
"""Verify detector handles multiple requests without state leakage."""
56+
for i in range(10):
57+
response = client.post(
58+
"/api/v1/text/contents",
59+
json={"contents": [f"Request {i}"], "detector_params": {}},
60+
)
61+
assert response.status_code == 200
62+
assert isinstance(response.json(), list)
63+
64+
def test_lifespan_cleanup(self, client):
65+
"""Verify lifespan cleanup runs on shutdown."""
66+
assert len(app.get_all_detectors()) > 0
67+
68+
client.__exit__(None, None, None)
69+
70+
assert len(app.get_all_detectors()) == 0

0 commit comments

Comments
 (0)