The suite keeps only logic tests
Check API validation and response shaping through direct calls. Checks: ruff check, ruff format --check, lint-imports (two contracts kept), and pytest (115 passed, 7 warnings in 42.52s). removed WebApi/tests/test_bundles.py::test_export_and_import_through_the_api — slow integration (logic now covered by test_layout_and_manifest_hold_every_file_hash and test_roundtrip_registers_candidates_with_lineage) removed WebApi/tests/test_bundles.py::test_import_refuses_a_wrong_sha_and_a_non_zip — slow integration (logic now covered by test_import_rejects_invalid_uploads) removed WebApi/tests/test_bundles.py::test_importing_the_same_bundle_again_fails_the_run — slow integration (logic now covered by test_importing_twice_is_a_conflict_and_partial_imports_skip) removed WebApi/tests/test_bundles.py::test_export_guard_failure_is_422 — slow integration (logic now covered by test_reference_group_size and test_selection_champion_latest_all) removed WebApi/tests/test_bundles.py::test_an_untrusted_bundle_is_refused — slow integration (logic now covered by test_import_rejects_invalid_uploads and test_import_refuses_an_untrusted_digest) removed WebApi/tests/test_cacu.py — parity-only removed WebApi/tests/test_cacu.py::test_cacu_from_the_hospital_to_indicators_and_risks — parity-only removed WebApi/tests/test_cama.py — parity-only removed WebApi/tests/test_cama.py::test_cama_from_the_hospital_to_indicators — parity-only removed WebApi/tests/test_conectar.py — slow integration (logic now covered by test_person_ids_survive_a_review_merge_and_split and test_a_concept_map_onto_a_code_no_field_allows_is_refused) removed WebApi/tests/test_conectar.py::test_an_operator_connects_a_site_and_settles_its_review — slow integration (logic now covered by test_person_ids_survive_a_review_merge_and_split and test_a_concept_map_onto_a_code_no_field_allows_is_refused) removed WebApi/tests/test_fhir_door.py — slow integration (logic now covered by test_fhir_in_joins_encounter_results_and_subject_by_full_url and tests/test_core_tea_fhir.py for FHIR mappings) removed WebApi/tests/test_fhir_door.py::test_what_one_platform_serves_another_places_through_the_fhir_door — slow integration (logic now covered by test_fhir_in_joins_encounter_results_and_subject_by_full_url and tests/test_core_tea_fhir.py for FHIR mappings) removed WebApi/tests/test_fhir_door.py::test_a_new_label_is_a_concept_map_row_not_code — slow integration (logic now covered by test_hospital_codes_and_dates_become_platform_values) removed WebApi/tests/test_governance.py::test_version_lifecycle — slow integration (logic now covered by models/tests/test_bundle.py::test_version_lifecycle) removed WebApi/tests/test_governance.py::test_imported_lineage — slow integration (logic now covered by test_roundtrip_registers_candidates_with_lineage) removed WebApi/tests/test_governance.py::test_preview — slow integration (logic now covered by test_the_job_deletes_its_files_and_returns_the_result_as_output and test_shape_outputs) removed WebApi/tests/test_governance.py::test_training_inputs — slow integration (logic now covered by the direct-call test_training_inputs) removed WebApi/tests/test_runs.py::test_unknown_run_is_404 — implementation-detail removed WebApi/tests/test_runs.py::test_failed_run_reports_its_error — slow integration (logic now covered by test_shape_failure_message_is_the_ops_own_exception) removed WebApi/tests/test_runs.py::test_history_filters_by_job_and_domain — implementation-detail removed WebApi/tests/test_runs.py::test_run_requests_validate_the_execution_choice — slow integration (logic now covered by test_resolve_rejects_unlisted_scheduler_and_scheduler_in_local_mode) removed WebApi/tests/test_runs.py::test_execution_options_and_health — implementation-detail removed WebApi/tests/test_runs.py::test_tea_eegs_analyze_validates_scope — slow integration (logic now covered by test_scope_validation) removed WebApi/tests/test_runs.py::test_a_locked_run_storage_is_read_again — implementation-detail removed WebApi/tests/test_runs.py::test_cancel_waits_for_a_starting_run — implementation-detail removed WebApi/tests/test_runs.py::test_cancel_a_finished_run_is_409 — slow integration (logic now covered by test_cancel_a_finished_run_is_409 with a client double) removed WebApi/tests/test_runs_concurrency.py — slow integration (logic now covered by test_cancel_returns_the_terminal_or_cancelling_state for the queued transition) removed WebApi/tests/test_runs_concurrency.py::test_writing_runs_never_overlap — implementation-detail removed WebApi/tests/test_runs_concurrency.py::test_cancelling_an_ingest_run_leaves_no_silver_or_gold — slow integration (logic now covered by test_cancel_returns_the_terminal_or_cancelling_state for the API state transition) removed WebApi/tests/test_runs_concurrency.py::test_cancelling_a_queued_run_never_starts_it — slow integration (logic now covered by test_cancel_returns_the_terminal_or_cancelling_state for the queued transition) removed WebApi/tests/test_runs_concurrency.py::test_a_dead_run_does_not_stay_running — implementation-detail removed WebApi/tests/test_synthea.py — slow integration (logic now covered by test_fhir_in_joins_encounter_results_and_subject_by_full_url and test_hospital_codes_and_dates_become_platform_values) removed WebApi/tests/test_synthea.py::test_a_synthea_population_comes_in_through_the_fhir_door — slow integration (logic now covered by test_fhir_in_joins_encounter_results_and_subject_by_full_url and test_hospital_codes_and_dates_become_platform_values) removed WebApi/tests/test_synthea_cama.py — slow integration (logic now covered by test_fhir_in_joins_encounter_results_and_subject_by_full_url for FHIR ingestion mapping) removed WebApi/tests/test_synthea_cama.py::test_a_synthea_cama_population_comes_in_through_the_fhir_door — slow integration (logic now covered by test_fhir_in_joins_encounter_results_and_subject_by_full_url for FHIR ingestion mapping) removed WebApi/tests/test_tea.py — parity-only removed WebApi/tests/test_tea.py::test_tea_from_the_hospital_to_indicators — parity-only removed WebApi/tests/test_training_config.py::test_tea_training_config_schema — slow integration (logic now covered by the direct-call test_tea_training_config_schema) removed WebApi/tests/test_training_config.py::test_other_domains_have_no_reference — slow integration (logic now covered by the direct-call test_other_domains_have_no_reference) removed WebApi/tests/test_training_config.py::test_train_validates_the_reference — slow integration (logic now covered by the direct-call test_train_validates_the_reference and test_bad_references_fail_with_a_reason) removed WebApi/tests/test_webapp.py — slow integration (logic now covered by existing WebApp unit tests for figure, row and export builders; loaded-page browser coverage is deferred to P5.5) removed WebApi/tests/test_webapp.py::test_every_page_loads_from_the_api — slow integration (logic now covered by existing WebApp unit tests for figure, row and export builders; loaded-page browser coverage is deferred to P5.5)
This commit is contained in:
+30
-122
@@ -1,129 +1,37 @@
|
||||
import hashlib
|
||||
import time
|
||||
from pathlib import Path
|
||||
from io import BytesIO
|
||||
from zipfile import ZipFile
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException, UploadFile
|
||||
|
||||
from models.tea.jobs import Baseline
|
||||
from persistance import bundle, config, registry
|
||||
|
||||
NAME = "tea.curve_x"
|
||||
from core.domains import Domain
|
||||
from persistance import config
|
||||
from webapi.models import import_models
|
||||
|
||||
|
||||
def _register(platform, monkeypatch, n_reference=10):
|
||||
with monkeypatch.context() as local:
|
||||
local.setenv("MLFLOW_TRACKING_URI", platform.env["MLFLOW_TRACKING_URI"])
|
||||
local.chdir(platform.env["ISSSTELEON_DATA"])
|
||||
version = registry.register(
|
||||
NAME,
|
||||
Baseline("x", {"value": {}}),
|
||||
{},
|
||||
{"n_reference": n_reference},
|
||||
{"technical_validation": {}, "interpretability": {}},
|
||||
)
|
||||
registry.add_report(NAME, version, "clinical_validation", "review.txt", b"approved")
|
||||
registry.promote(NAME, version)
|
||||
@pytest.mark.parametrize(
|
||||
("kind", "status", "detail"),
|
||||
[
|
||||
("wrong_sha", 422, "not the one given"),
|
||||
("non_zip", 422, "not a bundle"),
|
||||
("untrusted", 403, "not trusted on this server"),
|
||||
],
|
||||
)
|
||||
def test_import_rejects_invalid_uploads(tmp_path, monkeypatch, kind, status, detail):
|
||||
monkeypatch.setattr(config, "DATA_DIR", tmp_path)
|
||||
if kind == "non_zip":
|
||||
data = b"hello"
|
||||
else:
|
||||
buffer = BytesIO()
|
||||
with ZipFile(buffer, "w") as archive:
|
||||
archive.writestr("manifest.json", "{}")
|
||||
data = buffer.getvalue()
|
||||
digest = hashlib.sha256(data).hexdigest()
|
||||
sent = "0" * 64 if kind == "wrong_sha" else digest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def exported(api, platform, monkeypatch):
|
||||
_register(platform, monkeypatch)
|
||||
response = api.get("/models/tea/export")
|
||||
assert response.status_code == 200, response.text
|
||||
return response
|
||||
|
||||
|
||||
def _trust(platform, sha, monkeypatch):
|
||||
with monkeypatch.context() as local:
|
||||
local.setattr(config, "DATA_DIR", Path(platform.env["ISSSTELEON_DATA"]))
|
||||
bundle.trusted_file().write_text(f"{sha}\n")
|
||||
|
||||
|
||||
def _post(api, data, sha):
|
||||
return api.post(
|
||||
"/models/tea/import", params={"sha256": sha}, files={"file": ("bundle.zip", data)}
|
||||
)
|
||||
|
||||
|
||||
def _run(api, response):
|
||||
assert response.status_code == 200, response.text
|
||||
run_id = response.json()["run_id"]
|
||||
deadline = time.monotonic() + 120
|
||||
while True:
|
||||
response = api.get(f"/runs/{run_id}")
|
||||
assert response.status_code == 200, response.text
|
||||
run = response.json()
|
||||
if run["state"] in {"succeeded", "failed", "cancelled"}:
|
||||
return run
|
||||
assert time.monotonic() < deadline, run
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def test_export_and_import_through_the_api(exported, fresh_api, fresh_platform, monkeypatch):
|
||||
sha = hashlib.sha256(exported.content).hexdigest()
|
||||
assert exported.headers["content-type"] == "application/zip"
|
||||
assert exported.headers["x-bundle-sha256"] == sha
|
||||
assert exported.headers["x-bundle-versions"] == "tea.curve_x/1"
|
||||
assert (
|
||||
exported.headers["content-disposition"]
|
||||
== f'attachment; filename="tea-models-{sha[:12]}.zip"'
|
||||
)
|
||||
_trust(fresh_platform, sha, monkeypatch)
|
||||
run = _run(fresh_api, _post(fresh_api, exported.content, sha.upper()))
|
||||
assert run["state"] == "succeeded", run
|
||||
response = fresh_api.get("/models/tea/curve_x/versions")
|
||||
assert response.status_code == 200, response.text
|
||||
(version,) = response.json()
|
||||
assert version["version"] == 1
|
||||
assert version["metrics"]["n_reference"] == 10
|
||||
assert version["reports"] == sorted(registry.REPORTS)
|
||||
assert version["aliases"] == []
|
||||
|
||||
|
||||
def test_import_refuses_a_wrong_sha_and_a_non_zip(api, exported):
|
||||
wrong = _post(api, exported.content, "0" * 64)
|
||||
assert wrong.status_code == 422
|
||||
assert wrong.json()["detail"] == (
|
||||
f"the file's SHA-256 is {exported.headers['x-bundle-sha256']}, not the one given"
|
||||
)
|
||||
non_zip = _post(api, b"hello", hashlib.sha256(b"hello").hexdigest())
|
||||
assert non_zip.status_code == 422
|
||||
assert non_zip.json()["detail"] == "not a bundle"
|
||||
|
||||
|
||||
def test_importing_the_same_bundle_again_fails_the_run(
|
||||
exported,
|
||||
fresh_api,
|
||||
fresh_platform,
|
||||
monkeypatch,
|
||||
):
|
||||
sha = exported.headers["x-bundle-sha256"]
|
||||
_trust(fresh_platform, sha, monkeypatch)
|
||||
first = _run(fresh_api, _post(fresh_api, exported.content, sha))
|
||||
assert first["state"] == "succeeded", first
|
||||
second = _run(fresh_api, _post(fresh_api, exported.content, sha))
|
||||
assert second["state"] == "failed", second
|
||||
assert "already registered" in second["error"]
|
||||
|
||||
|
||||
def test_export_guard_failure_is_422(api, platform, monkeypatch):
|
||||
assert api.get("/models/cacu/export").status_code == 409
|
||||
_register(platform, monkeypatch, n_reference=3)
|
||||
response = api.get("/models/tea/export")
|
||||
assert response.status_code == 422
|
||||
assert "reference group of 3 children is under 5" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_an_untrusted_bundle_is_refused(api, platform, exported):
|
||||
sha = exported.headers["x-bundle-sha256"]
|
||||
response = _post(api, exported.content, sha)
|
||||
assert response.status_code == 403
|
||||
directory = Path(platform.env["ISSSTELEON_DATA"]) / "bundles"
|
||||
assert response.json()["detail"] == (
|
||||
f"bundle {sha} is not trusted on this server: "
|
||||
f"the operator adds its SHA-256 to {directory / 'trusted'}"
|
||||
)
|
||||
assert not (directory / f"{sha}.zip").exists()
|
||||
history = api.get("/runs", params={"job": "import_bundle"})
|
||||
assert history.status_code == 200, history.text
|
||||
assert history.json() == []
|
||||
with pytest.raises(HTTPException) as rejected:
|
||||
import_models(Domain.TEA, sent, UploadFile(BytesIO(data), filename="bundle.zip"))
|
||||
assert rejected.value.status_code == status
|
||||
assert detail in rejected.value.detail
|
||||
assert not (tmp_path / "bundles" / f"{digest}.zip").exists()
|
||||
|
||||
Reference in New Issue
Block a user