File size: 3,825 Bytes
f505798
 
922e525
 
f505798
922e525
f505798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
922e525
 
f505798
 
 
 
 
922e525
 
f505798
 
 
922e525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f505798
 
 
 
 
 
 
4b6e3ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from pathlib import Path

import pytest

from src.domain.models import Validation
from src.repositories.append_only_validation_repository import AppendOnlyValidationRepository, OptimisticLockError


def test_save_validation_appends_event_and_updates_snapshot(tmp_path: Path) -> None:
    repo = AppendOnlyValidationRepository(base_dir=str(tmp_path))

    first = Validation(
        detection_key="0000000000002001",
        status="positive",
        corrected_species=None,
        notes="first",
        validator="validator-a",
    )
    second = Validation(
        detection_key="0000000000002001",
        status="negative",
        corrected_species="Species B",
        notes="updated",
        validator="validator-b",
    )

    first_version = repo.save_validation(project_slug="demo-project", item=first, expected_version=0)
    second_version = repo.save_validation(project_slug="demo-project", item=second, expected_version=1)

    events = repo.list_events("demo-project")
    snapshot = repo.load_current_snapshot("demo-project")

    assert len(events) == 2
    assert first_version == 1
    assert second_version == 2
    assert events[0]["detection_key"] == "0000000000002001"
    assert snapshot["0000000000002001"]["status"] == "negative"
    assert snapshot["0000000000002001"]["validator"] == "validator-b"
    assert snapshot["0000000000002001"]["version"] == 2


def test_save_validation_raises_conflict_on_stale_version(tmp_path: Path) -> None:
    repo = AppendOnlyValidationRepository(base_dir=str(tmp_path))
    item = Validation(
        detection_key="0000000000002002",
        status="positive",
        corrected_species=None,
        notes="first",
        validator="validator-a",
    )

    _ = repo.save_validation(project_slug="demo-project", item=item, expected_version=0)

    with pytest.raises(OptimisticLockError):
        _ = repo.save_validation(project_slug="demo-project", item=item, expected_version=0)


def test_repository_returns_empty_for_missing_project(tmp_path: Path) -> None:
    repo = AppendOnlyValidationRepository(base_dir=str(tmp_path))

    assert repo.list_events("missing-project") == []
    assert repo.load_current_snapshot("missing-project") == {}


def test_current_snapshot_has_backup_after_updates(tmp_path: Path) -> None:
    repo = AppendOnlyValidationRepository(base_dir=str(tmp_path))
    item = Validation(
        detection_key="0000000000002003",
        status="positive",
        corrected_species=None,
        notes="first",
        validator="validator-a",
    )

    repo.save_validation(project_slug="demo-project", item=item, expected_version=0)
    repo.save_validation(
        project_slug="demo-project",
        item=Validation(
            detection_key="0000000000002003",
            status="negative",
            corrected_species=None,
            notes="second",
            validator="validator-a",
        ),
        expected_version=1,
    )

    backup_dir = tmp_path / "demo-project" / "validations" / ".backups"
    assert list(backup_dir.glob("current.json.*.bak"))


def test_current_snapshot_recovers_from_append_only_events_when_corrupt(tmp_path: Path) -> None:
    repo = AppendOnlyValidationRepository(base_dir=str(tmp_path))
    item = Validation(
        detection_key="0000000000002004",
        status="positive",
        corrected_species=None,
        notes="first",
        validator="validator-a",
    )
    repo.save_validation(project_slug="demo-project", item=item, expected_version=0)
    current_file = tmp_path / "demo-project" / "validations" / "current.json"
    current_file.write_text("{bad json", encoding="utf-8")

    snapshot = repo.load_current_snapshot("demo-project")

    assert snapshot["0000000000002004"]["status"] == "positive"
    assert snapshot["0000000000002004"]["version"] == 1