helios-sof 0.1.47

This crate provides a complete implementation of the SQL-on-FHIR specification for Rust, enabling the transformation of FHIR resources into tabular data using declarative ViewDefinitions. It supports all major FHIR versions (R4, R4B, R5, R6) through a version-agnostic abstraction layer.
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
use helios_sof::{ContentType, SofBundle, SofViewDefinition, run_view_definition};
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Deserialize)]
struct TestCase {
    #[allow(dead_code)]
    title: String,
    #[allow(dead_code)]
    description: String,
    #[serde(rename = "fhirVersion")]
    fhir_version: Vec<String>,
    resources: Vec<serde_json::Value>,
    tests: Vec<Test>,
}

#[derive(Debug, Deserialize)]
struct Test {
    title: String,
    #[allow(dead_code)]
    tags: Option<Vec<String>>,
    view: serde_json::Value,
    expect: Option<Vec<serde_json::Value>>,
    #[allow(dead_code)]
    #[serde(rename = "expectColumns")]
    expect_columns: Option<Vec<String>>,
    #[serde(rename = "expectError")]
    expect_error: Option<bool>,
}

#[derive(Debug)]
#[allow(dead_code)]
struct TestResult {
    passed: bool,
    reason: Option<String>,
}

fn create_test_bundle(
    resources: &[serde_json::Value],
) -> Result<SofBundle, Box<dyn std::error::Error>> {
    // Create a Bundle with the test resources
    let mut bundle_json = serde_json::json!({
        "resourceType": "Bundle",
        "id": "test-bundle",
        "type": "collection",
        "entry": []
    });

    if let Some(entry_array) = bundle_json["entry"].as_array_mut() {
        for resource in resources {
            entry_array.push(serde_json::json!({
                "resource": resource
            }));
        }
    }

    // Parse as R4 Bundle
    let bundle: helios_fhir::r4::Bundle = serde_json::from_value(bundle_json)?;
    Ok(SofBundle::R4(bundle))
}

fn parse_view_definition(
    view_json: &serde_json::Value,
) -> Result<SofViewDefinition, Box<dyn std::error::Error>> {
    // Add resourceType to make it a valid ViewDefinition resource
    let mut view_def = view_json.clone();
    if let Some(obj) = view_def.as_object_mut() {
        obj.insert(
            "resourceType".to_string(),
            serde_json::Value::String("ViewDefinition".to_string()),
        );
        obj.insert(
            "status".to_string(),
            serde_json::Value::String("active".to_string()),
        );
    }

    let view_definition: helios_fhir::r4::ViewDefinition = serde_json::from_value(view_def)?;
    Ok(SofViewDefinition::R4(view_definition))
}

fn run_single_test(test: &Test, bundle: &SofBundle) -> TestResult {
    // Check if this is an error test
    let expect_error = test.expect_error.unwrap_or(false);

    // Parse the ViewDefinition
    let view_definition = match parse_view_definition(&test.view) {
        Ok(vd) => vd,
        Err(e) => {
            if expect_error {
                // This is expected for error tests
                return TestResult {
                    passed: true,
                    reason: None,
                };
            } else {
                return TestResult {
                    passed: false,
                    reason: Some(format!("Failed to parse ViewDefinition: {}", e)),
                };
            }
        }
    };

    // Run the view definition
    let result = match run_view_definition(view_definition, bundle.clone(), ContentType::Json) {
        Ok(data) => data,
        Err(e) => {
            if expect_error {
                // This is expected for error tests
                return TestResult {
                    passed: true,
                    reason: None,
                };
            } else {
                return TestResult {
                    passed: false,
                    reason: Some(format!("Failed to execute ViewDefinition: {}", e)),
                };
            }
        }
    };

    // If we get here and expect_error is true, the test failed (no error occurred)
    if expect_error {
        return TestResult {
            passed: false,
            reason: Some("Expected an error but ViewDefinition executed successfully".to_string()),
        };
    }

    // Parse the result as JSON
    let actual_rows: Vec<serde_json::Value> = match serde_json::from_slice(&result) {
        Ok(rows) => rows,
        Err(e) => {
            return TestResult {
                passed: false,
                reason: Some(format!("Failed to parse result as JSON: {}", e)),
            };
        }
    };

    // Compare with expected results
    match &test.expect {
        Some(expected) => {
            if compare_results(&actual_rows, expected) {
                TestResult {
                    passed: true,
                    reason: None,
                }
            } else {
                TestResult {
                    passed: false,
                    reason: Some(format!(
                        "Results don't match. Expected: {:?}, Got: {:?}",
                        expected, actual_rows
                    )),
                }
            }
        }
        None => TestResult {
            passed: false,
            reason: Some("Test has neither 'expect' nor 'expectError' field".to_string()),
        },
    }
}

