File size: 1,891 Bytes
732b14f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Export / photo path resolution."""

from __future__ import annotations

from pathlib import Path

import pytest

from app.storage.photo_paths import report_photo_relpath, resolve_report_photo_path


def test_report_photo_relpath() -> None:
    rel = report_photo_relpath("tenant_a", "rep-1", "E1", "pid-1", "jpg")
    assert rel == "tenant_a/report_photos/rep-1/E1/pid-1.jpg"


def test_resolve_photo_path_absolute(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr("app.storage.photo_paths.settings.upload_dir", tmp_path / "uploads")
    img = tmp_path / "photo.jpg"
    img.write_bytes(b"fake")
    assert resolve_report_photo_path(str(img)) == str(img.resolve())


def test_resolve_photo_path_under_upload_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    upload = tmp_path / "uploads"
    monkeypatch.setattr("app.storage.photo_paths.settings.upload_dir", upload)
    rel = report_photo_relpath("tenant", "r1", "E1", "x", "jpg")
    img = upload / rel
    img.parent.mkdir(parents=True)
    img.write_bytes(b"fake")
    assert resolve_report_photo_path(rel) == str(img.resolve())


def test_resolve_photo_path_by_photo_id(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    upload = tmp_path / "uploads"
    monkeypatch.setattr("app.storage.photo_paths.settings.upload_dir", upload)
    rel = report_photo_relpath("tenant", "r1", "E1", "photo-uuid", "png")
    img = upload / rel
    img.parent.mkdir(parents=True)
    img.write_bytes(b"fake")
    assert (
        resolve_report_photo_path(
            "/old/container/abs/path.png",
            tenant_id="tenant",
            report_id="r1",
            section_code="E1",
            photo_id="photo-uuid",
        )
        == str(img.resolve())
    )


def test_resolve_photo_path_missing() -> None:
    assert resolve_report_photo_path("/nonexistent/photo.jpg") is None