From 5e7ce915762b8b9f8fb9b57557c30614c57dee6b Mon Sep 17 00:00:00 2001 From: saravanan palanisamy Date: Sat, 17 Oct 2020 15:33:04 +0400 Subject: [PATCH] add expect_one_of feature in jsonpath tolerance Signed-off-by: saravanan palanisamy add debug log - jsonpath tolerance Signed-off-by: saravanan palanisamy add new field for expect alternative values Signed-off-by: saravanan palanisamy add debug log for expect_alt Signed-off-by: saravanan palanisamy add test cases for expect_alt Signed-off-by: saravanan palanisamy --- chaoslib/hypothesis.py | 15 ++++++++++++++- tests/test_tolerance.py | 27 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/chaoslib/hypothesis.py b/chaoslib/hypothesis.py index 862d7b1..8afdbf3 100644 --- a/chaoslib/hypothesis.py +++ b/chaoslib/hypothesis.py @@ -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"]))) diff --git a/tests/test_tolerance.py b/tests/test_tolerance.py index 22c0183..d0f00c7 100644 --- a/tests/test_tolerance.py +++ b/tests/test_tolerance.py @@ -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", @@ -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", @@ -201,7 +226,7 @@ def test_tolerance_jsonpath_must_match_expected_values(): assert within_tolerance( t, value={ 'foo': [{"baz": "hello"}, {"baz": "bonjour"}] - }, + }, ) is True