"""What this module covers: the FHIR R4 measure routes of `webapi.fhir` for CaCu and CaMa: every `Measure` of the domain and one `MeasureReport` of each, checked against FHIR's models (`fhir.resources`, R4B), and the patient routes absent outside TEA. Fixtures: `domain_client`, a Flask app with only the domain's FHIR blueprint over its fixture database loaded through the cycle up to `event_models` (`conftest.base_de`); needs `DBT_MANIFEST`. """ import os from pathlib import Path import pytest from fhir.resources.R4B import get_fhir_model_class from flask import Flask from conftest import base_de from webapi.fhir import fhir_blueprint UNTIL_MODELS = ("catalogs", "load", "views", "registry", "definitions", "event_models") @pytest.fixture(scope="module", params=["cacu", "cama"]) def domain_client(request, tmp_path_factory): manifest = os.environ.get("DBT_MANIFEST") if not manifest: pytest.skip("DBT_MANIFEST isn't set: `dbt compile` in Transformation writes it") base = base_de(request.param, tmp_path_factory.mktemp(request.param)) base.run(*UNTIL_MODELS, allow_undecided=True) app = Flask(__name__) app.register_blueprint(fhir_blueprint(request.param, base.dominio, Path(manifest), with_patients=True), url_prefix=f"/{request.param}/fhir") yield request.param, app.test_client() base.motor.dispose() def valid(resource: dict) -> dict: get_fhir_model_class(resource["resourceType"]).model_validate(resource) for item in resource.get("entry", ()): get_fhir_model_class(item["resource"]["resourceType"]).model_validate(item["resource"]) return resource def test_every_measure_and_one_report_of_each_are_valid_fhir(domain_client): """Pins down: each Measure of the domain, and its report for 2025-S1, validate as FHIR R4 resources, compositions with strata included. Fails if the serialization of a scoring, a stratifier or a population breaks FHIR's shape.""" domain, client = domain_client measures = valid(client.get(f"/{domain}/fhir/Measure").get_json()) assert measures["total"] == {"cacu": 7, "cama": 8}[domain] for item in measures["entry"]: response = client.get(f"/{domain}/fhir/Measure/{item['resource']['id']}/$evaluate-measure?periodo=2025-S1") assert response.status_code in (200, 409), response.get_json() valid(response.get_json()) def test_the_patient_routes_are_tea_s_only(domain_client): """Pins down: `EpisodeOfCare` exists only on TEA's endpoint; elsewhere it's an OperationOutcome 404. Fails if the patient routes register for another domain.""" domain, client = domain_client response = client.get(f"/{domain}/fhir/EpisodeOfCare") assert response.status_code == 404 and valid(response.get_json())["resourceType"] == "OperationOutcome" def test_a_measure_carries_its_technical_sheet(domain_client): """Pins down: what a metric's `meta` writes for the technical sheet reaches its Measure: `ficha` and `figuras_detalle` as extensions, `referencias` as `relatedArtifact` and `descripcion_poblaciones` as the populations' descriptions. Fails if `measure_resource` stops reading one of them, or CACU_EU7's or CAMA_BIOPSIAS' `meta` loses it.""" domain, client = domain_client measure_id = {"cacu": "CACU-EU7", "cama": "CAMA-BIOPSIAS"}[domain] measure = client.get(f"/{domain}/fhir/Measure/{measure_id}").get_json() by_name = {} for extension in measure["extension"]: by_name.setdefault(extension["url"].rsplit("/", 1)[-1], []).append(extension) sheet = {e["url"]: e["valueString"] for e in by_name["ficha"][0]["extension"]} assert sheet["para"] == "Médico" and sheet["pregunta"].startswith("¿Cómo se distribuyen") if domain == "cacu": assert measure["relatedArtifact"] == [{ "type": "citation", "display": "European Guidelines for QA in Cervical Cancer Screening, 2ª ed. (2008)", "citation": "indicador 7"}] texts = {e["extension"][0]["valueString"]: e["extension"][1:] for e in by_name["figura-detalle"]} assert set(texts) == {"fig_c2_barras", "fig_c2_donas"} assert [e["url"] for e in texts["fig_c2_donas"]][:2] == ["descripcion", "como-leer"] population = measure["group"][0]["population"][0] assert population["description"].startswith("Pacientes con al menos una citología tomada en el periodo.")