-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAggregateTest.kt
More file actions
211 lines (172 loc) · 6.84 KB
/
Copy pathAggregateTest.kt
File metadata and controls
211 lines (172 loc) · 6.84 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
/*
* 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.toBigDecimal
import dev.ohs.fhir.fhirpath.types.FhirPathDate
import dev.ohs.fhir.fhirpath.types.FhirPathQuantity
import io.kotest.core.spec.style.FunSpec
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class AggregateTest :
FunSpec({
val engine = FhirPathEngine.forR4()
test("aggregate keeps inner total independent from outer total") {
val result =
engine.evaluateExpression(
"(1 | 2).aggregate((10 | 20 | 30).aggregate(\$total + \$this, 0) + \$total + \$this, 0)",
null,
)
assertEquals(listOf(123), result.toList())
}
test("sum returns empty on empty collection") {
assertEquals(emptyList<Any>(), engine.evaluateExpression("{}.sum()", null))
}
test("sum adds integers") {
assertEquals(listOf(6), engine.evaluateExpression("(1 | 2 | 3).sum()", null))
}
test("sum adds decimals") {
assertEquals(listOf(3.5.toBigDecimal()), engine.evaluateExpression("(1.0 | 2.5).sum()", null))
}
test("sum adds quantities") {
assertEquals(
listOf(FhirPathQuantity(15.toBigDecimal(), "'m'")),
engine.evaluateExpression("(10 'm' | 5 'm').sum()", null),
)
}
test("sum adds quantities with compatible units") {
assertEquals(
listOf(FhirPathQuantity(1.5.toBigDecimal(), "'kg'")),
engine.evaluateExpression("(1 'kg' | 500 'g').sum()", null),
)
}
test("sum fails on mixed types") {
assertFailsWith<Exception> { engine.evaluateExpression("(1 | 2.5).sum()", null) }
}
test("sum fails on non-numeric types") {
assertFailsWith<Exception> { engine.evaluateExpression("('a' | 'b').sum()", null) }
}
test("sum fails on quantities in incompatible units") {
assertFailsWith<Exception> { engine.evaluateExpression("(10 'm' | 5 's').sum()", null) }
}
test("min returns empty on empty collection") {
assertEquals(emptyList<Any>(), engine.evaluateExpression("{}.min()", null))
}
test("min finds smallest number") {
assertEquals(listOf(1), engine.evaluateExpression("(3 | 1 | 2).min()", null))
}
test("min finds smallest string") {
assertEquals(listOf("a"), engine.evaluateExpression("('b' | 'a' | 'c').min()", null))
}
test("min finds earliest date") {
assertEquals(
listOf(FhirPathDate(2022, 1, 1)),
engine.evaluateExpression("(@2023-05-15 | @2022-01-01).min()", null),
)
}
test("min finds smallest quantity") {
assertEquals(
listOf(FhirPathQuantity(5.toBigDecimal(), "'m'")),
engine.evaluateExpression("(10 'm' | 5 'm').min()", null),
)
}
test("min finds smallest quantity with compatible units") {
assertEquals(
listOf(FhirPathQuantity(500.toBigDecimal(), "'g'")),
engine.evaluateExpression("(1 'kg' | 500 'g').min()", null),
)
}
test("min fails on mixed types") {
assertFailsWith<Exception> { engine.evaluateExpression("(1 | 2.5).min()", null) }
}
test("min fails on non-comparable types") {
assertFailsWith<Exception> { engine.evaluateExpression("(true).min()", null) }
}
test("min fails on quantities in incompatible units") {
assertFailsWith<Exception> { engine.evaluateExpression("(10 'm' | 5 's').min()", null) }
}
test("max returns empty on empty collection") {
assertEquals(emptyList<Any>(), engine.evaluateExpression("{}.max()", null))
}
test("max finds largest number") {
assertEquals(listOf(3), engine.evaluateExpression("(3 | 1 | 2).max()", null))
}
test("max finds largest string") {
assertEquals(listOf("c"), engine.evaluateExpression("('b' | 'a' | 'c').max()", null))
}
test("max finds latest date") {
assertEquals(
listOf(FhirPathDate(2023, 5, 15)),
engine.evaluateExpression("(@2023-05-15 | @2022-01-01).max()", null),
)
}
test("max finds largest quantity") {
assertEquals(
listOf(FhirPathQuantity(10.toBigDecimal(), "'m'")),
engine.evaluateExpression("(10 'm' | 5 'm').max()", null),
)
}
test("max finds largest quantity with compatible units") {
assertEquals(
listOf(FhirPathQuantity(1.toBigDecimal(), "'kg'")),
engine.evaluateExpression("(1 'kg' | 500 'g').max()", null),
)
}
test("max fails on mixed types") {
assertFailsWith<Exception> { engine.evaluateExpression("(1 | 2.5).max()", null) }
}
test("max fails on non-comparable types") {
assertFailsWith<Exception> { engine.evaluateExpression("(true).max()", null) }
}
test("max fails on quantities in incompatible units") {
assertFailsWith<Exception> { engine.evaluateExpression("(10 'm' | 5 's').max()", null) }
}
test("avg returns empty on empty collection") {
assertEquals(emptyList<Any>(), engine.evaluateExpression("{}.avg()", null))
}
test("avg averages integers to decimal") {
assertEquals(listOf(2.toBigDecimal()), engine.evaluateExpression("(1 | 2 | 3).avg()", null))
}
test("avg averages decimals") {
assertEquals(
listOf(1.75.toBigDecimal()),
engine.evaluateExpression("(1.0 | 2.5).avg()", null),
)
}
test("avg averages mixed integer and decimal") {
assertEquals(listOf(1.75.toBigDecimal()), engine.evaluateExpression("(1 | 2.5).avg()", null))
}
test("avg averages quantities") {
assertEquals(
listOf(FhirPathQuantity(15.toBigDecimal(), "'m'")),
engine.evaluateExpression("(10 'm' | 20 'm').avg()", null),
)
}
test("avg averages quantities with compatible units") {
assertEquals(
listOf(FhirPathQuantity(0.75.toBigDecimal(), "'kg'")),
engine.evaluateExpression("(1 'kg' | 500 'g').avg()", null),
)
}
test("avg fails on mixed numeric and quantity") {
assertFailsWith<Exception> { engine.evaluateExpression("(1 | 2 'm').avg()", null) }
}
test("avg fails on non-numeric types") {
assertFailsWith<Exception> { engine.evaluateExpression("('a' | 'b').avg()", null) }
}
test("avg fails on quantities in incompatible units") {
assertFailsWith<Exception> { engine.evaluateExpression("(10 'm' | 5 's').avg()", null) }
}
})