"""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": 14, "cama": 11}[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"