-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTraceTest.kt
More file actions
147 lines (124 loc) · 3.95 KB
/
Copy pathTraceTest.kt
File metadata and controls
147 lines (124 loc) · 3.95 KB
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
/*
* Copyright 2025-2026 Open Health Stack Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.ohs.fhir.fhirpath
import kotlinx.serialization.json.Json
import dev.ohs.fhir.model.r4.Resource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
private val jsonR4 = Json { ignoreUnknownKeys = true }
private val engine = FhirPathEngine.forR4()
private const val PATIENT_JSON =
"""{
"resourceType": "Patient",
"id": "example",
"name": [
{
"use": "official",
"family": "Chalmers",
"given": [
"Peter",
"James"
]
},
{
"use": "usual",
"given": [
"Jim"
]
},
{
"use": "maiden",
"family": "Windsor",
"given": [
"Peter",
"James"
]
}
]
}"""
private fun loadPatient(): Resource {
return jsonR4.decodeFromString(PATIENT_JSON)
}
class TraceTest {
@Test
fun `trace captures values without projection`() {
val patient = loadPatient()
engine.evaluateExpression("name.trace('names').given", patient)
assertTrue(engine.traces.containsKey("names"))
val entries = engine.traces["names"]!!
assertEquals(3, entries.size)
}
@Test
fun `trace captures values with projection`() {
val patient = loadPatient()
engine.evaluateExpression("name.trace('families', family).given", patient)
val entries = engine.traces["families"]!!
val values = entries.map { it.value }
assertTrue(values.contains("Chalmers"))
assertTrue(values.contains("Windsor"))
}
@Test
fun `trace paths are derived from expression`() {
val patient = loadPatient()
engine.evaluateExpression("name.trace('names')", patient)
val entries = engine.traces["names"]!!
assertEquals("Patient.name[0]", entries[0].path)
assertEquals("Patient.name[1]", entries[1].path)
assertEquals("Patient.name[2]", entries[2].path)
}
@Test
fun `trace with deeper path includes full expression`() {
val patient = loadPatient()
engine.evaluateExpression("name.given.trace('givens')", patient)
val entries = engine.traces["givens"]!!
assertTrue(entries.isNotEmpty())
assertTrue(entries[0].path.startsWith("Patient.name.given["))
}
@Test
fun `multiple traces with different labels`() {
val patient = loadPatient()
engine.evaluateExpression(
"name.trace('all').where(use = 'official').trace('official').given",
patient,
)
assertTrue(engine.traces.containsKey("all"))
assertTrue(engine.traces.containsKey("official"))
assertEquals(3, engine.traces["all"]!!.size)
assertEquals(1, engine.traces["official"]!!.size)
}
@Test
fun `no trace in expression produces empty traces map`() {
val patient = loadPatient()
engine.evaluateExpression("name.given", patient)
assertTrue(engine.traces.isEmpty())
}
@Test
fun `trace does not alter evaluation result`() {
val patient = loadPatient()
val withTrace = engine.evaluateExpression("name.trace('t').given.count()", patient)
val withoutTrace = engine.evaluateExpression("name.given.count()", patient)
assertEquals(withoutTrace.toList(), withTrace.toList())
}
@Test
fun `traces are cleared between evaluations`() {
val patient = loadPatient()
engine.evaluateExpression("name.trace('first')", patient)
assertTrue(engine.traces.containsKey("first"))
engine.evaluateExpression("name.given.count()", patient)
assertTrue(engine.traces.isEmpty())
}
}