helios-persistence 0.1.47

Polyglot persistence layer for Helios FHIR Server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Tests for _include and _revinclude search parameters.
//!
//! This module tests _include (forward references) and _revinclude
//! (reverse references) functionality.

use serde_json::json;

use helios_persistence::core::{IncludeProvider, ResourceStorage, SearchProvider};
use helios_persistence::tenant::{TenantContext, TenantId, TenantPermissions};
use helios_persistence::types::{
    IncludeDirective, IncludeType, Pagination, SearchQuery,
};

#[cfg(feature = "sqlite")]
use helios_persistence::backends::sqlite::SqliteBackend;

#[cfg(feature = "sqlite")]
fn create_sqlite_backend() -> SqliteBackend {
    let backend = SqliteBackend::in_memory().expect("Failed to create SQLite backend");
    backend.init_schema().expect("Failed to initialize schema");
    backend
}

fn create_tenant() -> TenantContext {
    TenantContext::new(TenantId::new("test-tenant"), TenantPermissions::full_access())
}

#[cfg(feature = "sqlite")]
async fn seed_include_data(backend: &SqliteBackend, tenant: &TenantContext) {
    // Create organizations
    let org = json!({
        "resourceType": "Organization",
        "id": "org-hospital",
        "name": "Test Hospital"
    });
    backend.create_or_update(tenant, "Organization", "org-hospital", org).await.unwrap();

    // Create patients with organization references
    let patient1 = json!({
        "resourceType": "Patient",
        "id": "patient-1",
        "name": [{"family": "Smith"}],
        "managingOrganization": {"reference": "Organization/org-hospital"}
    });
    let patient2 = json!({
        "resourceType": "Patient",
        "id": "patient-2",
        "name": [{"family": "Jones"}],
        "managingOrganization": {"reference": "Organization/org-hospital"}
    });
    backend.create_or_update(tenant, "Patient", "patient-1", patient1).await.unwrap();
    backend.create_or_update(tenant, "Patient", "patient-2", patient2).await.unwrap();

    // Create observations referencing patients
    let obs1 = json!({
        "resourceType": "Observation",
        "status": "final",
        "subject": {"reference": "Patient/patient-1"},
        "code": {"coding": [{"code": "test"}]}
    });
    let obs2 = json!({
        "resourceType": "Observation",
        "status": "final",
        "subject": {"reference": "Patient/patient-1"},
        "code": {"coding": [{"code": "test2"}]}
    });
    backend.create(tenant, "Observation", obs1).await.unwrap();
    backend.create(tenant, "Observation", obs2).await.unwrap();
}

// ============================================================================
// _include Tests
// ============================================================================

/// Test _include returns referenced resources.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_include_basic() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();
    seed_include_data(&backend, &tenant).await;

    // Search for observations with _include=Observation:subject
    let query = SearchQuery::new("Observation").with_include(IncludeDirective {
        include_type: IncludeType::Include,
        source_type: "Observation".to_string(),
        search_param: "subject".to_string(),
        target_type: Some("Patient".to_string()),
        iterate: false,
    });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should have observations in resources
    assert!(!result.resources.is_empty());

    // Should have patients in included
    assert!(!result.included.is_empty());

    // Check that included resources are patients
    for resource in &result.included {
        assert_eq!(resource.resource_type(), "Patient");
    }
}

/// Test _include with specific target type.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_include_with_target_type() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();
    seed_include_data(&backend, &tenant).await;

    let query = SearchQuery::new("Patient").with_include(IncludeDirective {
        include_type: IncludeType::Include,
        source_type: "Patient".to_string(),
        search_param: "organization".to_string(),
        target_type: Some("Organization".to_string()),
        iterate: false,
    });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should have organizations in included
    for resource in &result.included {
        assert_eq!(resource.resource_type(), "Organization");
    }
}

/// Test _include:iterate for transitive includes.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_include_iterate() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();
    seed_include_data(&backend, &tenant).await;

    // _include:iterate follows references in included resources
    let query = SearchQuery::new("Observation")
        .with_include(IncludeDirective {
            include_type: IncludeType::Include,
            source_type: "Observation".to_string(),
            search_param: "subject".to_string(),
            target_type: Some("Patient".to_string()),
            iterate: false,
        })
        .with_include(IncludeDirective {
            include_type: IncludeType::Include,
            source_type: "Patient".to_string(),
            search_param: "organization".to_string(),
            target_type: Some("Organization".to_string()),
            iterate: true, // This follows references in included patients
        });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should include both patients and organizations
    let included_types: std::collections::HashSet<_> =
        result.included.iter().map(|r| r.resource_type()).collect();

    // Depending on implementation, may have both Patient and Organization
}

// ============================================================================
// _revinclude Tests
// ============================================================================

