Skip to content

Commit 99c0be8

Browse files
replace assertions with errors & add invalid detector name tests
1 parent 39e21b6 commit 99c0be8

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

detectors/built_in/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def detect_content(request: ContentAnalysisHttpRequest):
3232
for content in request.contents:
3333
message_detections = []
3434
for detector_kind, detector_registry in app.get_all_detectors().items():
35-
assert isinstance(detector_registry, BaseDetectorRegistry), f"Detector {detector_kind} is not a valid BaseDetectorRegistry"
35+
if not isinstance(detector_registry, BaseDetectorRegistry):
36+
raise TypeError(f"Detector {detector_kind} is not a valid BaseDetectorRegistry")
3637
if detector_kind in request.detector_params:
3738
try:
3839
message_detections += detector_registry.handle_request(content, request.detector_params)
@@ -48,7 +49,8 @@ def detect_content(request: ContentAnalysisHttpRequest):
4849
def get_registry():
4950
result = {}
5051
for detector_type, detector_registry in app.get_all_detectors().items():
51-
assert isinstance(detector_registry, BaseDetectorRegistry), f"Detector {detector_type} is not a valid BaseDetectorRegistry"
52+
if not isinstance(detector_registry, BaseDetectorRegistry):
53+
raise TypeError(f"Detector {detector_type} is not a valid BaseDetectorRegistry")
5254
result[detector_type] = {}
5355
for detector_name, detector_fn in detector_registry.get_registry().items():
5456
result[detector_type][detector_name] = detector_fn.__doc__

detectors/llm_judge/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def detector_unary_handler(
4949
"""Analyze content using LLM-as-Judge evaluation."""
5050
detector: LLMJudgeDetector = app.get_detector()
5151
if not detector:
52-
raise HTTPException(status_code=404, detail="Detector not found")
52+
raise HTTPException(status_code=503, detail="Detector not found")
5353
return ContentsAnalysisResponse(root=await detector.run(request))
5454

5555

@@ -65,7 +65,7 @@ async def list_metrics():
6565
"""List all available evaluation metrics."""
6666
detector: LLMJudgeDetector = app.get_detector()
6767
if not detector:
68-
raise HTTPException(status_code=404, detail="Detector not found")
68+
raise HTTPException(status_code=503, detail="Detector not found")
6969

7070
metrics = detector.list_available_metrics()
7171
return MetricsListResponse(metrics=metrics, total=len(metrics))

tests/detectors/builtIn/test_filetype.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,43 @@ def test_multiple_filetype_valid_and_invalid(self, client: TestClient):
284284
assert not any(d["detection"] == "invalid_yaml" for d in detections[5])
285285

286286
# 7: invalid yaml
287-
assert any(d["detection"] == "invalid_yaml" for d in detections[6])
287+
assert any(d["detection"] == "invalid_yaml" for d in detections[6])
288+
289+
290+
# === ERROR HANDLING & INVALID DETECTOR TYPES =================================================
291+
def test_unregistered_detector_kind_ignored(self, client: TestClient):
292+
"""Test that requesting an unregistered detector kind is silently ignored"""
293+
payload = {
294+
"contents": ['{"a": 1}'],
295+
"detector_params": {"nonexistent_detector": ["some_value"]}
296+
}
297+
resp = client.post("/api/v1/text/contents", json=payload)
298+
assert resp.status_code == 200
299+
# Should return empty list since nonexistent_detector is not registered
300+
assert resp.json()[0] == []
301+
302+
def test_mixed_valid_invalid_detector_kinds(self, client: TestClient):
303+
"""Test mixing valid and invalid detector kinds"""
304+
payload = {
305+
"contents": ['{a: 1, b: 2}'],
306+
"detector_params": {
307+
"file_type": ["json"], # valid detector kind
308+
"nonexistent_detector": ["some_value"] # invalid detector kind
309+
}
310+
}
311+
resp = client.post("/api/v1/text/contents", json=payload)
312+
assert resp.status_code == 200
313+
detections = resp.json()[0]
314+
# Should only process the valid detector kind
315+
assert detections[0]["detection"] == "invalid_json"
316+
317+
def test_empty_detector_params(self, client: TestClient):
318+
"""Test with empty detector_params"""
319+
payload = {
320+
"contents": ['{"a": 1}'],
321+
"detector_params": {}
322+
}
323+
resp = client.post("/api/v1/text/contents", json=payload)
324+
assert resp.status_code == 200
325+
# Should return empty list since no detectors are specified
326+
assert resp.json()[0] == []

tests/detectors/builtIn/test_regex.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,41 @@ def test_single_detector(self, client):
198198
assert any("[email protected]" in d["text"] for d in results[0])
199199

200200

201+
# === ERROR HANDLING & INVALID DETECTOR TYPES =================================================
202+
def test_unregistered_detector_kind_ignored(self, client):
203+
"""Test that requesting an unregistered detector kind is silently ignored"""
204+
payload = {
205+
"contents": ["[email protected]"],
206+
"detector_params": {"nonexistent_detector": ["some_value"]}
207+
}
208+
resp = client.post("/api/v1/text/contents", json=payload)
209+
assert resp.status_code == 200
210+
# Should return empty list since nonexistent_detector is not registered
211+
assert resp.json()[0] == []
212+
213+
def test_mixed_valid_invalid_detector_kinds(self, client):
214+
"""Test mixing valid and invalid detector kinds"""
215+
payload = {
216+
"contents": ["Contact me at [email protected]"],
217+
"detector_params": {
218+
"regex": ["email"], # valid detector kind
219+
"nonexistent_detector": ["some_value"] # invalid detector kind
220+
}
221+
}
222+
resp = client.post("/api/v1/text/contents", json=payload)
223+
assert resp.status_code == 200
224+
detections = resp.json()[0]
225+
# Should only process the valid detector kind
226+
assert detections[0]["text"] == "[email protected]"
227+
228+
def test_empty_detector_params(self, client):
229+
"""Test with empty detector_params"""
230+
payload = {
231+
"contents": ["[email protected]"],
232+
"detector_params": {}
233+
}
234+
resp = client.post("/api/v1/text/contents", json=payload)
235+
assert resp.status_code == 200
236+
# Should return empty list since no detectors are specified
237+
assert resp.json()[0] == []
201238

0 commit comments

Comments
 (0)