def test_explicit_syntax(self, scenario): a, b = scenario differ = JsonDiffer(syntax='explicit') d = differ.diff(a, b) # self.assertEqual(b, differ.patch(a, d)) dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm))
def test_marshal(self): differ = JsonDiffer() d = {delete: 3, '$delete': 4, insert: 4, '$$something': 1} dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm))
def test_marshal(self): differ = JsonDiffer() d = { delete: 3, '$delete': 4, insert: 4, '$$something': 1 } dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm))
def __print_preview(old_json, new_json): logger.warning( '\n---------------- Key Values Preview (Beta) ----------------') if not new_json: logger.warning( '\nSource configuration is empty. No changes will be made.') return False # perform diff operation # to simplify output, add one shared key in src and dest configuration new_json['@base'] = '' old_json['@base'] = '' differ = JsonDiffer(syntax='explicit') res = differ.diff(old_json, new_json) keys = str(res.keys()) if res == {} or (('update' not in keys) and ('insert' not in keys)): logger.warning( '\nTarget configuration already contains all key-values in source. No changes will be made.' ) return False # format result printing for action, changes in res.items(): if action.label == 'delete': continue # we do not delete KVs while importing/exporting if action.label == 'insert': logger.warning('\nAdding:') for key, adding in changes.items(): record = {'key': key} for attribute, value in adding.items(): record[str(attribute)] = str(value) logger.warning(json.dumps(record, ensure_ascii=False)) elif action.label == 'update': logger.warning('\nUpdating:') for key, updates in changes.items(): updates = list(updates.values())[0] attributes = list(updates.keys()) old_record = {'key': key} new_record = {'key': key} for attribute in attributes: old_record[attribute] = old_json[key][attribute] new_record[attribute] = new_json[key][attribute] logger.warning('- %s', json.dumps(old_record, ensure_ascii=False)) logger.warning('+ %s', json.dumps(new_record, ensure_ascii=False)) logger.warning("") # printing an empty line for formatting purpose return True
def test_symmetric_syntax(self, scenario): a, b = scenario differ = JsonDiffer(syntax='symmetric') d = differ.diff(a, b) self.assertEqual(b, differ.patch(a, d)) self.assertEqual(a, differ.unpatch(b, d)) dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm))
def test_explicit_syntax(self, scenario): a, b = scenario differ = JsonDiffer(syntax='explicit') d = differ.diff(a, b)
def test_compact_syntax(self, scenario): a, b = scenario differ = JsonDiffer(syntax='compact') d = differ.diff(a, b) self.assertEqual(b, differ.patch(a, d))