Skip to content

Commit 6a7e99e

Browse files
committed
Fix to_date and to_date_time conversion index error
1 parent 0014c83 commit 6a7e99e

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

fhirpathpy/engine/invocations/misc.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,54 +129,53 @@ def to_string(ctx, coll):
129129

130130
def to_date_time(ctx, coll):
131131
ln = len(coll)
132-
rtn = []
133132
if ln > 1:
134133
raise Exception("to_date_time called for a collection of length " + str(ln))
135134

136-
if ln == 1:
137-
value = util.get_data(coll[0])
135+
if ln != 1:
136+
return []
138137

139-
dateTimeObject = nodes.FP_DateTime(value)
138+
value = util.get_data(coll[0])
139+
dateTimeObject = nodes.FP_DateTime(value)
140140

141-
if dateTimeObject:
142-
rtn.append(dateTimeObject)
141+
if not dateTimeObject:
142+
return []
143143

144-
return util.get_data(rtn[0])
144+
return util.get_data(dateTimeObject)
145145

146146

147147
def to_time(ctx, coll):
148148
ln = len(coll)
149-
rtn = []
150149
if ln > 1:
151150
raise Exception("to_time called for a collection of length " + str(ln))
152151

153-
if ln == 1:
154-
value = util.get_data(coll[0])
152+
if ln != 1:
153+
return []
155154

156-
timeObject = nodes.FP_Time(value)
155+
value = util.get_data(coll[0])
156+
timeObject = nodes.FP_Time(value)
157157

158-
if timeObject:
159-
rtn.append(timeObject)
158+
if not timeObject:
159+
return []
160160

161-
return util.get_data(rtn[0])
161+
return util.get_data(timeObject)
162162

163163

164164
def to_date(ctx, coll):
165165
ln = len(coll)
166-
rtn = []
167-
168166
if ln > 1:
169167
raise Exception("to_date called for a collection of length " + str(ln))
170168

171-
if ln == 1:
172-
value = util.get_data(coll[0])
169+
if ln != 1:
170+
return []
173171

174-
dateObject = nodes.FP_DateTime(value)
172+
value = util.get_data(coll[0])
173+
dateObject = nodes.FP_DateTime(value)
175174

176-
if dateObject:
177-
rtn.append(dateObject)
175+
if not dateObject:
176+
return []
178177

179-
return util.get_data(rtn[0])
178+
return util.get_data(dateObject)
180179

181180

182181
def create_converts_to_fn(to_function, _type):

tests/test_real.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pickle
2+
import pytest
23

34
from fhirpathpy import compile, evaluate
45

5-
66
def find_concept_test():
77
env = {}
88
env["Source"] = {
@@ -190,3 +190,30 @@ def compile_with_user_defined_table_test():
190190
options={"userInvocationTable": user_invocation_table},
191191
)
192192
assert expr({"a": [5, 6, 7]}) == [5 * 5, 6 * 6, 7 * 7]
193+
194+
195+
def age_comparation_test():
196+
assert evaluate(
197+
{"resourceType": "Patient", "birthDate": "2024-08-01"},
198+
"Patient.birthDate + 12 month <= now()",
199+
) == [True]
200+
201+
@pytest.mark.parametrize(
202+
("effective", "result"),
203+
[
204+
(None, []),
205+
({}, []),
206+
("2020", ["2020"]),
207+
("2020-01-01", ["2020-01-01"])
208+
],
209+
)
210+
def emty_substring_test(effective, result):
211+
if(isinstance(effective, str)):
212+
effective = {"effective": effective}
213+
r = evaluate(effective, "effective.toString().substring(0,10).toDate()")
214+
if len(r) > 0:
215+
assert len(r) == len(result)
216+
for index,item in enumerate(r):
217+
assert str(item) == result[index]
218+
else:
219+
assert r == result

0 commit comments

Comments
 (0)