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
//! Tests for patient parameter handling with different reference formats
//!
//! This module tests that patient references work with both full references
//! (e.g., "Patient/pt-1") and bare IDs (e.g., "pt-1").
use axum::http::StatusCode;
use serde_json::json;
mod common;
/// Helper to create a ViewDefinition for patient data
fn patient_view_definition() -> serde_json::Value {
json!({
"resourceType": "ViewDefinition",
"id": "patient-demographics",
"status": "active",
"resource": "Patient",
"select": [{
"column": [{
"name": "id",
"path": "id"
}, {
"name": "family",
"path": "name.family"
}]
}]
})
}
/// Helper to create test patients
fn test_patients_bundle() -> serde_json::Value {
json!({
"resourceType": "Bundle",
"type": "collection",
"entry": [
{
"resource": {
"resourceType": "Patient",
"id": "pt-1",
"name": [{
"family": "Smith",
"given": ["John"]
}]
}
},
{
"resource": {
"resourceType": "Patient",
"id": "pt-2",
"name": [{
"family": "Jones",
"given": ["Jane"]
}]
}
}
]
})
}
/// Test patient parameter with full reference format (Patient/pt-1)
#[tokio::test]
async fn test_patient_parameter_with_full_reference() {
let server = common::test_server().await;
let parameters = json!({
"resourceType": "Parameters",
"parameter": [
{
"name": "subjectResource",
"resource": patient_view_definition()
},
{
"name": "resource",
"resource": test_patients_bundle()
},
{
"name": "patient",
"valueString": "Patient/pt-1"
}
]
});
// Production's default `_format` is `ndjson` (SoF v2 PR #353), not
// `json` as the old stub assumed. Request `application/json`
// explicitly so the response is a JSON array.
let response = server
.post("/$sql-run")
.content_type("application/json")
.add_header("Accept", "application/json")
.json(¶meters)
.await;
response.assert_status(StatusCode::OK);
let result = response.json::<serde_json::Value>();
// Should return only patient pt-1
assert!(result.is_array());
let patients = result.as_array().unwrap();
assert_eq!(patients.len(), 1);
assert_eq!(patients[0]["id"], "pt-1");
}
/// Test patient parameter with bare ID format (pt-1)
#[tokio::test]
async fn test_patient_parameter_with_bare_id() {
let server = common::test_server().await;
let parameters = json!({
"resourceType": "Parameters",
"parameter": [
{
"name": "subjectResource",
"resource": patient_view_definition()
},
{
"name": "resource",
"resource": test_patients_bundle()
},
{
"name": "patient",
"valueString": "pt-1"
}
]
});
// Production's default `_format` is `ndjson` (SoF v2 PR #353), not
// `json` as the old stub assumed. Request `application/json`
// explicitly so the response is a JSON array.
let response = server
.post("/$sql-run")
.content_type("application/json")
.add_header("Accept", "application/json")
.json(¶meters)
.await;
response.assert_status(StatusCode::OK);
let result = response.json::<serde_json::Value>();
// Should return only patient pt-1
assert!(result.is_array(), "Expected array, got: {:?}", result);
let patients = result.as_array().unwrap();
assert_eq!(patients.len(), 1);
assert_eq!(patients[0]["id"], "pt-1");
}
/// Test patient parameter with valueReference format
#[tokio::test]
async fn test_patient_parameter_with_value_reference() {
let server = common::test_server().await;
let parameters = json!({
"resourceType": "Parameters",
"parameter": [
{
"name": "subjectResource",
"resource": patient_view_definition()
},
{
"name": "resource",
"resource": test_patients_bundle()
},
{
"name": "patient",
"valueReference": {
"reference": "Patient/pt-2"
}
}
]
});
// Production's default `_format` is `ndjson` (SoF v2 PR #353), not
// `json` as the old stub assumed. Request `application/json`
// explicitly so the response is a JSON array.
let response = server
.post("/$sql-run")
.content_type("application/json")
.add_header("Accept", "application/json")
.json(¶meters)
.await;
response.assert_status(StatusCode::OK);
let result = response.json::<serde_json::Value>();
// Should return only patient pt-2
assert!(result.is_array());
let patients = result.as_array().unwrap();
assert_eq!(patients.len(), 1);
assert_eq!(patients[0]["id"], "pt-2");
}
/// Test filtering observations by patient with bare ID
#[tokio::test]
async fn test_observation_filtering_with_bare_patient_id() {
let server = common::test_server().await;
let view_def = json!({
"resourceType": "ViewDefinition",
"id": "observation-view",
"status": "active",
"resource": "Observation",
"select": [{
"column": [{
"name": "id",
"path": "id"
}, {
"name": "code",
"path": "code.coding[0].code"
}]
}]
});
let bundle = json!({
"resourceType": "Bundle",
"type": "collection",
"entry": [
{
"resource": {
"resourceType": "Observation",
"id": "obs-1",
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "8302-2"
}]
},
"subject": {
"reference": "Patient/pt-1"
}
}
},
{
"resource": {
"resourceType": "Observation",
"id": "obs-2",
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "8310-5"
}]
},
"subject": {
"reference": "Patient/pt-2"
}
}
},
// Production requires the referenced Patient to be present
// among the supplied resources: per the SoF v2 spec error
// table, a `patient` filter that does not resolve against a
// supplied `Patient` resource is a hard 400 (see
// `lib.rs::filter_resources_by_patient_and_group`'s
// absent-target check). The old stub filtered by reference
// match alone and never enforced this.
{
"resource": {
"resourceType": "Patient",
"id": "pt-1"
}
}
]
});
let parameters = json!({
"resourceType": "Parameters",
"parameter": [
{
"name": "subjectResource",
"resource": view_def
},
{
"name": "resource",
"resource": bundle
},
{
"name": "patient",
"valueString": "pt-1" // Using bare ID
}
]
});
// Production's default `_format` is `ndjson` (SoF v2 PR #353), not
// `json` as the old stub assumed. Request `application/json`
// explicitly so the response is a JSON array.
let response = server
.post("/$sql-run")
.content_type("application/json")
.add_header("Accept", "application/json")
.json(¶meters)
.await;
response.assert_status(StatusCode::OK);
let result = response.json::<serde_json::Value>();
// Should return only observation for patient pt-1
assert!(result.is_array());
let observations = result.as_array().unwrap();
assert_eq!(observations.len(), 1);
assert_eq!(observations[0]["id"], "obs-1");
}