def test_diff_dot_keyname(self): a = {'a.b.c': 1, 'a':{'b':{'c':2}}} b = {'a.b.c': 2, 'a':{'b':{'c':2}}} diff = dictdiff.diff(a,b) self.assertEqual(len(diff), 1) self.assertEqual(diff[0][0], "changed") self.assertEqual(diff[0][1], "a\.b\.c") a = {'a.b.c': 1, 'a':{'b':{'c':2}}} b = {'a.b.c': 1, 'a':{'b':{'c':3}}} diff = dictdiff.diff(a,b) self.assertEqual(len(diff), 1) self.assertEqual(diff[0][0], "changed") self.assertEqual(diff[0][1], "a.b.c") a = {'a.b.c': 1, 'a':{'b':{'c':2}}} b = {'a.b.c': 2, 'a':{'b':{'c':3}}} diff = dictdiff.diff(a,b) self.assertEqual(len(diff), 2) print diff for x in diff: if x[1] == 'a.b.c': self.assertEqual(x[2][0], 2) self.assertEqual(x[2][1], 3) elif x[1] == 'a\.b\.c': self.assertEqual(x[2][0], 1) self.assertEqual(x[2][1], 2) else: raise("invalid key")
def test_diff_x(self): a = { "client": { "heart_beat": "1s", "metric_enabled": True, "transport_kafka": { "broker_list": [ "10.0.0.1:9092", "10.0.0.2:9092" ] }, "groups": [ { "prefix:": "oletest", "collector_win_pdh": [ {"interval": "15s"}, {"interval": "30s"} ] }, { "prefix:": "xxxxx", "collector_win_pdh": [ {"interval": "15s", "queries":["q1", "q2", "q3"]} ] } ]} } b = { "client": { "heart_beat": "1s", "metric_enabled": True, "transport_kafka": { "broker_list": [ "10.0.0.1:9092", "10.0.0.2:9092", "10.0.0.3:9092" ] }, "groups": [ { "collector_win_pdh": [ {"interval": "10s"}, {"interval": "15s"} ], "prefix:": "oletest", } ]} } diff = dictdiff.diff(a,b) for x in xrange(self.size): dictdiff.diff(a,b)
def test_no_changes_dict_with_list_of_dict(self): src = { 'name': 'NAME', 'data': [{ 'name': 'NAME_1', 'data': 'DATA_1' }, { 'name': 'NAME_2', 'data': 'DATA_2' }, { 'name': 'NAME_3', 'data': 'DATA_3' }] } comp = { 'name': 'NAME', 'data': [{ 'name': 'NAME_1', 'data': 'DATA_1' }, { 'name': 'NAME_2', 'data': 'DATA_2' }, { 'name': 'NAME_3', 'data': 'DATA_3' }] } d = diff(src, comp) self.assertEqual(0, len(d))
def test_update_dict_with_list_null(self): src = {'name': 'NAME', 'data': ['A', 'B', 'C']} comp = {'name': 'NAME_UPD', 'data': None} d = diff(src, comp) self.assertEqual(2, len(d)) self.assertTrue(_updated('name', 'NAME', 'NAME_UPD') in d) self.assertTrue(_updated('data', ['A', 'B', 'C'], None) in d)
def test_updated_property(self): src = {'name': 'NAME', 'desc': 'DESC'} comp = {'name': 'NAME_UPD', 'desc': 'DESC_UPD'} d = diff(src, comp) self.assertEqual(2, len(d)) self.assertTrue(_updated('name', 'NAME', 'NAME_UPD') in d) self.assertTrue(_updated('desc', 'DESC', 'DESC_UPD') in d)
def test_update_list_reduce(self): src = ['A', 'B', 'C'] comp = ['A', 'Z'] d = diff(src, comp) self.assertEqual(2, len(d)) self.assertTrue(_updated('[1]', 'B', 'Z') in d) self.assertTrue(_removed('[2]', 'C') in d)
def test_all_new(self): src = {} comp = {'name': 'NAME', 'desc': 'DESC'} d = diff(src, comp) self.assertEqual(2, len(d)) self.assertTrue(_added('name', 'NAME') in d) self.assertTrue(_added('desc', 'DESC') in d)
def test_update_list_extend(self): src = ['A', 'B', 'C'] comp = ['A', 'Z', 'C', 'D'] d = diff(src, comp) self.assertEqual(2, len(d)) self.assertTrue(_updated('[1]', 'B', 'Z') in d) self.assertTrue(_added('[3]', 'D') in d)
def test_update_dict_with_list_reduce(self): src = {'name': 'NAME', 'data': ['A', 'B', 'C']} comp = {'name': 'NAME_UPD', 'data': ['A', 'Z']} d = diff(src, comp) self.assertEqual(3, len(d)) self.assertTrue(_updated('name', 'NAME', 'NAME_UPD') in d) self.assertTrue(_updated('data[1]', 'B', 'Z') in d) self.assertTrue(_removed('data[2]', 'C') in d)
def test_diff_8(self): a = {'a':{'b':[{'c': [{'d':[{'e': 1, 'f': 2}]}]}]}, 'b': 1} b = {'b': 1} diff = dictdiff.diff(a,b) self.assertEqual(len(diff), 2) print diff self.assertEqual(diff[0][0], "removed") self.assertEqual(diff[1][0], "removed")
def test_update_dict_with_list_extend(self): src = {'name': 'NAME', 'data': ['A', 'B', 'C']} comp = {'name': 'NAME_UPD', 'data': ['A', 'Z', 'Y', 'D']} d = diff(src, comp) self.assertEqual(4, len(d)) self.assertTrue(_updated('name', 'NAME', 'NAME_UPD') in d) self.assertTrue(_updated('data[1]', 'B', 'Z') in d) self.assertTrue(_updated('data[2]', 'C', 'Y') in d) self.assertTrue(_added('data[3]', 'D') in d)
def test_new_dict_with_list(self): src = None comp = {'name': 'NAME', 'data': ['A', 'B', 'C']} d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue( _added('', { 'name': 'NAME', 'data': ['A', 'B', 'C'] }) in d)
def test_path_dot_keyname(self): a = {'a.b.c': 1, 'a':{'b':{'c':2}}} b = {'a.b.c': 2, 'a':{'b':{'c':3}}} diff1 = dictdiff.diff(a, b) print diff1 print " ------- a: ", a dictdiff.patch(a, diff1) print " ------- a: ", a self.assertEqual(a['a.b.c'], 2) self.assertEqual(a['a']['b']['c'], 3)
def test_new_dict(self): src = None comp = {'name': 'NAME_UPD', 'desc': 'DESC_UPD'} d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue( _added('', { 'name': 'NAME_UPD', 'desc': 'DESC_UPD' }) in d)
def test_diff_7(self): a = {'a':{'b':[{'c': [{'d':[{'e': 1, 'f': 2}]}]}]}} b = {'a':{'b':[{'c': [{'d':[{'e': 1}]}]}]}} diff = dictdiff.diff(a,b) self.assertEqual(len(diff), 1) print diff res = diff[0] self.assertEqual(res[0], "removed") self.assertEqual(res[1], 'a.b.[0].c.[0].d.[0].f') self.assertEqual(res[2], 2)
def test_delete_dict(self): src = {'name': 'NAME_UPD', 'desc': 'DESC_UPD'} comp = None d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue( _removed('', { 'name': 'NAME_UPD', 'desc': 'DESC_UPD' }) in d)
def test_diff_2(self): # simple add a = {'a': 1} b = {'a': 1, 'b': 2} diff = dictdiff.diff(a, b) self.assertEqual(len(diff), 1) # print diff res = diff[0] self.assertEqual(res[0], "added") self.assertEqual(res[1], "b") self.assertEqual(res[2], 2)
def test_diff_2(self): # simple removed a = {'a': 1} b = {} diff = dictdiff.diff(a, b) self.assertEqual(len(diff), 1) # print diff res = diff[0] self.assertEqual(res[0], "removed") self.assertEqual(res[1], "a") self.assertEqual(res[2], 1)
def test_diff_4(self): a = {'a': {'b': 1, 'c':2}} b = {'a': {'b': 1, 'c':3}} diff = dictdiff.diff(a,b) self.assertEqual(len(diff), 1) print diff res = diff[0] self.assertEqual(res[0], "changed") self.assertEqual(res[1], "a.c") self.assertEqual(res[2][0], 2) self.assertEqual(res[2][1], 3)
def test_diff_1(self): # simple change a = {'a': 1} b = {'a': 2} diff = dictdiff.diff(a, b) self.assertEqual(len(diff), 1) # print diff res = diff[0] self.assertEqual(res[0], "changed") self.assertEqual(res[1], "a") self.assertEqual(res[2][0], 1) self.assertEqual(res[2][1], 2)
def test_patch_2(self): a = {'a':{'b':[{'c': [{'d':[{'e': 1, 'f': 2}]}]}]}, 'b': 1} b = {'a':{'b':[{'c': [{'d':[{'e': 1}]}]}]}, 'b': 5} print " 1 ====================-----" print " 1 ---- ", a diff1 = dictdiff.diff(a, b) print diff1 dictdiff.patch(a, diff1) print " 1 ---- ", a print " 1 ====================-----" with self.assertRaises(KeyError): dictdiff.dot_lookup(a, 'a.b.[0].c.[0].d.[0].f') self.assertEqual(a['b'], 5)
def test_diff_3(self): a = {'a': 1} b = {'b': 1} diff = dictdiff.diff(a,b) self.assertEqual(len(diff), 2) print diff res = diff[0] self.assertEqual(res[0], "added") self.assertEqual(res[1], "b") self.assertEqual(res[2], 1) res = diff[1] self.assertEqual(res[0], "removed") self.assertEqual(res[1], "a") self.assertEqual(res[2], 1)
def test_update_list_of_lists(self): src = [ ['A', 'B', 'C'], ['X', 'Y', 'Z'], ['1', '2', '3'], ] comp = [['A1', 'B', 'C', 'D'], ['X1', 'Y'], ['1A', '2', '3'], ['D', 'E']] d = diff(src, comp) self.assertEqual(6, len(d)) self.assertTrue(_updated('[0][0]', 'A', 'A1') in d) self.assertTrue(_added('[0][3]', 'D') in d) self.assertTrue(_updated('[1][0]', 'X', 'X1') in d) self.assertTrue(_removed('[1][2]', 'Z') in d) self.assertTrue(_updated('[2][0]', '1', '1A') in d) self.assertTrue(_added('[3]', ['D', 'E']) in d)
def test_new_dict_with_dict(self): src = None comp = { 'name': 'NAME_UPD', 'data': { 'attr1': 'VALUE_1', 'attr2': 'VALUE_2' } } d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue( _added( '', { 'name': 'NAME_UPD', 'data': { 'attr1': 'VALUE_1', 'attr2': 'VALUE_2' } }) in d)
def test_update_dict_with_dict(self): src = { 'name': 'NAME', 'data': { 'attr1': 'VALUE_1', 'attr2': 'VALUE_2' } } comp = { 'name': 'NAME_UPD', 'data': { 'attr1': 'VALUE_1_UPD', 'attr2': 'VALUE_2_UPD' } } d = diff(src, comp) self.assertEqual(3, len(d)) self.assertTrue(_updated('name', 'NAME', 'NAME_UPD') in d) self.assertTrue(_updated('data.attr1', 'VALUE_1', 'VALUE_1_UPD') in d) self.assertTrue(_updated('data.attr2', 'VALUE_2', 'VALUE_2_UPD') in d)
def test_update_dict_with_list_of_dict_extend(self): src = { 'name': 'NAME', 'data': [{ 'name': 'NAME_1', 'data': 'DATA_1' }, { 'name': 'NAME_2', 'data': 'DATA_2' }, { 'name': 'NAME_3', 'data': 'DATA_3' }] } comp = { 'name': 'NAME', 'data': [{ 'name': 'NAME_1', 'data': 'DATA_1' }, { 'name': 'NAME_2', 'data': 'DATA_2' }, { 'name': 'NAME_3', 'data': 'DATA_3' }, { 'name': 'NAME_4', 'data': 'DATA_4' }] } d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue( _added('data[3]', { 'name': 'NAME_4', 'data': 'DATA_4' }) in d)
def test_path_1(self): a = {'a':1} b = {'a':2} diff1 = dictdiff.diff(a, b) dictdiff.patch(a, diff1) self.assertEqual(a['a'], 2) diff2 = dictdiff.diff(a, b) self.assertEqual(len(diff2), 0) a = {'a': {'b': 1}} b = {'a': {'b': 2}} diff1 = dictdiff.diff(a, b) dictdiff.patch(a, diff1) self.assertEqual(a['a']['b'], 2) a = {'a': {'b': 1}} b = {'a': {'c': 1}} diff1 = dictdiff.diff(a, b) dictdiff.patch(a, diff1) # print a # print "0------------------------" self.assertEqual(a['a']['c'], 1) with self.assertRaises(KeyError): a['a']['b'] a = {'a': {'b': 1, 'c': {'e': 1}}} b = {'a': {'b': 1}} diff1 = dictdiff.diff(a, b) print "0------------------------" print diff1 dictdiff.patch(a, diff1) print a print "0------------------------" a = {'a': {'b': 1, 'c': {'e': 1}}} b = {'a': {'b': 1, 'c': {}}} diff1 = dictdiff.diff(a, b) print "0------------------------" print diff1 dictdiff.patch(a, diff1) print a print "0------------------------"
def test_update_dict_with_list_of_dict_reduce(self): src = { 'name': 'NAME', 'data': [{ 'name': 'NAME_1', 'data': 'DATA_1' }, { 'name': 'NAME_2', 'data': 'DATA_2' }, { 'name': 'NAME_3', 'data': 'DATA_3' }] } comp = { 'name': 'NAME_UPD', 'data': [{ 'name': 'NAME_1_UPD', 'data': 'DATA_1_UPD' }, { 'name': 'NAME_2', 'data': 'DATA_2' }] } d = diff(src, comp) self.assertEqual(4, len(d)) self.assertTrue(_updated('name', 'NAME', 'NAME_UPD') in d) self.assertTrue(_updated('data[0].name', 'NAME_1', 'NAME_1_UPD') in d) self.assertTrue(_updated('data[0].data', 'DATA_1', 'DATA_1_UPD') in d) self.assertTrue( _removed('data[2]', { 'name': 'NAME_3', 'data': 'DATA_3' }) in d)
def test_no_difference(self): src = {'name': 'NAME', 'desc': 'DESC'} comp = {'name': 'NAME', 'desc': 'DESC'} d = diff(src, comp) self.assertEqual(0, len(d))
def test_diff_1(self): a = {'a':{'b':[{'c': [{'d':[{'e': 1, 'f': 2}]}]}]}} b = {'a':{'b':[{'c': [{'d':[{'e': 1, 'f': 3}]}]}]}} for x in xrange(self.size): dictdiff.diff(a,b)
def test_removed_property(self): src = {'name': 'NAME', 'desc': 'DESC'} comp = {'name': 'NAME'} d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue(_removed('desc', 'DESC') in d)
def test_delete_value(self): src = 'VALUE' comp = None d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue(_removed('', 'VALUE') in d)
def test_new_value(self): src = None comp = 'VALUE' d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue(_added('', 'VALUE') in d)
def test_update_value(self): src = 'VALUE' comp = 'UPDATED' d = diff(src, comp) self.assertEqual(1, len(d)) self.assertTrue(_updated('', 'VALUE', 'UPDATED') in d)