File size: 5,626 Bytes
3405d6e
 
 
82df7d3
 
 
 
 
 
 
 
 
3405d6e
 
 
82df7d3
 
3405d6e
 
 
 
82df7d3
 
3405d6e
82df7d3
3405d6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82df7d3
3405d6e
 
 
82df7d3
 
 
 
 
 
 
 
 
 
 
 
 
8bb3be3
82df7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc096c2
 
 
 
82df7d3
 
 
 
 
 
 
 
 
 
3405d6e
 
 
 
 
 
 
 
 
 
 
82df7d3
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
"""Unit test for the annotation acknowledgement gate (`_enforce_release_ack`).

Selecting an annotation version older than the newest one PUBLISHED FOR THAT
(dataset, plan-kind) raises EnvironmentError unless `MedVision_ACK_RELEASE`
equals the repo's release version; the current (or newer) version passes
unconditionally. The gate applies to every task.

Comparing per pair rather than against the release version is what keeps a
release that did not regenerate a dataset from blocking it, while the
acknowledgement value stays the release version so a bump still invalidates
old acknowledgements.

Run:  python scripts/test_tl_ack_gate.py

The shared bootstrap in scripts/_medvision_test_support.py stubs `datasets`
when it is unavailable, so this runs anywhere.
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import _medvision_test_support as _support  # noqa: E402

mv = _support.load_loader("medvision_ack_test_")

enforce = mv._enforce_release_ack
# Arbitrary "latest" for the logic test — the gate compares the selected version
# against whatever latest_version it is handed (at runtime that is
# MedVisionConfig.version), so the test is independent of the real version.
LATEST = "1.1.1"


def _run(planner_version, latest_version, ack):
    """Set/clear the ack env var, call the gate, return True iff it raised."""
    if ack is None:
        os.environ.pop("MedVision_ACK_RELEASE", None)
    else:
        os.environ["MedVision_ACK_RELEASE"] = ack
    try:
        enforce(planner_version, latest_version)
        return False
    except EnvironmentError:
        return True


# (planner_version, latest_version, ack, expect_raise, description)
CASES = [
    (LATEST, LATEST, None, False, "latest version + no ack -> pass"),
    ("1.1.2", LATEST, None, False, "newer-than-latest + no ack -> pass"),
    ("1.1.0", LATEST, None, True, "older version + no ack -> block"),
    ("1.0.0", LATEST, "1", True, "older version + wrong ack -> block"),
    ("1.1.0", LATEST, LATEST, False, "older version + correct ack -> pass"),
]

failures = 0
total = 0
for planner_version, latest_version, ack, expect_raise, desc in CASES:
    got_raise = _run(planner_version, latest_version, ack)
    ok = got_raise == expect_raise
    total += 1
    print(
        f"[{'PASS' if ok else 'FAIL'}] {desc}  "
        f"(raised={got_raise}, expected={expect_raise})"
    )
    if not ok:
        failures += 1


# --- per-(dataset, plan-kind) triggering -----------------------------------
# The gate is handed the newest version published FOR THE PAIR being loaded,
# and acknowledges against the repo release. This is what makes a purely
# additive release non-breaking for datasets it did not touch.
RELEASE = "1.3.0"


def _run_pair(planner_version, dataset_name, kind, ack):
    if ack is None:
        os.environ.pop("MedVision_ACK_RELEASE", None)
    else:
        os.environ["MedVision_ACK_RELEASE"] = ack
    try:
        enforce(
            planner_version,
            mv._newest_declared(dataset_name, kind),
            ack_value=RELEASE,
            dataset_name=dataset_name,
            plan_kind=kind,
        )
        return False
    except EnvironmentError:
        return True


# (planner_version, dataset, kind, ack, expect_raise, description)
PAIR_CASES = [
    ("1.1.1", "ACDC", "segmentation", None, False,
     "pin 1.1.1 on a dataset whose newest is 1.0.0 -> pass (was blocked)"),
    ("1.1.1", "KiTS23", "biometry", None, False,
     "pin 1.1.1 on a pair whose newest is 1.1.1 -> pass (was blocked)"),
    ("1.0.0", "ACDC", "segmentation", None, False,
     "pin 1.0.0 on a never-regenerated pair -> pass"),
    ("1.1.0", "KiTS23", "biometry", None, True,
     "pin 1.1.0 on a pair whose newest is 1.1.1 -> block"),
    ("1.1.0", "KiTS23", "biometry", RELEASE, False,
     "acknowledged with the release value (blanket, composes across a sweep) -> pass"),
    # RE-BASED: this used to assert the pair's newest was REJECTED. Both values are
    # now accepted -- they acknowledge different things and both are legitimate.
    ("1.1.0", "KiTS23", "biometry", "1.1.1", False,
     "acknowledged with the pair's newest (the number the error shows) -> pass"),
    ("1.1.0", "KiTS23", "biometry", "1.1.0", True,
     "the pin itself is not an acknowledgement -> block"),
    ("1.1.0", "KiTS23", "biometry", "1.0.0", True,
     "an unrelated version is not an acknowledgement -> block"),
    ("1.1.0", "ACDC", "segmentation", "1.1.1", False,
     "ACDC's newest is 1.0.0, so 1.1.0 is not behind it -> passes before any ack"),
    # RELEASE rather than a literal: a hardcoded version stops being "the release" at the
    # next bump, and the case then still passes for an unrelated reason (KiTS23's newest
    # biometry annotation is 1.1.1, so any later pin is ahead of it either way).
    (RELEASE, "KiTS23", "biometry", None, False, "pin at release -> pass"),
    ("latest", "KiTS23", "biometry", None, True,
     "the literal 'latest' never reaches the gate; _normalize_requested "
     "resolves it first, so an unresolved value is correctly treated as stale"),
]

print()
for planner_version, dataset_name, kind, ack, expect_raise, desc in PAIR_CASES:
    got_raise = _run_pair(planner_version, dataset_name, kind, ack)
    ok = got_raise == expect_raise
    total += 1
    print(
        f"[{'PASS' if ok else 'FAIL'}] {desc}  "
        f"(raised={got_raise}, expected={expect_raise})"
    )
    if not ok:
        failures += 1

print()
if failures:
    print(f"{failures} case(s) failed.")
    sys.exit(1)
print(f"All {total} cases passed.")