Skip to content

Commit 41d8477

Browse files
committed
raise error if JMSPath retunrs None
1 parent 338030c commit 41d8477

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

netcompare/check_types.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ def get_value(output: Union[Mapping, List], path: str = "*", exclude: List = Non
5757
Returns:
5858
Evaluated data, may be anything depending on JMESPath used.
5959
"""
60-
try:
61-
json.loads(output)
62-
except TypeError:
63-
raise TypeError(f"'output' must be a valid JSON object. You have {type(output)}")
64-
6560
if exclude and isinstance(output, Dict):
6661
if not isinstance(exclude, list):
6762
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
7873

7974
values = jmespath.search(jmespath_value_parser(path), output)
8075

76+
if values is None:
77+
raise TypeError("JMSPath returned 'None'. Please, verify your JMSPath regex.")
78+
8179
# check for multi-nested lists if not found return here
8280
if not any(isinstance(i, list) for i in values):
8381
return values

tests/test_bugs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55

66

77
issue_67 = (
8-
{"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}},
9-
"TypeError: 'output' must be a valid JSON object. You have <class 'dict'>"
8+
{"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}
109
)
1110

1211
issue_67_test = [
1312
issue_67,
1413
]
1514

1615

17-
@pytest.mark.parametrize("data, expected_output", issue_67_test)
18-
def test_issue_67(data, expected_output):
16+
@pytest.mark.parametrize("data", issue_67_test)
17+
def test_issue_67(data):
1918
"""Resolve issue 67: https://github.com/networktocode-llc/netcompare/issues/67"""
2019
my_jmspath = "global[*]"
2120
my_check = CheckType.init(check_type="exact_match")
2221
with pytest.raises(TypeError) as error:
2322
my_check.get_value(output=data, path=my_jmspath)() # pylint: disable=E0110
2423

2524
assert (
26-
"'output' must be a valid JSON object. You have <class 'dict'>" in error.value.__str__()
25+
"JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__()
2726
)

0 commit comments

Comments
 (0)