Skip to content

Commit bfae3d9

Browse files
author
Daniel Yoo
committed
Add choice rule serialization test and improve choice rule documentation
1 parent 16177d7 commit bfae3d9

File tree

2 files changed

+106
-9
lines changed

2 files changed

+106
-9
lines changed

src/stepfunctions/steps/choice_rule.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def IsNull(cls, variable, value):
613613
614614
Args:
615615
variable (str): Path to the variable to compare.
616-
value (bool): Constant value to compare `variable` against.
616+
value (bool): Whether the value at variable is equal to the JSON literal null or not.
617617
618618
Returns:
619619
Rule: Rule with `IsNull` operator.
@@ -627,7 +627,7 @@ def IsPresent(cls, variable, value):
627627
628628
Args:
629629
variable (str): Path to the variable to compare.
630-
value (bool): Constant value to compare `variable` against.
630+
value (bool): Whether a field at variable exists in the input or not.
631631
632632
Returns:
633633
Rule: Rule with `IsPresent` operator.
@@ -641,7 +641,7 @@ def IsString(cls, variable, value):
641641
642642
Args:
643643
variable (str): Path to the variable to compare.
644-
value (bool): Constant value to compare `variable` against.
644+
value (bool): Whether the value at variable is a string or not.
645645
646646
Returns:
647647
Rule: Rule with `IsString` operator.
@@ -655,7 +655,7 @@ def IsNumeric(cls, variable, value):
655655
656656
Args:
657657
variable (str): Path to the variable to compare.
658-
value (bool): Constant value to compare `variable` against.
658+
value (bool): Whether the value at variable is a number or not.
659659
660660
Returns:
661661
Rule: Rule with `IsNumeric` operator.
@@ -669,7 +669,7 @@ def IsTimestamp(cls, variable, value):
669669
670670
Args:
671671
variable (str): Path to the variable to compare.
672-
value (bool): Constant value to compare `variable` against.
672+
value (bool): Whether the value at variable is a timestamp or not.
673673
674674
Returns:
675675
Rule: Rule with `IsTimestamp` operator.
@@ -683,7 +683,7 @@ def IsBoolean(cls, variable, value):
683683
684684
Args:
685685
variable (str): Path to the variable to compare.
686-
value (bool): Constant value to compare `variable` against.
686+
value (bool): Whether the value at variable is a boolean or not.
687687
688688
Returns:
689689
Rule: Rule with `IsBoolean` operator.

tests/unit/test_choice_rule.py

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_path_function_value_must_be_consistent():
7474
func('$.Variable', 'string')
7575

7676
def test_is_function_value_must_be_consistent():
77-
is_functions = {
77+
type_functions = {
7878
'IsPresent',
7979
'IsNull',
8080
'IsString',
@@ -83,13 +83,110 @@ def test_is_function_value_must_be_consistent():
8383
'IsTimestamp'
8484
}
8585

86-
for is_function in is_functions:
87-
func = getattr(ChoiceRule, is_function)
86+
for type_function in type_functions:
87+
func = getattr(ChoiceRule, type_function)
8888
with pytest.raises(ValueError):
8989
func('$.Variable', 'string')
9090
with pytest.raises(ValueError):
9191
func('$.Variable', 101)
9292

93+
# tox -e py37 -- -s -vv tests/unit/test_choice_rule.py::test_dynamic_comparator_serialization
94+
def test_static_comparator_serialization():
95+
string_timestamp_static_comparators = {
96+
'StringEquals',
97+
'StringLessThan',
98+
'StringLessThanEquals',
99+
'StringGreaterThan',
100+
'StringGreaterThanEquals',
101+
'TimestampEquals',
102+
'TimestampLessThan',
103+
'TimestampGreaterThan',
104+
'TimestampLessThanEquals'
105+
}
106+
107+
for string_timestamp_static_comparator in string_timestamp_static_comparators:
108+
type_rule = getattr(ChoiceRule, string_timestamp_static_comparator)('$.input', 'hello')
109+
expected_dict = {}
110+
expected_dict['Variable'] = '$.input'
111+
expected_dict[string_timestamp_static_comparator] = 'hello'
112+
assert type_rule.to_dict() == expected_dict
113+
114+
number_static_comparators = {
115+
'NumericEquals',
116+
'NumericLessThan',
117+
'NumericGreaterThan',
118+
'NumericLessThanEquals',
119+
'NumericGreaterThanEquals'
120+
}
121+
122+
for number_static_comparator in number_static_comparators:
123+
type_rule = getattr(ChoiceRule, number_static_comparator)('$.input', 123)
124+
expected_dict = {}
125+
expected_dict['Variable'] = '$.input'
126+
expected_dict[number_static_comparator] = 123
127+
assert type_rule.to_dict() == expected_dict
128+
129+
boolean_static_comparators = {
130+
'BooleanEquals'
131+
}
132+
133+
for boolean_static_comparator in boolean_static_comparators:
134+
type_rule = getattr(ChoiceRule, boolean_static_comparator)('$.input', False)
135+
expected_dict = {}
136+
expected_dict['Variable'] = '$.input'
137+
expected_dict[boolean_static_comparator] = False
138+
assert type_rule.to_dict() == expected_dict
139+
140+
def test_dynamic_comparator_serialization():
141+
dynamic_comparators = {
142+
'StringEqualsPath',
143+
'StringLessThanPath',
144+
'StringLessThanEqualsPath',
145+
'StringGreaterThanPath',
146+
'StringGreaterThanEqualsPath',
147+
'TimestampEqualsPath',
148+
'TimestampLessThanPath',
149+
'TimestampGreaterThanPath',
150+
'TimestampLessThanEqualsPath',
151+
'NumericEqualsPath',
152+
'NumericLessThanPath',
153+
'NumericGreaterThanPath',
154+
'NumericLessThanEqualsPath',
155+
'NumericGreaterThanEqualsPath',
156+
'BooleanEqualsPath'
157+
}
158+
159+
for dynamic_comparator in dynamic_comparators:
160+
type_rule = getattr(ChoiceRule, dynamic_comparator)('$.input', '$.input2')
161+
expected_dict = {}
162+
expected_dict['Variable'] = '$.input'
163+
expected_dict[dynamic_comparator] = '$.input2'
164+
assert type_rule.to_dict() == expected_dict
165+
166+
def test_type_check_comparators_serialization():
167+
type_comparators = {
168+
'IsPresent',
169+
'IsNull',
170+
'IsString',
171+
'IsNumeric',
172+
'IsBoolean',
173+
'IsTimestamp'
174+
}
175+
176+
for type_comparator in type_comparators:
177+
type_rule = getattr(ChoiceRule, type_comparator)('$.input', True)
178+
expected_dict = {}
179+
expected_dict['Variable'] = '$.input'
180+
expected_dict[type_comparator] = True
181+
assert type_rule.to_dict() == expected_dict
182+
183+
def test_string_matches_serialization():
184+
string_matches_rule = ChoiceRule.StringMatches('$.input', 'hello*world\\*')
185+
assert string_matches_rule.to_dict() == {
186+
'Variable': '$.input',
187+
'StringMatches': 'hello*world\\*'
188+
}
189+
93190
def test_rule_serialization():
94191
bool_rule = ChoiceRule.BooleanEquals('$.BooleanVariable', True)
95192
assert bool_rule.to_dict() == {

0 commit comments

Comments
 (0)