Skip to content

Commit 5e7ce91

Browse files
add expect_one_of feature in jsonpath tolerance
Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add debug log - jsonpath tolerance Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add new field for expect alternative values Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add debug log for expect_alt Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add test cases for expect_alt Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com>
1 parent f781dfe commit 5e7ce91

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

chaoslib/hypothesis.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,21 @@ def _(tolerance: dict, value: Any, configuration: Configuration = None,
334334
else:
335335
result = values == expect
336336

337+
if "expect" in tolerance and result is False:
338+
expect_alt = tolerance.get("expect_alt")
339+
if "expect_alt" in tolerance:
340+
if not isinstance(expect_alt, list):
341+
result = values == [expect_alt]
342+
else:
343+
result = values == expect_alt
344+
337345
if result is False:
338-
if "expect" in tolerance:
346+
if "expect" in tolerance and "expect_alt" in tolerance:
347+
logger.debug(
348+
"jsonpath found '{}' but expected '{}' or '{}'".format(
349+
str(values), str(tolerance["expect"]),
350+
str(tolerance["expect_alt"])))
351+
elif "expect" in tolerance:
339352
logger.debug(
340353
"jsonpath found '{}' but expected '{}'".format(
341354
str(values), str(tolerance["expect"])))

tests/test_tolerance.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ def test_tolerance_jsonpath_must_match_expected_value():
139139
}
140140
) is True
141141

142+
t = {
143+
"type": "jsonpath",
144+
"path": "$.foo[?(@.baz)].baz",
145+
"expect": [["hello", "bonjour"]],
146+
"expect_alt": [["hello", "joe"]]
147+
}
148+
ensure_hypothesis_tolerance_is_valid(t)
149+
assert within_tolerance(
150+
t, value={
151+
'foo': {"baz": ["hello", "joe"]}
152+
}
153+
) is True
142154

143155
t = {
144156
"type": "jsonpath",
@@ -152,6 +164,19 @@ def test_tolerance_jsonpath_must_match_expected_value():
152164
}
153165
) is True
154166

167+
t = {
168+
"type": "jsonpath",
169+
"path": "$.foo[?(@.baz)].baz",
170+
"expect": [[["hello"], ["bonjour"]]],
171+
"expect_alt": [[["hello"], ["joe"]]]
172+
}
173+
ensure_hypothesis_tolerance_is_valid(t)
174+
assert within_tolerance(
175+
t, value={
176+
'foo': {"baz": [["hello"], ["joe"]]}
177+
}
178+
) is True
179+
155180
t = {
156181
"type": "jsonpath",
157182
"path": "$.foo[?(@.baz)].baz",
@@ -201,7 +226,7 @@ def test_tolerance_jsonpath_must_match_expected_values():
201226
assert within_tolerance(
202227
t, value={
203228
'foo': [{"baz": "hello"}, {"baz": "bonjour"}]
204-
},
229+
},
205230
) is True
206231

207232

0 commit comments

Comments
 (0)