test_plate_detection_failure_reason.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. """A plate check that could not run says why instead of reporting an empty plate."""
  2. from unittest.mock import AsyncMock, patch
  3. import pytest
  4. from backend.app.services import plate_detection
  5. from backend.app.services.plate_detection import PlateDetectionResult, check_plate_empty
  6. @pytest.mark.asyncio
  7. async def test_no_camera_frame():
  8. with (
  9. patch.object(plate_detection, "OPENCV_AVAILABLE", True),
  10. patch.object(plate_detection, "capture_camera_image", AsyncMock(return_value=(None, None))),
  11. ):
  12. result = await check_plate_empty(printer_id=1, ip_address="10.0.0.1", access_code="x", model="X1C")
  13. assert result.failure_reason == "no_camera_frame"
  14. assert result.to_dict()["failure_reason"] == "no_camera_frame"
  15. @pytest.mark.asyncio
  16. async def test_no_opencv():
  17. with patch.object(plate_detection, "OPENCV_AVAILABLE", False):
  18. result = await check_plate_empty(printer_id=1, ip_address="10.0.0.1", access_code="x", model="X1C")
  19. assert result.failure_reason == "opencv_missing"
  20. def test_a_check_that_ran_has_no_failure_reason():
  21. result = PlateDetectionResult(is_empty=True, confidence=0.9, difference_percent=1.0, message="ok")
  22. assert result.to_dict()["failure_reason"] is None