From 4443a226097284f871677d1e1ed21f6ea82e808d Mon Sep 17 00:00:00 2001 From: Network to Code Date: Thu, 21 Jul 2022 09:31:42 +0200 Subject: [PATCH 1/8] add tests for issue 67 --- tests/test_bugs.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/test_bugs.py diff --git a/tests/test_bugs.py b/tests/test_bugs.py new file mode 100644 index 0000000..4f80fae --- /dev/null +++ b/tests/test_bugs.py @@ -0,0 +1,23 @@ +"""Test GitHub issues.""" +import pytest +from netcompare import CheckType +from .utility import ASSERT_FAIL_MESSAGE + + +issue_67 = ( + {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}, + [] + ) + +issue_67_test = [ + issue_67, +] + + +@pytest.mark.parametrize("data, expected_output", issue_67_test) +def test_issue_67(data, expected_output): + """Resolve issue 67: https://github.com/networktocode-llc/netcompare/issues/67""" + my_jmspath = "global[*]" + my_check = CheckType.init(check_type="exact_match") + pre_value = my_check.get_value(output=data, path=my_jmspath) + assert pre_value == pre_value, ASSERT_FAIL_MESSAGE.format(output=pre_value, expected_output=expected_output) From 338030c3dd23f622783296175ab0299b821b0b68 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Thu, 21 Jul 2022 12:12:44 +0200 Subject: [PATCH 2/8] FIx 67 --- netcompare/check_types.py | 6 ++++++ tests/test_bugs.py | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/netcompare/check_types.py b/netcompare/check_types.py index 10ea841..62912af 100644 --- a/netcompare/check_types.py +++ b/netcompare/check_types.py @@ -1,5 +1,6 @@ """CheckType Implementation.""" import re +import json import warnings from typing import Mapping, Tuple, List, Dict, Any, Union from abc import ABC, abstractmethod @@ -56,6 +57,11 @@ def get_value(output: Union[Mapping, List], path: str = "*", exclude: List = Non Returns: Evaluated data, may be anything depending on JMESPath used. """ + try: + json.loads(output) + except TypeError: + raise TypeError(f"'output' must be a valid JSON object. You have {type(output)}") + if exclude and isinstance(output, Dict): if not isinstance(exclude, list): raise ValueError(f"Exclude list must be defined as a list. You have {type(exclude)}") diff --git a/tests/test_bugs.py b/tests/test_bugs.py index 4f80fae..f7739c7 100644 --- a/tests/test_bugs.py +++ b/tests/test_bugs.py @@ -6,7 +6,7 @@ issue_67 = ( {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}, - [] + "TypeError: 'output' must be a valid JSON object. You have " ) issue_67_test = [ @@ -19,5 +19,9 @@ def test_issue_67(data, expected_output): """Resolve issue 67: https://github.com/networktocode-llc/netcompare/issues/67""" my_jmspath = "global[*]" my_check = CheckType.init(check_type="exact_match") - pre_value = my_check.get_value(output=data, path=my_jmspath) - assert pre_value == pre_value, ASSERT_FAIL_MESSAGE.format(output=pre_value, expected_output=expected_output) + with pytest.raises(TypeError) as error: + my_check.get_value(output=data, path=my_jmspath)() # pylint: disable=E0110 + + assert ( + "'output' must be a valid JSON object. You have " in error.value.__str__() + ) \ No newline at end of file From 41d8477334d19be27a09d7d5ed7f8cabb4a29eb4 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Mon, 25 Jul 2022 09:49:31 +0200 Subject: [PATCH 3/8] raise error if JMSPath retunrs None --- netcompare/check_types.py | 8 +++----- tests/test_bugs.py | 9 ++++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/netcompare/check_types.py b/netcompare/check_types.py index 62912af..110394d 100644 --- a/netcompare/check_types.py +++ b/netcompare/check_types.py @@ -57,11 +57,6 @@ def get_value(output: Union[Mapping, List], path: str = "*", exclude: List = Non Returns: Evaluated data, may be anything depending on JMESPath used. """ - try: - json.loads(output) - except TypeError: - raise TypeError(f"'output' must be a valid JSON object. You have {type(output)}") - if exclude and isinstance(output, Dict): if not isinstance(exclude, list): raise ValueError(f"Exclude list must be defined as a list. You have {type(exclude)}") @@ -78,6 +73,9 @@ def get_value(output: Union[Mapping, List], path: str = "*", exclude: List = Non values = jmespath.search(jmespath_value_parser(path), output) + if values is None: + raise TypeError("JMSPath returned 'None'. Please, verify your JMSPath regex.") + # check for multi-nested lists if not found return here if not any(isinstance(i, list) for i in values): return values diff --git a/tests/test_bugs.py b/tests/test_bugs.py index f7739c7..a0d0734 100644 --- a/tests/test_bugs.py +++ b/tests/test_bugs.py @@ -5,8 +5,7 @@ issue_67 = ( - {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}, - "TypeError: 'output' must be a valid JSON object. You have " + {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} ) issue_67_test = [ @@ -14,8 +13,8 @@ ] -@pytest.mark.parametrize("data, expected_output", issue_67_test) -def test_issue_67(data, expected_output): +@pytest.mark.parametrize("data", issue_67_test) +def test_issue_67(data): """Resolve issue 67: https://github.com/networktocode-llc/netcompare/issues/67""" my_jmspath = "global[*]" my_check = CheckType.init(check_type="exact_match") @@ -23,5 +22,5 @@ def test_issue_67(data, expected_output): my_check.get_value(output=data, path=my_jmspath)() # pylint: disable=E0110 assert ( - "'output' must be a valid JSON object. You have " in error.value.__str__() + "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__() ) \ No newline at end of file From c04f5b923f20a044c7638e3811c81c2b0c889100 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Mon, 25 Jul 2022 09:58:48 +0200 Subject: [PATCH 4/8] fix tests --- netcompare/check_types.py | 1 - tests/test_bugs.py | 9 ++------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/netcompare/check_types.py b/netcompare/check_types.py index 110394d..55d194b 100644 --- a/netcompare/check_types.py +++ b/netcompare/check_types.py @@ -1,6 +1,5 @@ """CheckType Implementation.""" import re -import json import warnings from typing import Mapping, Tuple, List, Dict, Any, Union from abc import ABC, abstractmethod diff --git a/tests/test_bugs.py b/tests/test_bugs.py index a0d0734..135d95c 100644 --- a/tests/test_bugs.py +++ b/tests/test_bugs.py @@ -1,12 +1,9 @@ """Test GitHub issues.""" import pytest from netcompare import CheckType -from .utility import ASSERT_FAIL_MESSAGE -issue_67 = ( - {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} - ) +issue_67 = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} issue_67_test = [ issue_67, @@ -21,6 +18,4 @@ def test_issue_67(data): with pytest.raises(TypeError) as error: my_check.get_value(output=data, path=my_jmspath)() # pylint: disable=E0110 - assert ( - "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__() - ) \ No newline at end of file + assert "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__() From 85c782e93619a1f1e4b2541b6026b364be0b6eb6 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Mon, 25 Jul 2022 11:03:39 +0200 Subject: [PATCH 5/8] rename test file and function --- tests/{test_bugs.py => test_get_value.py} | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) rename tests/{test_bugs.py => test_get_value.py} (58%) diff --git a/tests/test_bugs.py b/tests/test_get_value.py similarity index 58% rename from tests/test_bugs.py rename to tests/test_get_value.py index 135d95c..836327b 100644 --- a/tests/test_bugs.py +++ b/tests/test_get_value.py @@ -3,16 +3,12 @@ from netcompare import CheckType -issue_67 = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} +my_data = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} -issue_67_test = [ - issue_67, -] - -@pytest.mark.parametrize("data", issue_67_test) -def test_issue_67(data): - """Resolve issue 67: https://github.com/networktocode-llc/netcompare/issues/67""" +@pytest.mark.parametrize("data", [my_data]) +def test_jmspath_return_none(data): + """Habdle exception when JMSPath retunr None.""" my_jmspath = "global[*]" my_check = CheckType.init(check_type="exact_match") with pytest.raises(TypeError) as error: From a3762daf1c6dbd3f65a2f2ca763bd07dd9623f95 Mon Sep 17 00:00:00 2001 From: Federico87 <15066806+lvrfrc87@users.noreply.github.com> Date: Mon, 25 Jul 2022 11:25:55 +0200 Subject: [PATCH 6/8] Update tests/test_get_value.py Co-authored-by: Christian Adell --- tests/test_get_value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_get_value.py b/tests/test_get_value.py index 836327b..c6bd39c 100644 --- a/tests/test_get_value.py +++ b/tests/test_get_value.py @@ -8,7 +8,7 @@ @pytest.mark.parametrize("data", [my_data]) def test_jmspath_return_none(data): - """Habdle exception when JMSPath retunr None.""" + """Handle exception when JMSPath retunr None.""" my_jmspath = "global[*]" my_check = CheckType.init(check_type="exact_match") with pytest.raises(TypeError) as error: From 2ed2f1ee741bc29401cd526b4f18b8bce295be9b Mon Sep 17 00:00:00 2001 From: Federico87 <15066806+lvrfrc87@users.noreply.github.com> Date: Mon, 25 Jul 2022 11:26:01 +0200 Subject: [PATCH 7/8] Update tests/test_get_value.py Co-authored-by: Christian Adell --- tests/test_get_value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_get_value.py b/tests/test_get_value.py index c6bd39c..c653090 100644 --- a/tests/test_get_value.py +++ b/tests/test_get_value.py @@ -6,7 +6,7 @@ my_data = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} -@pytest.mark.parametrize("data", [my_data]) +@pytest.mark.parametrize("data", my_data) def test_jmspath_return_none(data): """Handle exception when JMSPath retunr None.""" my_jmspath = "global[*]" From 58e82a54653d591cfd686a7033e292f6277e69d7 Mon Sep 17 00:00:00 2001 From: Federico87 <15066806+lvrfrc87@users.noreply.github.com> Date: Mon, 25 Jul 2022 11:26:07 +0200 Subject: [PATCH 8/8] Update tests/test_get_value.py Co-authored-by: Christian Adell --- tests/test_get_value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_get_value.py b/tests/test_get_value.py index c653090..13a2974 100644 --- a/tests/test_get_value.py +++ b/tests/test_get_value.py @@ -3,7 +3,7 @@ from netcompare import CheckType -my_data = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} +my_data = [{"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}] @pytest.mark.parametrize("data", my_data)