Skip to content

Commit 4ff7aa1

Browse files
author
Laurent PINSON
committed
issue #9
1 parent 67e1c9e commit 4ff7aa1

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

JSONLibrary/JSONLibraryKeywords.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
from robot.api.deco import keyword
77
from robot.utils.asserts import assert_true, fail
88
from jsonpath_ng import Index, Fields
9-
from jsonpath_ng.ext import parse
9+
from jsonpath_ng.ext import parse as parse_ng
1010
from .version import VERSION
1111

1212
__author__ = 'Traitanit Huangsri'
1313
__email__ = 'traitanit.hua@gmail.com'
1414
__version__ = VERSION
1515

16+
def parse(json_path):
17+
try:
18+
_rv=parse_ng(json_path)
19+
except BaseException as e:
20+
fail("Parser failed to undestand syntax '{}'. error message: \n{}\n\nYou may raise an issue on https://github.com/h2non/jsonpath-ng".format(json_path,e))
21+
return _rv
22+
1623

1724
class JSONLibraryKeywords(object):
1825
ROBOT_EXIT_ON_FAILURE = True

acceptance/JSONLibrary.robot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ TestShouldNotHaveValueByJSONPath
4848

4949
Run Keyword And Expect Error *Match found* Should Not Have Value In Json ${json_obj} $..isMarried
5050

51+
TestInvalidSyntaxByJSONPath
52+
[Documentation] Check that an invalid syntax fail the test and doesn't crash Robot
53+
${value}= Get Value From Json ${json_obj} $.bankAccounts[?(@.amount>=100)].bank
54+
Should Be Equal As Strings "${value}" "['WesternUnion', 'HSBC']"
55+
56+
${res}= Run Keyword And return status Get Value From Json ${json_obj} $.bankAccounts[?(@.amount=>100)].bank
57+
Should Not Be True ${res}
58+
5159
TestDeleteObjectByJSONPath
5260
[Documentation] Delete object from json object using JSONPath
5361
${json_obj}= Delete Object From Json ${json_obj} $..isMarried

tests/json/example.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
"number": "0123-4567-8999"
2525
}
2626
],
27+
"bankAccounts": [
28+
{
29+
"bank": "WesternUnion",
30+
"amount": "503"
31+
}, {
32+
"bank": "HSBC",
33+
"amount": "1320"
34+
}, {
35+
"bank": "GoldenSack",
36+
"amount": "-10"
37+
}
38+
],
2739
"siblings": [],
2840
"occupation": null
2941
}

tests/test_JSONLibrary.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ def test_delete_all_array_elements_from_json(self):
9999
expected_result = []
100100
self.assertListEqual(expected_result, json_object['phoneNumbers'])
101101

102+
def test_invalid_syntax_doesnt_crash(self):
103+
json_path = '$.bankAccounts[?(@.amount>=100)].bank'
104+
values = self.test.get_value_from_json(self.json, json_path)
105+
expected_result = ['WesternUnion', 'HSBC']
106+
self.assertListEqual(values, expected_result)
107+
108+
json_path = "$.bankAccounts[?(@.amount=>100)].bank"
109+
self.assertRaises(AssertionError, self.test.get_value_from_json, self.json, json_path)
110+
102111
def test_convert_json_to_string(self):
103112
json_str = self.test.convert_json_to_string(self.json)
104113
self.assertTrue(isinstance(json_str, str))

0 commit comments

Comments
 (0)