Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(stepfunctions): support numeric Choice path comparisons (#1674)

Jay Janssen2d2a9fe3c213
diff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.py
index 5e5f9702..4e360561 100644
--- a/ministack/services/stepfunctions.py
+++ b/ministack/services/stepfunctions.py
@@ -2032,6 +2032,18 @@ def _evaluate_rule(rule, data):
return _is_num(value) and value <= rule["NumericLessThanEquals"]
if "NumericGreaterThanEquals" in rule:
return _is_num(value) and value >= rule["NumericGreaterThanEquals"]
+ if "NumericLessThanPath" in rule:
+ rhs = _resolve_path(rule["NumericLessThanPath"], data)
+ return _is_num(value) and _is_num(rhs) and value < rhs
+ if "NumericGreaterThanPath" in rule:
+ rhs = _resolve_path(rule["NumericGreaterThanPath"], data)
+ return _is_num(value) and _is_num(rhs) and value > rhs
+ if "NumericLessThanEqualsPath" in rule:
+ rhs = _resolve_path(rule["NumericLessThanEqualsPath"], data)
+ return _is_num(value) and _is_num(rhs) and value <= rhs
+ if "NumericGreaterThanEqualsPath" in rule:
+ rhs = _resolve_path(rule["NumericGreaterThanEqualsPath"], data)
+ return _is_num(value) and _is_num(rhs) and value >= rhs
# --- boolean ---
if "BooleanEquals" in rule:
diff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.py
index 3b2d2162..5755e1d2 100644
--- a/tests/test_stepfunctions.py
+++ b/tests/test_stepfunctions.py
@@ -8068,3 +8068,63 @@ def test_sfn_jsonata_tomillis_still_parses_iso(sfn):
assert json.loads(desc["output"]) == {"v": 1577836800000}
finally:
sfn.delete_state_machine(stateMachineArn=sm)
+
+
+def test_sfn_choice_numeric_path_dynamic_batch_loop(sfn):
+ """A dynamic batch loop guarded by NumericGreaterThanEqualsPath must exit once
+ the cursor reaches the batch size. Before the *Path relational operators were
+ implemented the guard silently evaluated false, the cursor ran past the end of
+ the array and States.ArrayGetItem raised IndexError, failing the execution."""
+ import uuid as _uuid
+ definition = json.dumps(
+ {
+ "StartAt": "CheckIndex",
+ "States": {
+ "CheckIndex": {
+ "Type": "Choice",
+ "Choices": [
+ {
+ "Variable": "$.index",
+ "NumericGreaterThanEqualsPath": "$.count",
+ "Next": "Done",
+ }
+ ],
+ "Default": "Fetch",
+ },
+ "Fetch": {
+ "Type": "Pass",
+ "Parameters": {
+ "items.$": "$.items",
+ "count.$": "$.count",
+ "index.$": "States.MathAdd($.index, 1)",
+ "item.$": "States.ArrayGetItem($.items, $.index)",
+ },
+ "Next": "CheckIndex",
+ },
+ "Done": {
+ "Type": "Pass",
+ "Parameters": {"last.$": "$.item", "index.$": "$.index"},
+ "End": True,
+ },
+ },
+ }
+ )
+ name = f"qa-sfn-numeric-path-loop-{_uuid.uuid4().hex[:8]}"
+ arn = sfn.create_state_machine(
+ name=name,
+ definition=definition,
+ roleArn="arn:aws:iam::000000000000:role/r",
+ )["stateMachineArn"]
+ try:
+ items = ["alpha", "beta", "gamma"]
+ exec_arn = sfn.start_execution(
+ stateMachineArn=arn,
+ input=json.dumps({"items": items, "count": len(items), "index": 0}),
+ )["executionArn"]
+ desc = _wait_sfn(sfn, exec_arn)
+ assert desc["status"] == "SUCCEEDED", desc
+ output = json.loads(desc["output"])
+ assert output["last"] == "gamma"
+ assert output["index"] == 3
+ finally:
+ sfn.delete_state_machine(stateMachineArn=arn)
diff --git a/tests/test_stepfunctions_choice_numeric_paths.py b/tests/test_stepfunctions_choice_numeric_paths.py
new file mode 100644
index 00000000..6d466663
--- /dev/null
+++ b/tests/test_stepfunctions_choice_numeric_paths.py
@@ -0,0 +1,101 @@
+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
+"""Unit tests for numeric ``*Path`` Choice operators in the Step Functions engine.
+
+Unlike the integration tests in ``test_stepfunctions.py`` these exercise the
+ASL rule evaluator directly, so no running emulator is required.
+"""
+
+from ministack.services.stepfunctions import _evaluate_rule
+
+
+def test_numeric_path_operators_relate_variable_to_resolved_path():
+ """NumericLessThanPath/GreaterThanPath/LessThanEqualsPath/GreaterThanEqualsPath
+ compare the Variable-resolved LHS against the path-resolved RHS."""
+ data = {"value": 5, "limit": 3, "floor": 5, "ceil": 10}
+ assert _evaluate_rule({"Variable": "$.value", "NumericLessThanPath": "$.ceil"}, data)
+ assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanPath": "$.limit"}, data)
+ assert _evaluate_rule({"Variable": "$.value", "NumericLessThanEqualsPath": "$.floor"}, data)
+ assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.floor"}, data)
+ # Inverse comparisons must not match.
+ assert not _evaluate_rule({"Variable": "$.value", "NumericLessThanPath": "$.limit"}, data)
+ assert not _evaluate_rule({"Variable": "$.value", "NumericGreaterThanPath": "$.ceil"}, data)
+ assert not _evaluate_rule({"Variable": "$.value", "NumericLessThanEqualsPath": "$.limit"}, data)
+ assert not _evaluate_rule({"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.ceil"}, data)
+
+
+def test_numeric_path_operators_reject_bool_operand():
+ """Booleans are not numeric operands: ``_is_num`` excludes ``bool`` even though
+ ``bool`` is an ``int`` subclass in Python."""
+ data = {"flag": True, "limit": 0}
+ for op in (
+ "NumericEqualsPath",
+ "NumericLessThanPath",
+ "NumericGreaterThanPath",
+ "NumericLessThanEqualsPath",
+ "NumericGreaterThanEqualsPath",
+ ):
+ assert not _evaluate_rule({"Variable": "$.flag", op: "$.limit"}, data), op
+
+
+def test_numeric_path_operators_reject_non_numeric_operand():
+ """Strings and missing values are not numeric operands."""
+ data = {"text": "5", "limit": 3}
+ for op in (
+ "NumericLessThanPath",
+ "NumericGreaterThanPath",
+ "NumericLessThanEqualsPath",
+ "NumericGreaterThanEqualsPath",
+ ):
+ assert not _evaluate_rule({"Variable": "$.text", op: "$.limit"}, data), op
+ assert not _evaluate_rule({"Variable": "$.missing", op: "$.limit"}, data), op
+
+
+def test_numeric_path_operators_float_operands():
+ """Float LHS and RHS values compare correctly."""
+ data = {"value": 0.5, "limit": 0.25}
+ assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanPath": "$.limit"}, data)
+ assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.limit"}, data)
+ assert not _evaluate_rule({"Variable": "$.value", "NumericLessThanPath": "$.limit"}, data)
+
+
+def test_numeric_path_operators_reject_non_numeric_rhs():
+ """A non-numeric RHS (missing, string, null, or bool) must make the rule False:
+ no TypeError from comparing a number to a string and no bool coerced to int
+ (e.g. ``1 >= True`` must not match)."""
+ cases = [
+ ("missing", {}),
+ ("text", {"text": "3"}),
+ ("null", {"null": None}),
+ ("flag", {"flag": True}),
+ ]
+ for op in (
+ "NumericLessThanPath",
+ "NumericGreaterThanPath",
+ "NumericLessThanEqualsPath",
+ "NumericGreaterThanEqualsPath",
+ ):
+ for rhs_key, data in cases:
+ data = {**data, "value": 1}
+ # "1 >= True" is True under raw Python int coercion; the guarded
+ # comparison must return False instead.
+ assert not _evaluate_rule({"Variable": "$.value", op: f"$.{rhs_key}"}, data), (
+ op,
+ rhs_key,
+ )
+
+
+def test_numeric_path_operators_accept_int_float_rhs_mix():
+ """int LHS vs float RHS (and vice versa) still compares numerically."""
+ assert _evaluate_rule(
+ {"Variable": "$.value", "NumericLessThanPath": "$.limit"}, {"value": 1, "limit": 1.5}
+ )
+ assert _evaluate_rule(
+ {"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.limit"}, {"value": 2.0, "limit": 2}
+ )
+
+
+def test_numeric_equals_path_still_works():
+ """Regression guard for the pre-existing NumericEqualsPath operator."""
+ data = {"value": 7, "expected": 7}
+ assert _evaluate_rule({"Variable": "$.value", "NumericEqualsPath": "$.expected"}, data)
+ assert not _evaluate_rule({"Variable": "$.value", "NumericEqualsPath": "$.other"}, {"value": 7, "other": 8})