|
1 | | -//! MedCare (healthcare) tenant bridge — locks to the `Healthcare` |
2 | | -//! namespace. The Healthcare namespace itself is reserved and will be |
3 | | -//! populated by a future session (the FMA / SNOMED / RadLex import is the |
4 | | -//! remit of `lance-graph-rdf` in `lance-graph-rdf-fma-snomed-v1`). |
5 | | -
|
6 | | -use crate::bridge::{BridgeFromRegistry, NamespaceBridge}; |
7 | | -use crate::error::{Error, Result}; |
8 | | -use crate::namespace::NamespaceId; |
9 | | -use crate::registry::OntologyRegistry; |
10 | | -use std::sync::Arc; |
11 | | - |
12 | | -pub const NAMESPACE: &str = "Healthcare"; |
13 | | - |
14 | | -pub struct MedcareBridge { |
15 | | - registry: Arc<OntologyRegistry>, |
16 | | - g_lock: NamespaceId, |
17 | | -} |
| 1 | +//! MedCare (healthcare) tenant bridge — now a thin type alias over |
| 2 | +//! [`crate::bridges::unified::UnifiedBridge`] parameterised by |
| 3 | +//! [`ogar_vocab::ports::HealthcarePort`]. |
| 4 | +//! |
| 5 | +//! Before the Healthcare codebook promotion (Northstar T9) this file |
| 6 | +//! carried a bespoke `MedcareBridge` struct + hand-written |
| 7 | +//! `NamespaceBridge` impl — the same boilerplate every per-tenant bridge |
| 8 | +//! cloned. lance-graph#570 collapsed `OpenProjectBridge` / `RedmineBridge` |
| 9 | +//! onto the generic [`UnifiedBridge<P>`] harness but explicitly deferred |
| 10 | +//! `MedcareBridge` "until Healthcare gets promoted into the codebook". |
| 11 | +//! That promotion has now landed: OGAR mints the `0x09XX` Health concepts |
| 12 | +//! and `ports::HealthcarePort` carries the namespace / bridge_id / public- |
| 13 | +//! name → class_id alias table, so this bridge becomes one line. |
| 14 | +//! |
| 15 | +//! The differences between bridges (namespace, bridge_id, alias table) |
| 16 | +//! all come from the OGAR class schema. Codebook public names (`Patient`, |
| 17 | +//! `Diagnosis`, …) synthesize an `EntityRef` whose `entity_type_id()` is |
| 18 | +//! the canonical Health class_id; names outside the alias table fall |
| 19 | +//! through to the registry-resolution path (so a hydrated TTL entity that |
| 20 | +//! is not yet a codebook concept still resolves). The audit / authorization |
| 21 | +//! path uses `row()` (registry-backed, not overridden here), so it is |
| 22 | +//! unaffected by codebook synthesis on `entity()`. |
| 23 | +//! |
| 24 | +//! See `crate::bridges::unified::tests` for the generic-level coverage and |
| 25 | +//! `tests/bridge_scope_lock.rs` for the Healthcare scope-lock pins. |
| 26 | +
|
| 27 | +use crate::bridges::unified::UnifiedBridge; |
| 28 | +// `HealthcarePort::NAMESPACE` / `::aliases()` are `PortSpec` associated |
| 29 | +// items — the trait must be in scope for the resolution to work (codex |
| 30 | +// P1 on PR #570). Same import in the test module below. |
| 31 | +use ogar_vocab::ports::PortSpec; |
| 32 | +pub use ogar_vocab::ports::HealthcarePort; |
| 33 | + |
| 34 | +/// MedCare `NamespaceBridge` — alias over the generic harness, locked to |
| 35 | +/// the `Healthcare` namespace via [`HealthcarePort`]. |
| 36 | +pub type MedcareBridge = UnifiedBridge<HealthcarePort>; |
| 37 | + |
| 38 | +/// Canonical namespace name for MedCare / Healthcare. Mirrors |
| 39 | +/// `HealthcarePort::NAMESPACE` so existing consumers that imported the |
| 40 | +/// constant from this module keep building. |
| 41 | +pub const NAMESPACE: &str = HealthcarePort::NAMESPACE; |
| 42 | + |
| 43 | +#[cfg(test)] |
| 44 | +mod tests { |
| 45 | + //! Co-located unit tests for the migrated alias — constructor |
| 46 | + //! success/failure, contract methods, codebook resolution, fallback |
| 47 | + //! to registry. Mirrors `openproject_bridge::tests`; only the |
| 48 | + //! `MedcareBridge` ident (now `UnifiedBridge<HealthcarePort>`) and the |
| 49 | + //! Healthcare fixtures differ. |
18 | 50 |
|
19 | | -impl MedcareBridge { |
20 | | - pub fn new(registry: Arc<OntologyRegistry>) -> Result<Self> { |
21 | | - let g_lock = registry |
22 | | - .namespace_id(NAMESPACE) |
23 | | - .ok_or_else(|| Error::UnknownNamespace(NAMESPACE.to_string()))?; |
24 | | - Ok(Self { registry, g_lock }) |
| 51 | + use super::*; |
| 52 | + use crate::bridge::{BridgeError, NamespaceBridge}; |
| 53 | + use crate::error::Error; |
| 54 | + use crate::namespace::NamespaceId; |
| 55 | + use crate::namespace_registry::NamespaceRegistry; |
| 56 | + use crate::registry::OntologyRegistry; |
| 57 | + use ogar_vocab::class_ids; |
| 58 | + // PortSpec needed in scope for `HealthcarePort::aliases()`. |
| 59 | + use ogar_vocab::ports::PortSpec; |
| 60 | + use std::fs; |
| 61 | + use std::sync::Arc; |
| 62 | + |
| 63 | + fn registry_with_healthcare() -> Arc<OntologyRegistry> { |
| 64 | + let ttl = r#" |
| 65 | +@prefix ogit: <http://www.purl.org/ogit/> . |
| 66 | +@prefix ogit.Healthcare: <http://www.purl.org/ogit/Healthcare/> . |
| 67 | +@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
| 68 | +
|
| 69 | +ogit.Healthcare:Patient |
| 70 | + a rdfs:Class; |
| 71 | + rdfs:subClassOf ogit:Entity; |
| 72 | + rdfs:label "Patient"; |
| 73 | + ogit:scope "NTO"; |
| 74 | + ogit:parent ogit:Node; |
| 75 | + ogit:mandatory-attributes ( ogit:id ); |
| 76 | + ogit:optional-attributes ( ) ; |
| 77 | +. |
| 78 | +"#; |
| 79 | + let tmp = tempfile::tempdir().unwrap(); |
| 80 | + fs::create_dir_all(tmp.path().join("Healthcare")).unwrap(); |
| 81 | + fs::write(tmp.path().join("Healthcare").join("ents.ttl"), ttl).unwrap(); |
| 82 | + let registry = Arc::new(OntologyRegistry::new_in_memory()); |
| 83 | + registry |
| 84 | + .hydrate_once_sync(tmp.path(), &["Healthcare"]) |
| 85 | + .unwrap(); |
| 86 | + std::mem::forget(tmp); |
| 87 | + registry |
25 | 88 | } |
26 | | -} |
27 | 89 |
|
28 | | -impl NamespaceBridge for MedcareBridge { |
29 | | - fn bridge_id(&self) -> &'static str { |
30 | | - "medcare" |
| 90 | + #[test] |
| 91 | + fn new_succeeds_when_namespace_registered() { |
| 92 | + let registry = registry_with_healthcare(); |
| 93 | + let bridge = MedcareBridge::new(registry).unwrap(); |
| 94 | + assert_ne!(bridge.g_lock(), NamespaceId::UNKNOWN); |
31 | 95 | } |
32 | | - fn registry(&self) -> &OntologyRegistry { |
33 | | - &self.registry |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn new_returns_unknown_namespace_when_not_registered() { |
| 99 | + let registry = Arc::new(OntologyRegistry::new_in_memory()); |
| 100 | + // `unwrap_err()` would require `MedcareBridge: Debug`, which the |
| 101 | + // underlying `UnifiedBridge<P>` intentionally doesn't impl. |
| 102 | + match MedcareBridge::new(registry) { |
| 103 | + Ok(_) => panic!("expected UnknownNamespace, got Ok(_)"), |
| 104 | + Err(Error::UnknownNamespace(name)) => assert_eq!(name, "Healthcare"), |
| 105 | + Err(other) => panic!("expected UnknownNamespace, got {other:?}"), |
| 106 | + } |
34 | 107 | } |
35 | | - fn g_lock(&self) -> NamespaceId { |
36 | | - self.g_lock |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn bridge_id_is_lowercase_medcare() { |
| 111 | + let bridge = MedcareBridge::new(registry_with_healthcare()).unwrap(); |
| 112 | + assert_eq!(bridge.bridge_id(), "medcare"); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn g_lock_matches_registry_namespace_id() { |
| 117 | + let registry = registry_with_healthcare(); |
| 118 | + let expected = registry.namespace_id(NAMESPACE).unwrap(); |
| 119 | + let bridge = MedcareBridge::new(registry).unwrap(); |
| 120 | + assert_eq!(bridge.g_lock(), expected); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn entity_resolves_patient_to_canonical_class_id() { |
| 125 | + let bridge = MedcareBridge::new(registry_with_healthcare()).unwrap(); |
| 126 | + let entity = bridge.entity("Patient").unwrap(); |
| 127 | + assert_eq!(entity.schema_ptr.entity_type_id(), class_ids::PATIENT); |
| 128 | + assert_eq!(entity.schema_ptr.entity_type_id(), 0x0901); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn entity_synthesised_schema_ptr_stamps_seeded_context_id() { |
| 133 | + let bridge = MedcareBridge::new(registry_with_healthcare()).unwrap(); |
| 134 | + let entity = bridge.entity("Patient").unwrap(); |
| 135 | + let expected = NamespaceRegistry::seed_context_id("Healthcare").unwrap(); |
| 136 | + assert_eq!(entity.schema_ptr.ontology_context_id(), expected); |
| 137 | + assert_eq!(entity.schema_ptr.ontology_context_id(), 2); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn entity_for_each_codebook_entry_returns_its_canonical_class_id() { |
| 142 | + let bridge = MedcareBridge::new(registry_with_healthcare()).unwrap(); |
| 143 | + for &(public_name, expected_id) in HealthcarePort::aliases() { |
| 144 | + let entity = bridge.entity(public_name).unwrap_or_else(|e| { |
| 145 | + panic!("codebook entry `{public_name}` failed to resolve: {e:?}") |
| 146 | + }); |
| 147 | + assert_eq!( |
| 148 | + entity.schema_ptr.entity_type_id(), |
| 149 | + expected_id, |
| 150 | + "codebook entry `{public_name}` should resolve to 0x{expected_id:04X}", |
| 151 | + ); |
| 152 | + } |
37 | 153 | } |
38 | | -} |
39 | 154 |
|
40 | | -impl BridgeFromRegistry for MedcareBridge { |
41 | | - fn from_registry(registry: Arc<OntologyRegistry>) -> Result<Self> { |
42 | | - Self::new(registry) |
| 155 | + #[test] |
| 156 | + fn entity_for_non_codebook_name_falls_back_to_registry_lookup() { |
| 157 | + let bridge = MedcareBridge::new(registry_with_healthcare()).unwrap(); |
| 158 | + let err = bridge.entity("NotAConcept").unwrap_err(); |
| 159 | + match err { |
| 160 | + BridgeError::NotInScope { public_name, .. } => { |
| 161 | + assert_eq!(public_name, "NotAConcept") |
| 162 | + } |
| 163 | + other => panic!("expected NotInScope, got {other:?}"), |
| 164 | + } |
43 | 165 | } |
44 | 166 | } |
0 commit comments