test(ocr): add resilience tests for tiny image and unexpected exception propagation
Add test for 1×1 image (sub-tile-size) resilience and narrow preprocess_page fallback from except Exception to (cv2.error, ValueError, MemoryError) so programming errors propagate instead of being silently swallowed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit was merged in pull request #255.
This commit is contained in:
@@ -48,17 +48,35 @@ class TestPreprocessPage:
|
||||
|
||||
assert l_mean_after > l_mean_before
|
||||
|
||||
def test_does_not_crash_on_sub_tile_size_image(self):
|
||||
"""A 1×1 image is smaller than the CLAHE tile (8×8) in both axes.
|
||||
preprocess_page must not raise — it either succeeds or falls back silently."""
|
||||
from preprocessing import preprocess_page
|
||||
|
||||
img = Image.new("RGB", (1, 1), color=(128, 100, 80))
|
||||
result = preprocess_page(img)
|
||||
|
||||
assert isinstance(result, Image.Image)
|
||||
|
||||
def test_falls_back_to_pixel_identical_original_on_cv2_error(self):
|
||||
"""When cv2 raises, preprocess_page must return the unmodified original image."""
|
||||
"""When cv2 raises a known error, preprocess_page returns the unmodified original image."""
|
||||
from preprocessing import preprocess_page
|
||||
|
||||
img = Image.new("RGB", (80, 60), color=(123, 45, 67))
|
||||
original_pixels = list(img.getdata())
|
||||
|
||||
with patch("preprocessing.cv2") as mock_cv2:
|
||||
mock_cv2.cvtColor.side_effect = RuntimeError("cv2 exploded")
|
||||
|
||||
with patch("preprocessing.cv2.cvtColor", side_effect=ValueError("bad input")):
|
||||
result = preprocess_page(img)
|
||||
|
||||
result_pixels = list(result.getdata())
|
||||
assert result_pixels == original_pixels
|
||||
|
||||
def test_unexpected_exception_propagates(self):
|
||||
"""A RuntimeError (programming error) must propagate — not be swallowed by the cv2 fallback."""
|
||||
from preprocessing import preprocess_page
|
||||
|
||||
img = Image.new("RGB", (80, 60))
|
||||
|
||||
with patch("preprocessing.cv2.cvtColor", side_effect=RuntimeError("unexpected")):
|
||||
with pytest.raises(RuntimeError, match="unexpected"):
|
||||
preprocess_page(img)
|
||||
|
||||
Reference in New Issue
Block a user