"""Unit tests for cores.metadata — unified EXIF/XMP/IPTC extraction.""" from __future__ import annotations import io import pytest from PIL import Image from cores.metadata import extract_all, extract_exif, extract_gps, gps_to_coords class TestExtractAll: def test_empty_bytes_returns_error(self): result = extract_all(b"") assert result["error"] is not None assert "No image bytes" in result["error"] def test_invalid_bytes_returns_error(self): result = extract_all(b"not an image") assert result["error"] is not None def test_valid_jpeg_no_exif(self, sample_image_bytes): result = extract_all(sample_image_bytes) assert result["error"] is None assert result["format"] == "JPEG" assert result["exif"] == {} assert result["gps"] == {} assert result["gps_coords"] is None assert result["camera_make"] is None def test_returns_exif_dict(self, sample_image_bytes): result = extract_all(sample_image_bytes) assert isinstance(result["exif"], dict) class TestExtractExif: def test_returns_exif_only(self, sample_image_bytes): exif = extract_exif(sample_image_bytes) assert isinstance(exif, dict) # Synthetic JPEG has no EXIF assert exif == {} class TestExtractGps: def test_no_gps_returns_none(self, sample_image_bytes): assert extract_gps(sample_image_bytes) is None class TestGpsToCoords: def test_valid_north_east(self): gps = { "GPSLatitude": [(48, 1), (51, 1), (24, 1)], # 48°51'24" "GPSLatitudeRef": "N", "GPSLongitude": [(2, 1), (17, 1), (40, 1)], # 2°17'40" "GPSLongitudeRef": "E", } coords = gps_to_coords(gps) assert coords is not None assert coords["lat"] > 48.0 assert coords["lon"] > 2.0 def test_south_west(self): gps = { "GPSLatitude": [(33, 1), (0, 1), (0, 1)], "GPSLatitudeRef": "S", "GPSLongitude": [(71, 1), (0, 1), (0, 1)], "GPSLongitudeRef": "W", } coords = gps_to_coords(gps) assert coords is not None assert coords["lat"] < 0 assert coords["lon"] < 0 def test_invalid_gps_returns_none(self): assert gps_to_coords({}) is None assert gps_to_coords({"GPSLatitude": "invalid"}) is None