Files
rowing_stats/tests/test_image_io.py
T
adamfandClaude Opus 5.5 3017cac31b Add tests for parsing, image loading, display detection and CLI
Tesseract is stubbed out except in one end-to-end smoke test on a sample
photo, which is skipped when the binary is not installed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-09 11:46:19 +01:00

71 lines
2.0 KiB
Python

from datetime import datetime
import numpy as np
import pytest
from PIL import Image
from rowing_stats.errors import ImageLoadError
from rowing_stats.image_io import (
EXIF_DATETIME_TAG,
get_datetime_taken,
load_image,
pil_to_cv,
)
def test_load_image_missing_file(tmp_path):
with pytest.raises(ImageLoadError, match="not found"):
load_image(tmp_path / "nope.jpeg")
def test_load_image_not_an_image(tmp_path):
path = tmp_path / "notes.jpeg"
path.write_text("this is not a JPEG")
with pytest.raises(ImageLoadError, match="not a recognised image"):
load_image(path)
def test_load_image_truncated_file(tmp_path):
whole = tmp_path / "whole.jpeg"
Image.new("RGB", (64, 64), "white").save(whole)
truncated = tmp_path / "truncated.jpeg"
truncated.write_bytes(whole.read_bytes()[:200])
with pytest.raises(ImageLoadError, match="could not read"):
load_image(truncated)
def test_load_image_directory(tmp_path):
with pytest.raises(ImageLoadError):
load_image(tmp_path)
def test_pil_to_cv_swaps_channels_to_bgr():
arr = pil_to_cv(Image.new("RGB", (2, 3), (255, 0, 0)))
assert arr.shape == (3, 2, 3)
assert arr[0, 0].tolist() == [0, 0, 255]
def test_pil_to_cv_handles_greyscale_and_alpha():
assert pil_to_cv(Image.new("L", (2, 2))).shape == (2, 2, 3)
assert pil_to_cv(Image.new("RGBA", (2, 2))).shape == (2, 2, 3)
assert pil_to_cv(Image.new("L", (2, 2))).dtype == np.uint8
def _with_exif_datetime(value: str) -> Image.Image:
img = Image.new("RGB", (2, 2))
img.getexif()[EXIF_DATETIME_TAG] = value
return img
def test_get_datetime_taken():
img = _with_exif_datetime("2025:11:15 22:16:05")
assert get_datetime_taken(img) == datetime(2025, 11, 15, 22, 16, 5)
def test_get_datetime_taken_missing():
assert get_datetime_taken(Image.new("RGB", (2, 2))) is None
def test_get_datetime_taken_malformed():
assert get_datetime_taken(_with_exif_datetime("yesterday")) is None