-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFhirPathEngineTest.kt
More file actions
193 lines (176 loc) · 8.45 KB
/
Copy pathFhirPathEngineTest.kt
File metadata and controls
193 lines (176 loc) · 8.45 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
/*
* 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()
private val fhirPathEngineStrict = FhirPathEngine.forR4(strictMode = true)
/**
* 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("testConformsTo" 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(
"testPolymorphismAsB" to
"No error should be thrown according to https://hl7.org/fhirpath/#as-type-specifier",
"testDollarOrderNotAllowed" to
"Ordered function validation not implemented. Test expects error when using skip() on unordered collection (children()), but engine does not track collection ordering.",
"testDateTimeGreaterThanDate1" to
"Comparison of two date time values, one with a timezone offset one without",
"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)",
"testQuantity4" to "https://github.com/FHIR/fhir-test-cases/pull/243",
"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`",
"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.",
"testPlusDate19" to
"Test ignores decimal portion of second duration (0.1 's'), contradicting FHIRPath spec section on DateTime arithmetic",
"testType22" to
"`is` with an unknown `System` type should evaluate to false, but the type resolver throws `Unknown System type Patient`.",
"LowBoundaryDateTimeMillisecond1" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/lowBoundary.20and.20highBoundary.20with.20incomplete.20date.20time/with/611113639",
"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",
"testPolymorphicsB" to "Allow invalid test where it's not strict mode but expects output",
"testPeriodInvariantNew" to
"https://chat.fhir.org/#narrow/channel/179266-fhirpath/topic/lowBoundary.20and.20highBoundary.20with.20incomplete.20date.20time/with/611113639",
"testContainedId" to "TBD",
)
@OptIn(ExperimentalKotest::class)
class FhirPathEngineTest :
FunSpec({
val inputMap: Map<String, Resource> =
listJsonFiles(TEST_INPUT_DIR)
.flatMap { entry ->
val resource = jsonR4.decodeFromString<Resource>(entry.value)
listOf(entry.key to resource, entry.key.replace(".json$".toRegex(), ".xml") to resource)
}
.toMap()
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
}
) {
val engine =
if (testCase.mode == "strict" || testCase.expression.mode == "strict")
fhirPathEngineStrict
else fhirPathEngine
if (testCase.expression.invalid != null) {
assertFailsWith<Exception> {
engine.evaluateExpression(
testCase.expression.value,
testCase.inputfile?.let { inputMap[it] },
)
}
} else {
val results =
engine.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>