-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFhirPathEngineTest.kt
More file actions
239 lines (223 loc) · 11.4 KB
/
Copy pathFhirPathEngineTest.kt
File metadata and controls
239 lines (223 loc) · 11.4 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
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
/*
* 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 com.ionspin.kotlin.bignum.decimal.BigDecimal
import dev.ohs.fhir.fhirpath.types.FhirPathDate
import dev.ohs.fhir.fhirpath.types.FhirPathDateTime
import dev.ohs.fhir.fhirpath.types.FhirPathQuantity
import dev.ohs.fhir.fhirpath.types.FhirPathTime
import dev.ohs.fhir.model.r4.Resource
import io.kotest.common.ExperimentalKotest
import io.kotest.core.spec.style.FunSpec
import io.kotest.core.test.Enabled
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlinx.serialization.json.Json
import nl.adaptivity.xmlutil.serialization.XML
private const val TEST_RESOURCE_DIR = "third_party/fhir-test-cases/r4"
private const val TEST_INPUT_DIR = "${TEST_RESOURCE_DIR}/resources"
private val jsonR4 = Json { ignoreUnknownKeys = true }
private val fhirPathEngine = FhirPathEngine.forR4()
/**
* A map from the test group name to the reason why the test group is skipped.
*
* N.B. This should be kept in sync with the conformance table in the `README.md` file.
*/
val skippedTestGroupToReasonMap =
mapOf(
"testEscapeUnescape" to "Unimplemented",
"testVariables" to "Unimplemented",
"Comparable" to "Unimplemented",
"Precision" to "Unimplemented",
)
/**
* A map from the test case name to the reason why the test case is skipped.
*
* N.B. This should be kept in sync with the conformance table in the `README.md` file.
*/
val skippedTestCaseToReasonMap =
mapOf(
"testPolymorphismB" to "Strict mode is not implemented yet",
"testPolymorphismAsB" to
"No error should be thrown according to https://hl7.org/fhirpath/#as-type-specifier",
"testDateTimeGreaterThanDate1" to
"Comparison of two date time values, one with a timezone offset one without",
"testQuantity4" to "https://github.com/FHIR/fhir-test-cases/pull/243",
"testSubSetOf3" to
"The test resource is invalid and missing (https://github.com/FHIR/fhir-test-cases/issues/247); the scope of \"\$this\" is unclear (https://jira.hl7.org/browse/FHIR-44601)",
"testIif11" to
"https://jira.hl7.org/browse/FHIR-44774; https://jira.hl7.org/browse/FHIR-44601; https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/scope.20of.20this/with/531507415; https://chat.fhir.org/#narrow/stream/179266-fhirpath/topic/context.20of.20the.20.60iif.20.60; https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/receiver.20of.20iif/with/558282370",
"testNow1" to "As `testDateTimeGreaterThanDate1`",
"testPlusDate13" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/Definite.20durations.20above.20seconds.20in.20date.20time.20arithmetic/with/564095766",
"testPlusDate15" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/Definite.20durations.20above.20seconds.20in.20date.20time.20arithmetic/with/564095766",
"testPlusDate18" to
"To be fixed together with `testPlusDate13`, `testPlusDate15`, `testPlusDate21`, `testPlusDate22` for a consistent implementation.",
"testPlusDate19" to
"To be fixed together with `testPlusDate13`, `testPlusDate15`, `testPlusDate21`, `testPlusDate22` for a consistent implementation.",
"testPlusDate20" to
"To be fixed together with `testPlusDate13`, `testPlusDate15`, `testPlusDate21`, `testPlusDate22` for a consistent implementation.",
"testPlusDate21" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/Definite.20durations.20above.20seconds.20in.20date.20time.20arithmetic/with/564095766",
"testPlusDate22" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/Definite.20durations.20above.20seconds.20in.20date.20time.20arithmetic/with/564095766",
"testMinus5" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/Definite.20durations.20above.20seconds.20in.20date.20time.20arithmetic/with/564095766",
"testDollarOrderNotAllowed" to
"Ordered function validation not implemented. Test expects error when using skip() on unordered collection (children()), but engine does not track collection ordering.",
"testSimpleFail" to "Strict mode is not implemented yet",
"testSimpleWithWrongContext" to "Strict mode is not implemented yet",
"testPolymorphicsB" to "Allow invalid test where it's not strict mode but expects output",
"testIndex" to "TBD",
"testPeriodInvariantOld" to "hasValue() is not implemented.",
"testPeriodInvariantNew" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/lowBoundary.20and.20highBoundary.20with.20incomplete.20date.20time/with/611113639",
"testCombine2" to "FHIR String and Kotlin String comparison issue in exclude()",
"testCombine3" to "As above",
"testContainedId" to "TBD",
"testPrimitiveExtensions" to "Function `hasValue` is not implemented.",
"testSort8" to "Test uses `-\$this` for descending string sort, but spec uses asc/desc.",
"testSort10" to "Test uses `-` prefix for descending sort, but spec uses asc/desc.",
"testType1" to "Function `type` is not implemented.",
"testType1a" to "As `testType1`.",
"testType2" to "As `testType1`.",
"testType2a" to "As `testType1`.",
"testType3" to "As `testType1`.",
"testType4" to "As `testType1`.",
"testType9" to "As `testType1`.",
"testType10" to "As `testType1`.",
"testType15" to "As `testType1`.",
"testType16" to "As `testType1`.",
"testType20" to
"Function `type` is not implemented; the `ofType` part of the expression works.",
"testType21" to "As `testType20`.",
"testType22" to
"`is` with an unknown `System` type should evaluate to false, but the type resolver throws `Unknown System type Patient`.",
"testType23" to "As `testType20`.",
"testTypeA" to
"Evaluating `Parameters.parameter[x].value` crashes with `NoSuchElementException: ArrayDeque is empty`.",
"testTypeA1" to "As `testTypeA`.",
"testTypeA2" to "As `testTypeA`.",
"testTypeA3" to "As `testTypeA`.",
"testTypeA4" to "As `testTypeA`.",
"HighBoundaryDateTimeMillisecond1" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/lowBoundary.20and.20highBoundary.20with.20incomplete.20date.20time/with/611113639",
"HighBoundaryDateTimeMillisecond3" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/lowBoundary.20and.20highBoundary.20with.20incomplete.20date.20time/with/611113639",
"LowBoundaryDateTimeMillisecond1" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/lowBoundary.20and.20highBoundary.20with.20incomplete.20date.20time/with/611113639",
"testFHIRPathIsFunction2" to
"`code` specializes `string` in FHIR, but type checks use exact equality with no subtype semantics.",
"testFHIRPathIsFunction8" to
"The vendored copy of the input resource `observation-example` is missing the `patient-age` extension these tests query; the extension exists upstream in fhir-test-cases.",
"testFHIRPathIsFunction9" to
"As `testFHIRPathIsFunction8`. Once the input is updated, this test also needs subtype-aware `is`: the extension value is an `Age`, which specializes `Quantity`.",
"testFHIRPathIsFunction10" to "As `testFHIRPathIsFunction8`.",
)
@OptIn(ExperimentalKotest::class)
class FhirPathEngineTest :
FunSpec({
val inputMap: Map<String, Resource> =
listJsonFiles(TEST_INPUT_DIR)
.mapKeys { it.key.replace(".json$".toRegex(), ".xml") }
.mapValues { jsonR4.decodeFromString(it.value) }
val xmlContent = loadFile("${TEST_RESOURCE_DIR}/tests-fhir-r4.xml")
val testSuite = XML.decodeFromString<Tests>(xmlContent)
testSuite.groups.forEach { group ->
context(group.name).config(
enabledOrReasonIf = {
skippedTestGroupToReasonMap[group.name]?.let { Enabled.disabled(it) } ?: Enabled.enabled
}
) {
group.tests.forEach { testCase ->
test(testCase.name).config(
enabledOrReasonIf = {
skippedTestCaseToReasonMap[testCase.name]?.let { Enabled.disabled(it) }
?: Enabled.enabled
}
) {
if (testCase.expression.invalid != null) {
assertFailsWith<Exception> {
fhirPathEngine.evaluateExpression(
testCase.expression.value,
testCase.inputfile?.let { inputMap[it] },
)
}
} else {
val results =
fhirPathEngine.evaluateExpression(
testCase.expression.value,
testCase.inputfile?.let { inputMap[it] },
)
dev.ohs.fhir.fhirpath.assertEquals(testCase.outputs, results)
}
}
}
}
}
})
private fun assertEquals(expected: List<Output>, actual: Collection<Any>) {
assertEquals(expected.size, actual.size)
expected.zip(actual).forEach { dev.ohs.fhir.fhirpath.assertEquals(it.first, it.second) }
}
private fun assertEquals(expected: Output, actual: Any) {
val type = expected.type ?: inferType(actual)
when (type) {
"date" -> assertEquals(FhirPathDate.fromString(expected.value.trimStart('@')), actual)
"dateTime" -> assertEquals(FhirPathDateTime.fromString(expected.value.trimStart('@')), actual)
"time" -> assertEquals(FhirPathTime.fromString(expected.value.trimStart('@', 'T')), actual)
"code",
"string" -> assertEquals(expected.value, actual.toStringValue())
"boolean" -> assertEquals(expected.value, if (actual is Boolean) actual.toString() else "true")
"integer" -> assertEquals(expected.value, (actual as Int).toString())
"decimal" -> assertEquals(expected.value.toBigDecimalPreservingScale(), actual as BigDecimal)
"Quantity" -> assertEquals(expected.value, actual.toQuantityString())
else -> throw AssertionError("Unknown type: $type")
}
}
private fun inferType(actual: Any): String =
when (actual) {
is Boolean -> "boolean"
is Int -> "integer"
is BigDecimal -> "decimal"
is FhirPathDate -> "date"
is FhirPathDateTime -> "dateTime"
is FhirPathTime -> "time"
is dev.ohs.fhir.model.r4.Quantity,
is FhirPathQuantity -> "Quantity"
is String,
is dev.ohs.fhir.model.r4.String -> "string"
else -> throw AssertionError("Unknown actual type: ${actual::class}")
}
private fun Any.toStringValue(): String =
when (this) {
is String -> this
is dev.ohs.fhir.model.r4.String -> value!!
else -> throw AssertionError("Cannot extract string value from: $this")
}
private fun Any.toQuantityString(): String =
when (this) {
is dev.ohs.fhir.model.r4.Quantity -> "${value!!.value} ${code!!.value}"
is FhirPathQuantity -> {
val cleanUnit = unit?.trim('\'')
val formattedVal = value?.toPlainStringPreservingDecimalPlaces()
"$formattedVal '$cleanUnit'"
}
else -> throw AssertionError("Cannot extract Quantity value from: $this")
}
expect fun loadFile(file: String): String
expect fun listJsonFiles(dir: String): Map<String, String>