/// Test _revinclude returns resources that reference the search results.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_revinclude_basic() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();
    seed_include_data(&backend, &tenant).await;

    // Search for patients with _revinclude=Observation:subject
    let query = SearchQuery::new("Patient").with_include(IncludeDirective {
        include_type: IncludeType::Revinclude,
        source_type: "Observation".to_string(),
        search_param: "subject".to_string(),
        target_type: Some("Patient".to_string()),
        iterate: false,
    });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should have patients in resources
    assert!(!result.resources.is_empty());
    for resource in &result.resources {
        assert_eq!(resource.resource_type(), "Patient");
    }

    // Should have observations in included (those that reference the patients)
    for resource in &result.included {
        assert_eq!(resource.resource_type(), "Observation");
    }
}

/// Test _revinclude only includes resources referencing search results.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_revinclude_filtered() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();
    seed_include_data(&backend, &tenant).await;

    // Search for specific patient
    let query = SearchQuery::new("Patient")
        .with_parameter(helios_persistence::types::SearchParameter {
            name: "_id".to_string(),
            param_type: helios_persistence::types::SearchParamType::Token,
            modifier: None,
            values: vec![helios_persistence::types::SearchValue::eq("patient-1")],
            chain: vec![],
        components: vec![],
        })
        .with_include(IncludeDirective {
            include_type: IncludeType::Revinclude,
            source_type: "Observation".to_string(),
            search_param: "subject".to_string(),
            target_type: Some("Patient".to_string()),
            iterate: false,
        });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should only have patient-1
    assert_eq!(result.resources.len(), 1);

    // Should only have observations for patient-1
    for resource in &result.included {
        assert_eq!(
            resource.content()["subject"]["reference"],
            "Patient/patient-1"
        );
    }
}

// ============================================================================
// Edge Cases
// ============================================================================

// ============================================================================
// :iterate Modifier Tests (Recursive Inclusion)
// ============================================================================

/// Test :iterate modifier respects depth limits.
///
/// The :iterate modifier allows recursive inclusion of resources. Implementations
/// must enforce depth limits to prevent unbounded recursion and detect cycles.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_include_iterate_recursive_depth() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();

    // Create a chain of organizations: grandparent -> parent -> child
    let grandparent = json!({
        "resourceType": "Organization",
        "id": "org-grandparent",
        "name": "Grandparent Org"
    });
    backend.create_or_update(&tenant, "Organization", "org-grandparent", grandparent).await.unwrap();

    let parent = json!({
        "resourceType": "Organization",
        "id": "org-parent",
        "name": "Parent Org",
        "partOf": {"reference": "Organization/org-grandparent"}
    });
    backend.create_or_update(&tenant, "Organization", "org-parent", parent).await.unwrap();

    let child = json!({
        "resourceType": "Organization",
        "id": "org-child",
        "name": "Child Org",
        "partOf": {"reference": "Organization/org-parent"}
    });
    backend.create_or_update(&tenant, "Organization", "org-child", child).await.unwrap();

    // Create patient at child org
    let patient = json!({
        "resourceType": "Patient",
        "id": "patient-org-chain",
        "managingOrganization": {"reference": "Organization/org-child"}
    });
    backend.create_or_update(&tenant, "Patient", "patient-org-chain", patient).await.unwrap();

    // Search with :iterate to follow the organization hierarchy
    let query = SearchQuery::new("Patient")
        .with_parameter(helios_persistence::types::SearchParameter {
            name: "_id".to_string(),
            param_type: helios_persistence::types::SearchParamType::Token,
            modifier: None,
            values: vec![helios_persistence::types::SearchValue::eq("patient-org-chain")],
            chain: vec![],
        components: vec![],
        })
        .with_include(IncludeDirective {
            include_type: IncludeType::Include,
            source_type: "Patient".to_string(),
            search_param: "organization".to_string(),
            target_type: Some("Organization".to_string()),
            iterate: false,
        })
        .with_include(IncludeDirective {
            include_type: IncludeType::Include,
            source_type: "Organization".to_string(),
            search_param: "partof".to_string(),
            target_type: Some("Organization".to_string()),
            iterate: true, // Recursively include parent organizations
        });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should have the patient as the main result
    assert_eq!(result.resources.len(), 1);
    assert_eq!(result.resources[0].resource_type(), "Patient");

    // Should include organizations from the chain
    // The depth of inclusion depends on implementation limits
    let org_count = result.included.iter()
        .filter(|r| r.resource_type() == "Organization")
        .count();

    // At minimum, should include the direct organization reference
    assert!(org_count >= 1, "Should include at least the direct organization");
}

