#!/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.")