Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions jsoncompare/jsoncompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _generate_pprint_json(value):
return json.dumps(value, sort_keys=True, indent=4)


def _is_dict_same(expected, actual, ignore_value_of_keys):
def _is_dict_same(expected, actual, ignore_value_of_keys, ignore_missing_keys=False):
# DAN - I had to flip flop this
for key in expected:
if not key in actual:
Expand All @@ -63,15 +63,15 @@ def _is_dict_same(expected, actual, ignore_value_of_keys):
if not key in ignore_value_of_keys:
# have to change order
#are_same_flag, stack = _are_same(actual[key], expected[key], ignore_value_of_keys)
are_same_flag, stack = _are_same(expected[key], actual[key],ignore_value_of_keys)
are_same_flag, stack = _are_same(expected[key], actual[key], ignore_value_of_keys, ignore_missing_keys)
if not are_same_flag:
return False, \
stack.append(StackItem('Different values', expected[key], actual[key]))
return True, Stack()

def _is_list_same(expected, actual, ignore_value_of_keys):
def _is_list_same(expected, actual, ignore_value_of_keys, ignore_missing_keys=False):
for i in xrange(len(expected)):
are_same_flag, stack = _are_same(expected[i], actual[i], ignore_value_of_keys)
are_same_flag, stack = _are_same(expected[i], actual[i], ignore_value_of_keys, ignore_missing_keys)
if not are_same_flag:
return False, \
stack.append(
Expand Down Expand Up @@ -134,13 +134,11 @@ def _are_same(expected, actual, ignore_value_of_keys, ignore_missing_keys=False)
expected,
actual))



if isinstance(expected, dict):
return _is_dict_same(expected, actual, ignore_value_of_keys)
return _is_dict_same(expected, actual, ignore_value_of_keys, ignore_missing_keys)

if isinstance(expected, list):
return _is_list_same(expected, actual, ignore_value_of_keys)
return _is_list_same(expected, actual, ignore_value_of_keys, ignore_missing_keys)

return False, Stack().append(StackItem('Unhandled Type: {0}'.format(type(expected)), expected, actual))

Expand Down
134 changes: 134 additions & 0 deletions jsoncompare/test/test_jsoncompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,140 @@ def test_contains_actual_smaller(self):
]
self.assertFalse(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is larger - it can (potentialy) contain all of the expected attributes
def test_contains_actual_bigger_nested(self):
actual = {
"inner": [
{"wtf": "omg"},
{"wtf1": "omg1"},
{"wtf3": "omg3"}
]
}
expected = {
"inner": [
{"wtf": "omg"},
{"wtf1": "omg1"}
]
}
self.assertTrue(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is smaller - it can NOT contain all of expected attributes
def test_contains_actual_smaller_nested(self):
actual = {
"inner": [
{"wtf": "omg"},
{"wtf1": "omg1"}
]
}
expected = {
"inner": [
{"wtf": "omg"},
{"wtf1": "omg1"},
{"wtf2": "omg2"}
]
}
self.assertFalse(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is larger - it can (potentialy) contain all of the expected attributes
def test_contains_nested_in_array(self):
actual = {
"inner": [
{"a": 1, "b": 2}
]
}
expected = {
"inner": [
{"a": 1}
]
}
self.assertTrue(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is larger - it can (potentialy) contain all of the expected attributes
def test_contains_nested_in_array_in_object(self):
actual = {
"outer": [
{"inner": {"a": 1, "b": 2}}
]
}
expected = {
"outer": [
{"inner": {"a": 1}}
]
}
self.assertTrue(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is larger - it can (potentialy) contain all of the expected attributes
def test_contains_nested_in_array_in_object_in_array(self):
actual = {
"outer": [
{
"inner": [
{"a": 1, "b": 2}
]
}
]
}
expected = {
"outer": [
{
"inner": [
{"b": 2}
]
}
]
}
self.assertTrue(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is larger - it can (potentialy) contain all of the expected attributes
def test_not_contains_nested_in_array(self):
actual = {
"inner": [
{"a": 1}
]
}
expected = {
"inner": [
{"a": 1, "b": 2}
]
}
self.assertFalse(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is larger - it can (potentialy) contain all of the expected attributes
def test_not_contains_nested_in_array_in_object(self):
actual = {
"outer": [
{"inner": {"a": 1}}
]
}
expected = {
"outer": [
{"inner": {"a": 1, "b": 2}}
]
}
self.assertFalse(jsoncompare.contains(expected, actual)[0])

# Test two json where Actual is larger - it can (potentialy) contain all of the expected attributes
def test_not_contains_nested_in_array_in_object_in_array(self):
actual = {
"outer": [
{
"inner": [
{"b": 2}
]
}
]
}
expected = {
"outer": [
{
"inner": [
{"a": 1, "b": 2}
]
}
]
}
self.assertFalse(jsoncompare.contains(expected, actual)[0])


if __name__ == '__main__':
unittest.main()