Skip to content

Commit

Permalink
add expect_one_of feature in jsonpath tolerance
Browse files Browse the repository at this point in the history
Signed-off-by: saravanan palanisamy <[email protected]>

add debug log - jsonpath tolerance

Signed-off-by: saravanan palanisamy <[email protected]>

add new field for expect alternative values

Signed-off-by: saravanan palanisamy <[email protected]>

add debug log for expect_alt

Signed-off-by: saravanan palanisamy <[email protected]>

add test cases for expect_alt

Signed-off-by: saravanan palanisamy <[email protected]>
  • Loading branch information
saravanan30erd committed Oct 19, 2020
1 parent f781dfe commit 5e7ce91
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
15 changes: 14 additions & 1 deletion chaoslib/hypothesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,21 @@ def _(tolerance: dict, value: Any, configuration: Configuration = None,
else:
result = values == expect

if "expect" in tolerance and result is False:
expect_alt = tolerance.get("expect_alt")
if "expect_alt" in tolerance:
if not isinstance(expect_alt, list):
result = values == [expect_alt]
else:
result = values == expect_alt

if result is False:
if "expect" in tolerance:
if "expect" in tolerance and "expect_alt" in tolerance:
logger.debug(
"jsonpath found '{}' but expected '{}' or '{}'".format(
str(values), str(tolerance["expect"]),
str(tolerance["expect_alt"])))
elif "expect" in tolerance:
logger.debug(
"jsonpath found '{}' but expected '{}'".format(
str(values), str(tolerance["expect"])))
Expand Down
27 changes: 26 additions & 1 deletion tests/test_tolerance.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ def test_tolerance_jsonpath_must_match_expected_value():
}
) is True

t = {
"type": "jsonpath",
"path": "$.foo[?(@.baz)].baz",
"expect": [["hello", "bonjour"]],
"expect_alt": [["hello", "joe"]]
}
ensure_hypothesis_tolerance_is_valid(t)
assert within_tolerance(
t, value={
'foo': {"baz": ["hello", "joe"]}
}
) is True

t = {
"type": "jsonpath",
Expand All @@ -152,6 +164,19 @@ def test_tolerance_jsonpath_must_match_expected_value():
}
) is True

t = {
"type": "jsonpath",
"path": "$.foo[?(@.baz)].baz",
"expect": [[["hello"], ["bonjour"]]],
"expect_alt": [[["hello"], ["joe"]]]
}
ensure_hypothesis_tolerance_is_valid(t)
assert within_tolerance(
t, value={
'foo': {"baz": [["hello"], ["joe"]]}
}
) is True

t = {
"type": "jsonpath",
"path": "$.foo[?(@.baz)].baz",
Expand Down Expand Up @@ -201,7 +226,7 @@ def test_tolerance_jsonpath_must_match_expected_values():
assert within_tolerance(
t, value={
'foo': [{"baz": "hello"}, {"baz": "bonjour"}]
},
},
) is True


Expand Down

0 comments on commit 5e7ce91

Please sign in to comment.