Coverage for src/lumen/fhir/compartment.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-08-31 18:46 +0000

1""" 

2This module handles FHIR compartment membership checks using HL7 definitions. 

3It maps resource attributes to search parameters to check compartment membership. 

4""" 

5 

6from __future__ import annotations 

7 

8import json 

9from functools import cache 

10from pathlib import Path 

11 

12_DATA_DIR = Path(__file__).parent / "data" / "r4b" 

13 

14 

15@cache 

16def compartment_params(compartment: str) -> dict[str, tuple[str, ...]]: 

17 """Return ``{resource_type: (compartment search-param names, ...)}`` for a compartment. 

18 

19 ``compartment`` is a compartment resource type (e.g. ``Patient``, 

20 ``Encounter``). Only resource types that actually belong to the compartment 

21 (have at least one compartment-defining parameter) are included. 

22 """ 

23 path = _DATA_DIR / f"r4b-compartment-{compartment.lower()}.json" 

24 data = json.loads(path.read_text()) 

25 out: dict[str, tuple[str, ...]] = {} 

26 for entry in data.get("resource", []): 

27 params = entry.get("param") 

28 if params: 

29 out[entry["code"]] = tuple(params) 

30 return out