/// Test :iterate modifier handles circular references safely.
///
/// When resources reference each other in a cycle, :iterate must detect
/// the cycle and stop to prevent infinite loops.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_include_iterate_cycle_detection() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();

    // Create organizations that reference each other in a cycle
    // A -> B -> C -> A (circular)
    let org_a = json!({
        "resourceType": "Organization",
        "id": "org-cycle-a",
        "name": "Org A",
        "partOf": {"reference": "Organization/org-cycle-c"}
    });

    let org_b = json!({
        "resourceType": "Organization",
        "id": "org-cycle-b",
        "name": "Org B",
        "partOf": {"reference": "Organization/org-cycle-a"}
    });

    let org_c = json!({
        "resourceType": "Organization",
        "id": "org-cycle-c",
        "name": "Org C",
        "partOf": {"reference": "Organization/org-cycle-b"}
    });

    // Create in order that allows references (order matters for some backends)
    backend.create_or_update(&tenant, "Organization", "org-cycle-a", org_a.clone()).await.unwrap();
    backend.create_or_update(&tenant, "Organization", "org-cycle-b", org_b).await.unwrap();
    backend.create_or_update(&tenant, "Organization", "org-cycle-c", org_c).await.unwrap();
    // Update A to complete the cycle
    backend.create_or_update(&tenant, "Organization", "org-cycle-a", org_a).await.unwrap();

    // Search with :iterate - should not hang or overflow
    let query = SearchQuery::new("Organization")
        .with_parameter(helios_persistence::types::SearchParameter {
            name: "_id".to_string(),
            param_type: helios_persistence::types::SearchParamType::Token,
            modifier: None,
            values: vec![helios_persistence::types::SearchValue::eq("org-cycle-a")],
            chain: vec![],
        components: vec![],
        })
        .with_include(IncludeDirective {
            include_type: IncludeType::Include,
            source_type: "Organization".to_string(),
            search_param: "partof".to_string(),
            target_type: Some("Organization".to_string()),
            iterate: true,
        });

    // This should complete without infinite loop
    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await;

    // Should succeed (cycle detected) or fail gracefully
    match result {
        Ok(result) => {
            // Implementation detected cycle and returned finite results
            // Should have at most 3 unique organizations
            let unique_ids: std::collections::HashSet<_> = result.included.iter()
                .map(|r| r.id())
                .collect();
            assert!(unique_ids.len() <= 3, "Should not have more than 3 orgs (cycle detected)");
        }
        Err(_) => {
            // Implementation may return error for detected cycles - also acceptable
        }
    }
}

/// Test :iterate with maximum depth limit.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_include_iterate_max_depth() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();

    // Create a deep chain of locations: L1 -> L2 -> L3 -> L4 -> L5 -> L6 -> L7 -> L8
    let mut prev_id: Option<String> = None;
    for i in 1..=8 {
        let id = format!("location-depth-{}", i);
        let mut location = json!({
            "resourceType": "Location",
            "id": id.clone(),
            "name": format!("Location {}", i)
        });
        if let Some(ref parent) = prev_id {
            location["partOf"] = json!({"reference": format!("Location/{}", parent)});
        }
        backend.create_or_update(&tenant, "Location", &id, location).await.unwrap();
        prev_id = Some(id);
    }

    // Search from the deepest location with :iterate
    let query = SearchQuery::new("Location")
        .with_parameter(helios_persistence::types::SearchParameter {
            name: "_id".to_string(),
            param_type: helios_persistence::types::SearchParamType::Token,
            modifier: None,
            values: vec![helios_persistence::types::SearchValue::eq("location-depth-8")],
            chain: vec![],
        components: vec![],
        })
        .with_include(IncludeDirective {
            include_type: IncludeType::Include,
            source_type: "Location".to_string(),
            search_param: "partof".to_string(),
            target_type: Some("Location".to_string()),
            iterate: true,
        });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should have location-depth-8 as main result
    assert_eq!(result.resources.len(), 1);

    // Implementation may limit depth (commonly 4-6 levels)
    // This test documents that depth limiting works
    let included_locations = result.included.iter()
        .filter(|r| r.resource_type() == "Location")
        .count();

    // Should include at least some parent locations
    // The exact number depends on the implementation's depth limit
    assert!(
        included_locations >= 1,
        "Should include at least one parent location"
    );
}

/// Test _include with no referenced resources.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn test_include_no_references() {
    let backend = create_sqlite_backend();
    let tenant = create_tenant();

    // Create patient without organization reference
    let patient = json!({
        "resourceType": "Patient",
        "name": [{"family": "NoOrg"}]
    });
    backend.create(&tenant, "Patient", patient).await.unwrap();

    let query = SearchQuery::new("Patient").with_include(IncludeDirective {
        include_type: IncludeType::Include,
        source_type: "Patient".to_string(),
        search_param: "organization".to_string(),
        target_type: Some("Organization".to_string()),
        iterate: false,
    });

    let result = backend
        .search(&tenant, &query, Pagination::new(100))
        .await
        .unwrap();

    // Should have patient but no included resources
    assert!(!result.resources.is_empty());
    assert!(result.included.is_empty());
}