def test_unequal_list_nested_in_map(self): new_file = self.write_string_to_file('{"key1":["value2"]}', "item1") old_file = self.write_string_to_file('{"key1":["value1"]}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: key1[0]=value2', u'-: key1[0]=value1']) self.cleanup()
def test_switch_order(self): new_file = self.write_string_to_file('["test1", "test2"]', "item1") old_file = self.write_string_to_file('["test2", "test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [1]=test2', u'-: [0]=test2']) self.cleanup()
def test_similar_nested_maps_in_list(self): """ Similar maps in list """ new_file = self.write_string_to_file('[{"key1":"value1"},' '{"key1":"value1",' '"key2":"value2"}]', "item1") old_file = self.write_string_to_file('[{"key1":"value1",' '"key2":"value2"}]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) if sys.version_info.major == 3: self.assertEqual( sorted(comparison_tool.diff(use_model=False)), sorted([ "-: [0].key2=value2", "+: [1].key2=b'value2'", "+: [1].key1=b'value1'" ])) else: self.assertEqual(comparison_tool.diff(use_model=False), [ u'-: [0].key2=value2', u'+: [1].key2=value2', u'+: [1].key1=value1' ]) self.cleanup()
def test_add_to_nested_map_in_list(self): new_file = self.write_string_to_file('[{"key1":"value1", ' '"key2":"value2"}]', "item2") old_file = self.write_string_to_file('[{"key2":"value2"}]', "item1") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [0].key1=value1']) self.cleanup()
def test_simple_no_difference(self): """ No diffrence so empty list """ new_file = self.write_string_to_file('["test"]', "item1") old_file = self.write_string_to_file('["test"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), []) self.cleanup()
def test_simple_regex_no_difference(self): """ With no difference we should have an empty list """ new_file = self.write_string_to_file('["test"]', "item1") old_file = self.write_string_to_file('["(.*)"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=True), []) self.cleanup()
def test_nested_regex_matching(self): """ We cannot have a regular expression match a whole dictionary. """ filename1 = self.write_string_to_file('{"key1": {"key2":"value2"}}', "item1") filename2 = self.write_string_to_file('{"key1":"(.*)"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertFalse(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_list_subtract_multiple_matches(self): """ In case of multiple match pick up first """ new_file = self.write_string_to_file('["test1"]', "item1") old_file = self.write_string_to_file('["test1", "test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'-: [1]=test1']) self.cleanup()
def test_list_add_multiple_matches(self): """ When multiple list items match, we should pick the first one. """ new_file = self.write_string_to_file('["test1", "test1"]', "item1") old_file = self.write_string_to_file('["test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [1]=test1']) self.cleanup()
def test_regex_integer_match(self): """ Test to ensure that we can match integers even though their type is not text """ new_file = self.write_string_to_file('[42]', "item1") old_file = self.write_string_to_file('["[0-9]+"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=True), []) self.cleanup()
def test_ambiguous_regex(self): """ With an ambiguous regex, we should match the first item in the list """ new_file = self.write_string_to_file('["test1", "test2"]', "item1") old_file = self.write_string_to_file('["(.*)"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=True), [u'+: [1]=test2']) self.cleanup()
def test_simple_regex_difference(self): """ With no match we should show a list deletion then list addition. """ new_file = self.write_string_to_file('["test"]', "item1") old_file = self.write_string_to_file('["[0-9]+"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=True), [u'+: [0]=test', u'-: [0]=[0-9]+']) self.cleanup()
def test_list_addition_to_front(self): """ While adding an item to end we should get a string that tells someting was added """ new_file = self.write_string_to_file('["test2", "test1"]', "item1") old_file = self.write_string_to_file('["test1"]', "item0") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [0]=test2']) self.cleanup()
def test_list_subtract_multiple_matches(self): """ When multiple list items match, we should pick the first one. """ new_file = self.write_string_to_file('["test1"]', "item1") old_file = self.write_string_to_file('["test1", "test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'-: [1]=test1']) self.cleanup()
def test_list_subtraction_from_end(self): """ Message after addition to end """ new_file = self.write_string_to_file('["test2"]', "item1") old_file = self.write_string_to_file('["test2", "test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'-: [1]=test1']) self.cleanup()
def test_nested_regex_matching_on_key(self): """ Keys still match with a regex if their nested values are equal """ filename1 = self.write_string_to_file('{"key1": {"key2":"value2"}}', "item1") filename2 = self.write_string_to_file('{"(.*)": {"key2":"value2"}}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertTrue(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_list_order_with_regex(self): """ Regex matching should match the first item of the list, and then treat the rest as out of order """ new_file = self.write_string_to_file('["test1", "test2"]', "item1") old_file = self.write_string_to_file('["test2", "(.*)"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=True), [u'+: [1]=test2', u'-: [0]=test2']) self.cleanup()
def test_map_out_of_order_with_regex(self): """ We should still map map equivalencies with entries out of order """ filename1 = self.write_string_to_file('{"key1":"value1",' '"key2":"value2"}', "item1") filename2 = self.write_string_to_file('{"(.*)":"value2",' '"key1":"value1"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertTrue(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_multiple_nested_regex_matching(self): """ Test nested regex matches can still be determined """ filename1 = self.write_string_to_file('{"key1": {"key2":"value2",' '"key3":"value3"}}', "item1") filename2 = self.write_string_to_file('{"(.*)": {"key2":"(.*)",' '"(.*)":"value3"}}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertTrue(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_list_subtraction_from_end(self): """ When we add an item to the end of a list we should get an appropriate string in the diff telling us what was added """ new_file = self.write_string_to_file('["test2"]', "item1") old_file = self.write_string_to_file('["test2", "test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'-: [1]=test1']) self.cleanup()
def test_find_directory_match_regex(self): filename1 = self.write_string_to_file('["test1"]', "item1") dirname = self.write_files_to_directory({ "dir_item1": '["(.*)"]', "dir_item2": '["test2"]', "dir_item3": '["test3"]', }, "test") comparison_tool = JsonDiff.from_file(filename1, dirname) self.assertEqual(comparison_tool.comparison(use_model=True), "dir_item1") self.cleanup()
def test_list_order_with_regex(self): """ List order in a JSON document is deterministic. If there order is changed they should not match Even when using a regex, these should not match """ filename1 = self.write_string_to_file('["test1", "test2"]', "item1") filename2 = self.write_string_to_file('["test2", "(.*)"]', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertFalse(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_duplicate_value(self): """ Testing the duplicate """ new_file = self.write_string_to_file('{"key1":"value1", ' '"key2":"value1"}', "item1") old_file = self.write_string_to_file('{"key1":"value1"}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: key2=value1']) self.cleanup()
def test_simple_map_key_difference(self): """ If a key changes, we must treat the maps as completely different objects """ new_file = self.write_string_to_file('{"key1":"value1"}', "item1") old_file = self.write_string_to_file('{"key2":"value1"}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: key1=value1', u'-: key2=value1']) self.cleanup()
def test_simple_value_difference(self): """ Look out for changes """ new_file = self.write_string_to_file('["test"]', "item1") old_file = self.write_string_to_file('["other"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [0]=test', u'-: [0]=other']) self.cleanup()
def test_simple_map_value_difference(self): """ For maps we should be able to tell that a specific value changed if the keys match :return: """ new_file = self.write_string_to_file('{"key":"value1"}', "item1") old_file = self.write_string_to_file('{"key":"value2"}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'Changed: key to value1 from value2']) self.cleanup()
def test_find_directory_match_regex(self): filename1 = self.write_string_to_file('["test1"]', "item1") dirname = self.write_files_to_directory( { "dir_item1": '["(.*)"]', "dir_item2": '["test2"]', "dir_item3": '["test3"]', }, "test") comparison_tool = JsonDiff.from_file(filename1, dirname) self.assertEqual(comparison_tool.comparison(use_model=True), "dir_item1") self.cleanup()
def test_list_order(self): """ List order in a JSON document is deterministic. If there order is changed they should not match """ filename1 = self.write_string_to_file('["test1", "test2"]', "item1") filename2 = self.write_string_to_file('["test2", "test1"]', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) # We cannot match ambigous regular expressions # Todo create a recursive search to fix this problem self.assertFalse(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_simple_value_difference(self): """ Due to the nature of lists, we have to note specific insertions and deletions as opposed to listing 'Changes' :return: """ new_file = self.write_string_to_file('["test"]', "item1") old_file = self.write_string_to_file('["other"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [0]=test', u'-: [0]=other']) self.cleanup()
def test_simple_map_key_difference(self): """ If a key changes, we must treat the maps as completely different objects even if their values change :return: """ new_file = self.write_string_to_file('{"key1":"value1"}', "item1") old_file = self.write_string_to_file('{"key2":"value1"}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: key1=value1', u'-: key2=value1']) self.cleanup()
def test_duplicate_value(self): """ Since the keys are different we should match on the first key, and then treat the different key with the same value as a whole new object """ new_file = self.write_string_to_file('{"key1":"value1", ' '"key2":"value1"}', "item1") old_file = self.write_string_to_file('{"key1":"value1"}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: key2=value1']) self.cleanup()
def test_almost_ambiguous_regex(self): """ This test demonstrates that crafting slightly more specific regular expressions can fix the ambiguity bug """ filename1 = self.write_string_to_file('{"key1":"value1",' '"key2":"value2"}', "item1") filename2 = self.write_string_to_file('{"(.*)[1]+":"value1",' '"(.*)":"value2"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertTrue(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_ambiguous_regex(self): """ We cannot match ambigous regular expressions Todo create a recursive search to fix this problem """ filename1 = self.write_string_to_file('{"key1":"value1",' '"key2":"value2"}', "item1") filename2 = self.write_string_to_file('{"(.*)":"value1", ' '"(.*)":"value2"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertFalse(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_list_add_more_multiple_matches(self): """ When multiple list items match, we should pick the first one. This test ensures that the indices of additional duplicate matches are correct. """ new_file = self.write_string_to_file('["test1", "test1", "test1"]', "item1") old_file = self.write_string_to_file('["test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [1]=test1', u'+: [2]=test1']) self.cleanup()
def test_list_subtract_more_multiple_matches(self): """ When multiple list items match, we should pick the first one. This test ensures that the indices of additional duplicate matches are correct. """ new_file = self.write_string_to_file('["test1"]', "item1") old_file = self.write_string_to_file('["test1", "test1", "test1"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'-: [2]=test1', u'-: [1]=test1']) self.cleanup()
def test_simple_type_difference(self): """ When the type changes we should get an appropriate message in the difference :return: """ new_file = self.write_string_to_file('["test"]', "item1") old_file = self.write_string_to_file('{"key":"value"}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), ["TypeDifference : - is list: ([u'test']), " "but was dict: ({u'key': u'value'})"]) self.cleanup()
def test_duplicate_value(self): """ Since the keys are different we should match on the first key, and then treat the different key with the same value as a whole new object """ new_file = self.write_string_to_file( '{"key1":"value1", ' '"key2":"value1"}', "item1") old_file = self.write_string_to_file('{"key1":"value1"}', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: key2=value1']) self.cleanup()
def test_regex_for_map_type_difference(self): """ Trying to match a regular expression with a dictionary should result in a type difference """ filename1 = self.write_string_to_file('{"key1":{"key2":"value"}}', "item1") filename2 = self.write_string_to_file('{"key1":"(.*)"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertEqual(comparison_tool.diff(use_model=True), [ "TypeDifference : key1 - dict: ({u'key2': u'value'}), " "unicode: ((.*))"]) self.cleanup()
def test_map_out_of_order_with_regex(self): """ We should still map map equivalencies with entries out of order """ filename1 = self.write_string_to_file( '{"key1":"value1",' '"key2":"value2"}', "item1") filename2 = self.write_string_to_file( '{"(.*)":"value2",' '"key1":"value1"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertTrue(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_no_match_in_directory(self): """ When there is no match in a directory, we should return False """ filename = self.write_string_to_file('["test1"]', "item1") dirname = self.write_files_to_directory({ "dir_item1": '["test4"]', "dir_item2": '["test2"]', "dir_item3": '["test3"]', }, "test") comparison_tool = JsonDiff.from_file(filename, dirname) self.assertFalse(comparison_tool.comparison(use_model=True)) self.cleanup()
def test_completely_different_lists_difference(self): """ Changes in the list """ new_file = self.write_string_to_file( '["test1", "test2", "test3"]', "item1") old_file = self.write_string_to_file( '["other1", "other2", "other3"]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [u'+: [0]=test1', u'+: [1]=test2', u'+: [2]=test3', u'-: [0]=other1', u'-: [1]=other2', u'-: [2]=other3']) self.cleanup()
def test_find_match_in_directory(self): """ A match in a directory should return the filename that it matched """ filename = self.write_string_to_file('["test1"]', "item1") dirname = self.write_files_to_directory({ "dir_item1": '["test1"]', "dir_item2": '["test2"]', "dir_item3": '["test3"]', }, "test") comparison_tool = JsonDiff.from_file(filename, dirname) self.assertEqual(comparison_tool.comparison(use_model=True), "dir_item1") self.cleanup()
def test_regex_for_map_type_difference(self): """ Trying to match a regular expression with a dictionary should result in a type difference """ filename1 = self.write_string_to_file('{"key1":{"key2":"value"}}', "item1") filename2 = self.write_string_to_file('{"key1":"(.*)"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) if sys.version_info.major == 3: self.assertEqual(comparison_tool.diff(use_model=True), [ "TypeDifference : key1 - dict: ({'key2': 'value'}), " "str: ((.*))"]) else: self.assertEqual(comparison_tool.diff(use_model=True), [ "TypeDifference : key1 - dict: ({u'key2': u'value'}), " "unicode: ((.*))"]) self.cleanup()
def test_similar_nested_maps_in_list(self): """ We have a known issue where out of order nested maps will find a match for the first item in the new list if there is anything that can match at all, even if there is a better match elsewhere """ #todo fix this bug in matching new_file = self.write_string_to_file('[{"key1":"value1"},' '{"key1":"value1",' '"key2":"value2"}]', "item1") old_file = self.write_string_to_file('[{"key1":"value1",' '"key2":"value2"}]', "item2") comparison_tool = JsonDiff.from_file(new_file, old_file) self.assertEqual(comparison_tool.diff(use_model=False), [ u'-: [0].key2=value2', u'+: [1].key2=value2', u'+: [1].key1=value1' ]) #Ideally should return: +: [0].key1=value1 self.cleanup()
def test_regex_match_value(self): filename1 = self.write_string_to_file('{"key":"value"}', "item1") filename2 = self.write_string_to_file('{"key":"(.*)"}', "item2") comparison_tool = JsonDiff.from_file(filename1, filename2) self.assertEqual(comparison_tool.diff(use_model=True), []) self.cleanup()