fn compare_results(actual: &[serde_json::Value], expected: &[serde_json::Value]) -> bool {
    if actual.len() != expected.len() {
        return false;
    }

    // For now, do a simple comparison
    for (actual_row, expected_row) in actual.iter().zip(expected.iter()) {
        if !compare_json_values(actual_row, expected_row) {
            return false;
        }
    }

    true
}

fn compare_json_values(actual: &serde_json::Value, expected: &serde_json::Value) -> bool {
    match (actual, expected) {
        (serde_json::Value::Null, serde_json::Value::Null) => true,
        (serde_json::Value::Object(actual_obj), serde_json::Value::Object(expected_obj)) => {
            // Check that all expected keys are present with correct values
            for (key, expected_val) in expected_obj {
                match actual_obj.get(key) {
                    Some(actual_val) => {
                        if !compare_json_values(actual_val, expected_val) {
                            return false;
                        }
                    }
                    None => {
                        // Expected key is missing, only OK if expected value is null
                        if !expected_val.is_null() {
                            return false;
                        }
                    }
                }
            }
            true
        }
        _ => actual == expected,
    }
}

#[test]
fn test_basic_view_definition() {
    // Test the first test case from basic.json
    let resources = vec![
        serde_json::json!({
            "resourceType": "Patient",
            "id": "pt1",
            "name": [{"family": "F1"}],
            "active": true
        }),
        serde_json::json!({
            "resourceType": "Patient",
            "id": "pt2",
            "name": [{"family": "F2"}],
            "active": false
        }),
        serde_json::json!({
            "resourceType": "Patient",
            "id": "pt3"
        }),
    ];

    let bundle = create_test_bundle(&resources).expect("Failed to create test bundle");

    let view = serde_json::json!({
        "resource": "Patient",
        "status": "active",
        "select": [{
            "column": [{
                "name": "id",
                "path": "id",
                "type": "id"
            }]
        }]
    });

    let view_definition = parse_view_definition(&view).expect("Failed to parse ViewDefinition");

    let result = run_view_definition(view_definition, bundle, ContentType::Json)
        .expect("Failed to run ViewDefinition");

    let actual_rows: Vec<serde_json::Value> =
        serde_json::from_slice(&result).expect("Failed to parse result as JSON");

    let expected = [
        serde_json::json!({"id": "pt1"}),
        serde_json::json!({"id": "pt2"}),
        serde_json::json!({"id": "pt3"}),
    ];

    assert_eq!(actual_rows.len(), expected.len(), "Row count mismatch");
    for (actual, expected) in actual_rows.iter().zip(expected.iter()) {
        assert!(
            compare_json_values(actual, expected),
            "Row mismatch: expected {:?}, got {:?}",
            expected,
            actual
        );
    }
}

