diff --git a/jsoncompare/jsoncompare.py b/jsoncompare/jsoncompare.py index 9a2357f..64c5424 100644 --- a/jsoncompare/jsoncompare.py +++ b/jsoncompare/jsoncompare.py @@ -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: @@ -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( @@ -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)) diff --git a/jsoncompare/test/test_jsoncompare.py b/jsoncompare/test/test_jsoncompare.py index 541ddfb..ea72c29 100755 --- a/jsoncompare/test/test_jsoncompare.py +++ b/jsoncompare/test/test_jsoncompare.py @@ -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()