File size: 2,418 Bytes
892fa81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""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