Skip to content

Commit

Permalink
Allow for variable length lists if full_list_validation is False
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfe1 committed Nov 11, 2020
1 parent 5207a0d commit d7dcf52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/Zoomba/APILibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def validate_response_contains_expected_response(self, json_actual_response, exp
if actual_response_dict == expected_response_dict:
return
self.key_by_key_validator(actual_response_dict, expected_response_dict, ignored_keys,
unmatched_keys_list, **kwargs)
unmatched_keys_list, full_list_validation=full_list_validation, **kwargs)
self.generate_unmatched_keys_error_message(unmatched_keys_list)
return
if isinstance(actual_response_dict, list) and actual_response_dict:
Expand All @@ -174,7 +174,7 @@ def validate_response_contains_expected_response(self, json_actual_response, exp
try:
if exp_item[identity_key] == actual_item[identity_key]:
self.key_by_key_validator(actual_item, exp_item, ignored_keys, unmatched_keys_list,
**kwargs)
full_list_validation=full_list_validation, **kwargs)
self.generate_unmatched_keys_error_message(unmatched_keys_list)
break
elif actual_response_dict[-1] == actual_item:
Expand Down Expand Up @@ -243,7 +243,7 @@ def validate_response_contains_correct_number_of_items(self, json_actual_respons
zoomba.fail("The response is not a list:\nActual Response:\n" + str(actual_response_dict))

def key_by_key_validator(self, actual_dictionary, expected_dictionary, ignored_keys=None, unmatched_keys_list=None,
parent_key=None, **kwargs):
parent_key=None, full_list_validation=False, **kwargs):
""" This method is used to find and verify the value of every key in the expectedItem dictionary when compared
against a single dictionary actual_item, unless any keys are included on the ignored_keys array./n
Expand All @@ -269,16 +269,17 @@ def key_by_key_validator(self, actual_dictionary, expected_dictionary, ignored_k
zoomba.fail("Key not found in Actual : " + str(actual_dictionary) + " Key: " + str(key))
continue
if isinstance(value, list):
if len(value) != len(actual_dictionary[key]):
if full_list_validation and len(value) != len(actual_dictionary[key]):
# if len(value) != len(actual_dictionary[key]):
zoomba.fail("Arrays not the same length:" + \
"\nExpected: " + str(value) + \
"\nActual: " + str(actual_dictionary[key]))
continue
self._key_by_key_list(key, value, actual_dictionary, unmatched_keys_list, ignored_keys, parent_key,
**kwargs)
full_list_validation=full_list_validation, **kwargs)
elif isinstance(value, dict):
self._key_by_key_dict(key, value, actual_dictionary, expected_dictionary, unmatched_keys_list,
ignored_keys, **kwargs)
ignored_keys, full_list_validation=full_list_validation, **kwargs)
elif isinstance(expected_dictionary[key], str) and not expected_dictionary[key].isdigit():
try:
parse(expected_dictionary[key])
Expand Down Expand Up @@ -352,7 +353,7 @@ def generate_unmatched_keys_error_message(self, unmatched_keys):
zoomba.fail(keys_error_msg + "\nPlease see differing value(s)")

def _key_by_key_list(self, key, value, actual_dictionary, unmatched_keys_list=None, ignored_keys=None,
parent_key=None, **kwargs):
parent_key=None, full_list_validation=False, **kwargs):
for index, item in enumerate(value):
if isinstance(item, str):
if value != actual_dictionary[key]:
Expand All @@ -369,15 +370,16 @@ def _key_by_key_list(self, key, value, actual_dictionary, unmatched_keys_list=No
else:
current_unmatched_length = 0
self.key_by_key_validator(temp_actual_dict, temp_expected_dict,
ignored_keys, unmatched_keys_list, parent_key=key, **kwargs)
ignored_keys, unmatched_keys_list, parent_key=key,
full_list_validation=full_list_validation, **kwargs)
if unmatched_keys_list is None:
continue
else:
_unmatched_list_check(unmatched_keys_list, current_unmatched_length,
key, index, parent_key, is_list=True)

def _key_by_key_dict(self, key, value, actual_dictionary, expected_dictionary, unmatched_keys_list=None,
ignored_keys=None, **kwargs):
ignored_keys=None, full_list_validation=False, **kwargs):
try:
if len(value) != len(actual_dictionary[key]):
zoomba.fail("Dicts do not match:" + \
Expand All @@ -392,7 +394,8 @@ def _key_by_key_dict(self, key, value, actual_dictionary, expected_dictionary, u
if unmatched_keys_list is not None:
current_unmatched_length = len(unmatched_keys_list)
self.key_by_key_validator(actual_dictionary[key], expected_dictionary[key],
ignored_keys, unmatched_keys_list, parent_key=key, **kwargs)
ignored_keys, unmatched_keys_list, parent_key=key,
full_list_validation=full_list_validation, **kwargs)
if unmatched_keys_list is None:
return
_unmatched_list_check(unmatched_keys_list, current_unmatched_length, key)
Expand All @@ -403,7 +406,7 @@ def full_list_validation(self, actual_response_dict, expected_response_dict, unm
return
for actual_item, expected_item in zip(actual_response_dict, expected_response_dict):
self.key_by_key_validator(actual_item, expected_item, ignored_keys, unmatched_keys_list,
**kwargs)
full_list_validation=True, **kwargs)
if unmatched_keys_list:
unmatched_keys_list.append(("------------------\n" + "Full List Breakdown:",
"Expected: " + str(expected_response_dict),
Expand Down
3 changes: 1 addition & 2 deletions test/API/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def test_key_by_key_validator_list_dict_embedded_list_fail(self):
assert unmatched == [('------------------\nKey: a[0].b[0].c', 'Expected: 5', 'Actual: 4')]

def test__unmatched_list_check_parent_not_list_not_equal(self):
library = APILibrary()
unmatched = [('------------------\nKey: c', 'Expected: 5', 'Actual: 4')]
_unmatched_list_check(unmatched, 0, "a", index=None, parent_key="b", is_list=False)
assert unmatched == [('------------------\nKey: a.c', 'Expected: 5', 'Actual: 4')]
Expand All @@ -164,7 +163,7 @@ def test_key_by_key_validator_key_not_in_actual_fail(self, fail):
@patch('robot.libraries.BuiltIn.BuiltIn.fail')
def test_key_by_key_validator_list_not_same_length_fail(self, fail):
library = APILibrary()
library.key_by_key_validator({"a": ["1", "2"]}, {"a": ["1"]})
library.key_by_key_validator({"a": ["1", "2"]}, {"a": ["1"]}, full_list_validation=True)
fail.assert_called_with("Arrays not the same length:\nExpected: ['1']\nActual: ['1', '2']")

@patch('robot.libraries.BuiltIn.BuiltIn.fail')
Expand Down

0 comments on commit d7dcf52

Please sign in to comment.