Skip to content

Commit eb027c8

Browse files
authored
Merge pull request #44 from elrandira/master
issue #43 backward compability for issue #5
2 parents 1526afc + c90d179 commit eb027c8

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

JSONLibrary/JSONLibraryKeywords.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,25 @@ def add_object_to_json(self, json_object, json_path, object_to_add):
8686
return json_object
8787

8888
@keyword("Get Value From Json")
89-
def get_value_from_json(self, json_object, json_path):
89+
def get_value_from_json(self, json_object, json_path, fail_on_empty=False):
9090
"""Get Value From JSON using JSONPath
9191
9292
Arguments:
9393
- json_object: json as a dictionary object.
9494
- json_path: jsonpath expression
95+
- fail_on_empty: fail the testcases if nothing is returned
9596
9697
Return array of values
9798
9899
Examples:
99100
| ${values}= | Get Value From Json | ${json} | $..phone_number |
101+
| ${values}= | Get Value From Json | ${json} | $..missing | fail_on_empty=${True} |
100102
"""
101103
json_path_expr = parse(json_path)
102104
rv=json_path_expr.find(json_object)
103-
# make the keyword fails if nothing was return
104-
assert_true(rv is not None and len(rv)!=0,
105-
f"Get Value From Json keyword failed to find a value for {json_path}")
105+
# optional: make the keyword fails if nothing was return
106+
if fail_on_empty is True and (rv is None or len(rv)==0):
107+
fail(f"Get Value From Json keyword failed to find a value for {json_path}")
106108
return [match.value for match in rv]
107109

108110
@keyword("Update Value To Json")
@@ -210,7 +212,7 @@ def should_have_value_in_json(self, json_object, json_path):
210212
| Should Have Value In Json | ${json} | $..id_card_number |
211213
"""
212214
try:
213-
self.get_value_from_json(json_object, json_path)
215+
self.get_value_from_json(json_object, json_path, fail_on_empty=True)
214216
except AssertionError:
215217
fail(f"No value found for path {json_path}")
216218

@@ -229,7 +231,7 @@ def should_not_have_value_in_json(self, json_object, json_path):
229231
| Should Not Have Value In Json | ${json} | $..id_card_number |
230232
"""
231233
try:
232-
rv=self.get_value_from_json(json_object, json_path)
234+
rv=self.get_value_from_json(json_object, json_path, fail_on_empty=True)
233235
except AssertionError:
234236
pass
235237
else:

JSONLibrary/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
VERSION = "0.4"
4+
VERSION = "0.4.1"

acceptance/JSONLibrary.robot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ TestGetValueByJSONPath
2626
${values}= Get Value From Json ${json_obj} $..address.postalCode
2727
Should Be Equal As Strings ${values[0]} 630-0192
2828

29+
${values}= Get Value From Json ${json_obj} $..errorField
30+
${size}= Get Length ${values}
31+
Should Be Equal As Integers ${size} 0
32+
2933
TestErrorGetValueByJSONPath
3034
[Documentation] Check Get Value From JSON can fail if no match is found
31-
Run Keyword And Expect Error *failed to find* Get Value From Json ${json_obj} $..errorField
35+
Run Keyword And Expect Error *failed to find* Get Value From Json ${json_obj} $..errorField fail_on_empty=${True}
3236

3337
TestUpdateValueByJSONPath
3438
[Documentation] Update value to json object using JSONPath

docs/JSONLibrary.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/test_JSONLibrary.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ def test_get_empty_list_from_json_path(self):
5353

5454
def test_get_value_from_json_path_not_found(self):
5555
json_path = '$..notfound'
56-
self.assertRaises(AssertionError, self.test.get_value_from_json, self.json, json_path)
56+
self.assertRaises(AssertionError, self.test.get_value_from_json, self.json, json_path, fail_on_empty=True)
57+
58+
# backward-compatilibity, fail_on_empty is False by default
59+
values = self.test.get_value_from_json(self.json, json_path)
60+
expected_result = []
61+
self.assertListEqual(values, expected_result)
5762

5863
def test_has_value_from_json_path_passed(self):
5964
json_path = '$..isMarried'

0 commit comments

Comments
 (0)