#[test]
fn test_basic_boolean_attribute() {
    // Test boolean handling
    let resources = vec![
        serde_json::json!({
            "resourceType": "Patient",
            "id": "pt1",
            "name": [{"family": "F1"}],
            "active": true
        }),
        serde_json::json!({
            "resourceType": "Patient",
            "id": "pt2",
            "name": [{"family": "F2"}],
            "active": false
        }),
        serde_json::json!({
            "resourceType": "Patient",
            "id": "pt3"
        }),
    ];

    let bundle = create_test_bundle(&resources).expect("Failed to create test bundle");

    let view = serde_json::json!({
        "resource": "Patient",
        "status": "active",
        "select": [{
            "column": [
                {
                    "name": "id",
                    "path": "id",
                    "type": "id"
                },
                {
                    "name": "active",
                    "path": "active",
                    "type": "boolean"
                }
            ]
        }]
    });

    let view_definition = parse_view_definition(&view).expect("Failed to parse ViewDefinition");

    let result = run_view_definition(view_definition, bundle, ContentType::Json)
        .expect("Failed to run ViewDefinition");

    let actual_rows: Vec<serde_json::Value> =
        serde_json::from_slice(&result).expect("Failed to parse result as JSON");

    let expected = [
        serde_json::json!({"id": "pt1", "active": true}),
        serde_json::json!({"id": "pt2", "active": false}),
        serde_json::json!({"id": "pt3", "active": null}),
    ];

    assert_eq!(actual_rows.len(), expected.len(), "Row count mismatch");
    for (actual, expected) in actual_rows.iter().zip(expected.iter()) {
        assert!(
            compare_json_values(actual, expected),
            "Row mismatch: expected {:?}, got {:?}",
            expected,
            actual
        );
    }
}

#[test]
fn test_run_basic_test_file() {
    // Load and run a simple test case from the test suite
    let mut test_suite_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    test_suite_path.push("tests/sql-on-fhir-v2/tests/basic.json");

    if !test_suite_path.exists() {
        // Skip test if test suite is not available
        return;
    }

    let content = fs::read_to_string(test_suite_path).expect("Failed to read test file");
    let test_case: TestCase = serde_json::from_str(&content).expect("Failed to parse test case");

    // Check if we support the FHIR version
    let supports_r4 = test_case.fhir_version.contains(&"4.0.1".to_string());
    if !supports_r4 {
        return; // Skip tests that don't support R4
    }

    // Create a bundle from the test resources
    let bundle = create_test_bundle(&test_case.resources).expect("Failed to create test bundle");

    // Run the first test
    if let Some(first_test) = test_case.tests.first() {
        let result = run_single_test(first_test, &bundle);
        println!("Test '{}' result: {:?}", first_test.title, result);

        // For now, we'll just print the result rather than assert
        // This allows us to see what's working and what needs to be fixed
    }
}

#[test]
fn test_repeat_directive() {
    // Load and run repeat directive tests
    let mut test_suite_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    test_suite_path.push("tests/sql-on-fhir-v2/tests/repeat.json");

    if !test_suite_path.exists() {
        panic!("Repeat test file not found at {:?}", test_suite_path);
    }

    let content = fs::read_to_string(&test_suite_path).expect("Failed to read repeat test file");
    let test_case: TestCase =
        serde_json::from_str(&content).expect("Failed to parse repeat test case");

    // Check if we support the FHIR version
    let supports_r4 = test_case.fhir_version.contains(&"4.0.1".to_string());
    if !supports_r4 {
        panic!("Repeat tests don't support R4");
    }

    // Create a bundle from the test resources
    let bundle = create_test_bundle(&test_case.resources).expect("Failed to create test bundle");

    // Run all tests
    let mut failed_tests = Vec::new();
    for test in &test_case.tests {
        let result = run_single_test(test, &bundle);
        if !result.passed {
            failed_tests.push((test.title.clone(), result.reason.clone()));
        }
        println!(
            "Test '{}': {}",
            test.title,
            if result.passed { "PASSED" } else { "FAILED" }
        );
        if let Some(reason) = &result.reason {
            println!("  Reason: {}", reason);
        }
    }

    if !failed_tests.is_empty() {
        panic!(
            "Failed {} repeat tests:\n{}",
            failed_tests.len(),
            failed_tests
                .iter()
                .map(|(title, reason)| format!(
                    "  - {}: {}",
                    title,
                    reason.as_ref().unwrap_or(&"Unknown reason".to_string())
                ))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }
}