Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions fhirpathpy/engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from fhirpathpy.engine.invocations import (
invocation_registry as base_invocation_registry,
)
from fhirpathpy.engine.nodes import TypeInfo
from fhirpathpy.engine.nodes import FP_Quantity, TypeInfo


def check_integer_param(val):
Expand All @@ -19,15 +19,22 @@ def check_integer_param(val):
def check_number_param(val):
data = util.get_data(val)
if not isinstance(data, numbers.Number):
raise Exception("Expected number, got: " + json.dumps(data))
raise Exception("Expected number, got: " + util.format_error_value(data))
return data


def check_number_or_quantity_param(val):
data = util.get_data(util.val_data_converted(val))
if isinstance(data, FP_Quantity):
return data
return check_number_param(data)


def check_boolean_param(val):
data = util.get_data(val)
if data is True or data is False:
return data
raise Exception("Expected boolean, got: " + json.dumps(data))
raise Exception("Expected boolean, got: " + util.format_error_value(data))


def check_string_param(val):
Expand Down Expand Up @@ -130,6 +137,7 @@ def type_specifier(ctx, parent_data, node):
param_check_table = {
"Integer": check_integer_param,
"Number": check_number_param,
"NumberOrQuantity": check_number_or_quantity_param,
"Boolean": check_boolean_param,
"String": check_string_param,
}
Expand Down Expand Up @@ -171,7 +179,10 @@ def func(data):

if len(res) > 1:
raise Exception(
"Unexpected collection" + json.dumps(res) + "; expected singleton of type " + node_type
"Unexpected collection"
+ util.format_error_value([util.get_data(item) for item in res])
+ "; expected singleton of type "
+ node_type
)

if len(res) == 0 or util.get_data(res[0]) is None:
Expand Down
4 changes: 2 additions & 2 deletions fhirpathpy/engine/invocations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@
"&": {"fn": math.amp, "arity": {2: ["String", "String"]}},
"+": {"fn": math.plus, "arity": {2: ["Any", "Any"]}, "nullable": True},
"-": {"fn": math.minus, "arity": {2: ["Any", "Any"]}, "nullable": True},
"*": {"fn": math.mul, "arity": {2: ["Number", "Number"]}, "nullable": True},
"/": {"fn": math.div, "arity": {2: ["Number", "Number"]}, "nullable": True},
"*": {"fn": math.mul, "arity": {2: ["NumberOrQuantity", "NumberOrQuantity"]}, "nullable": True},
"/": {"fn": math.div, "arity": {2: ["NumberOrQuantity", "NumberOrQuantity"]}, "nullable": True},
"mod": {"fn": math.mod, "arity": {2: ["Number", "Number"]}, "nullable": True},
"div": {"fn": math.intdiv, "arity": {2: ["Number", "Number"]}, "nullable": True},
"or": {"fn": logic.or_op, "arity": {2: [["Boolean"], ["Boolean"]]}},
Expand Down
10 changes: 10 additions & 0 deletions fhirpathpy/engine/invocations/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,22 @@ def minus(ctx, xs, ys):


def mul(ctx, x, y):
if isinstance(x, nodes.FP_Quantity) and isinstance(y, nodes.FP_Quantity):
raise Exception("Cannot multiply " + str(x) + " by " + str(y))
if isinstance(x, nodes.FP_Quantity):
return nodes.FP_Quantity(x.value * y, x.unit)
if isinstance(y, nodes.FP_Quantity):
return nodes.FP_Quantity(x * y.value, y.unit)
return x * y


def div(ctx, x, y):
if isinstance(y, nodes.FP_Quantity):
raise Exception("Cannot divide " + str(x) + " by " + str(y))
if y == 0:
return []
if isinstance(x, nodes.FP_Quantity):
return nodes.FP_Quantity(x.value / y, x.unit)
return x / y


Expand Down
21 changes: 21 additions & 0 deletions tests/cases/6.6_math.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ tests:
- desc: '** Empty result multiplying empty collection (2)'
expression: n4 * n1
result: []
- desc: '** Can multiply a number by a quantity'
expression: 30 * 1 'min'
result: ["30 'min'"]
- desc: '** Can multiply a quantity by a number'
expression: 2 'mg' * 3
result: ["6 'mg'"]
- desc: '** Can add a multiplied quantity to a dateTime'
expression: "@2026-12-04T10:00:00Z + n1 * 1 'min' = @2026-12-04T10:02:00Z"
result: [true]
- desc: '** Error multiplying two quantities'
expression: 2 'm' * 3 'm'
error: true


- desc: '6.6.2 division'
Expand All @@ -37,6 +49,15 @@ tests:
- desc: '** Can divide two number literals'
expression: 5/2
result: [2.5]
- desc: '** Can divide a quantity by a number'
expression: 6 'mg' / 2
result: ["3 'mg'"]
- desc: '** Empty result dividing a quantity by zero'
expression: 6 'mg' / 0
result: []
- desc: '** Error dividing by a quantity'
expression: 6 / 2 'mg'
error: true


- desc: '6.6.3 addition'
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/fhir-quantity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ tests:
expression: QuestionnaireResponse.item[0].answer.value = 2 year
result:
- true
- desc: A FHIR Quantity can be multiplied by a number
inputfile: quantity-example.json
model: 'r4'
expression: QuestionnaireResponse.item[0].answer.value * 2 = 4 year and 2 * QuestionnaireResponse.item[0].answer.value = 4 year
result:
- true
- desc: A FHIR Quantity can be divided by a number
inputfile: quantity-example.json
model: 'r4'
expression: QuestionnaireResponse.item[0].answer.value / 2 = 1 year
result:
- true
- desc: After converting 'a' to year it is equivalent to 'a'
inputfile: quantity-example.json
model: 'r4'
Expand Down
2 changes: 2 additions & 0 deletions tests/test_evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ def convert_quantity_output_to_text_test():
("'abc'.substring(@2026-12-01 + 1 day)", 'Expected integer, but got: "2026-12-02"'),
("(@2026-12-01 + 1 day) + 'x'", "Cannot [2026-12-02] + ['x']"),
("(@T10:00 | @2026-01-01).max()", "Invalid comparison of a DateTime with something else"),
("7 'min' div 2", "Expected number, got: \"7 'min'\""),
("(@2026 | @2027) * 2", 'Unexpected collection["2026", "2027"]'),
],
)
def raise_readable_date_time_error_test(expression: str, message: str):
Expand Down
Loading