45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import base64
|
|
|
|
import pytest
|
|
|
|
from app.detector import MockDetector, abnormal_probability, detection_status
|
|
|
|
# 1x1 透明 PNG
|
|
TINY_PNG = base64.b64decode(
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
|
|
)
|
|
|
|
|
|
def test_mock_detector_returns_canned_detection():
|
|
det = MockDetector(class_name="sick", confidence=0.92)
|
|
result = det.detect(TINY_PNG)
|
|
assert len(result) == 1
|
|
assert result[0]["class_name"] == "sick"
|
|
assert result[0]["confidence"] == 0.92
|
|
assert result[0]["bbox"]["w"] > 0
|
|
|
|
|
|
def test_mock_detector_rejects_invalid_image():
|
|
det = MockDetector()
|
|
with pytest.raises(ValueError):
|
|
det.detect(b"not an image")
|
|
|
|
|
|
def test_abnormal_probability_ignores_healthy_and_unknown():
|
|
detections = [
|
|
{"class_name": "healthy", "confidence": 0.95},
|
|
{"class_name": "unknown", "confidence": 0.8},
|
|
{"class_name": "sick", "confidence": 0.72},
|
|
]
|
|
assert abnormal_probability(detections, ("healthy", "sick")) == 0.72
|
|
|
|
|
|
def test_detection_status_returns_unknown_for_empty_or_unknown():
|
|
assert detection_status([], ("healthy", "sick")) == "unknown"
|
|
assert detection_status([{"class_name": "unknown", "confidence": 0.8}], ("healthy", "sick")) == "unknown"
|
|
assert detection_status([{"class_name": "healthy", "confidence": 0.95}], ("healthy", "sick")) == "healthy"
|
|
assert detection_status(
|
|
[{"class_name": "white_muscardine", "confidence": 0.7}],
|
|
("healthy", "white_muscardine", "nuclear_polyhedrosis"),
|
|
) == "abnormal"
|