def diff(self, exclude=[], auto_format=True, query={}): """raise AssertionError if collections don't match by fixtures""" self._exclude_keys = exclude for db in self._db_tree: for collection in db['collections']: db_data_content = self._datastore[db['database']][ collection['name']].find(query) db_file_content = file( os.path.join(self._db_path, db['database'], collection['file']), 'r').readlines() for record, line in itertools.izip_longest(db_data_content, db_file_content, fillvalue={}): remote_db_record = self._stripkey(self._stripoid(record), self._exclude_keys) local_file_record = self._stripkey( self._stripoid(json.loads(line)), self._exclude_keys) if auto_format: remote_db_record = self._formatvalue(remote_db_record) local_file_record = self._formatvalue( local_file_record) if remote_db_record != local_file_record: raise AssertionJsonDiffError(XDiff().json_to_json( remote_db_record, local_file_record).pretty_diff_console())
def xmlcompare(self): file1paths = glob.iglob(os.path.join(self.local_path, '*')) xmlsamed = True for file1path in file1paths: comparedFileNames = file1path.split('/') filelen = len(file1path.split('/')) print file1path file1 = self.xmlfiletodict(file1path) if file1 == None: print "This file in local is empty" continue file2path = os.path.join(self.remote_path, comparedFileNames[filelen - 1]) if os.path.exists(file2path) == False: xmlsamed = False break file2 = self.xmlfiletodict(file2path) if file2 == None: print "The file in remote is empty" continue if XDiff().dict_diff(file1, file2): xmlsamed = False break return xmlsamed
def assertDiff(self, remote): local = [ self._stripkey(record, self._exclude) for record in json.load( open( os.path.join( self._path, '%s_%s.json' % (self._cap, self._timeformat(self._ts_start))), 'r')) ] remote = [self._stripkey(record, self._exclude) for record in remote] diff = XDiff().json_to_json( self._stripkey(local, self._exclude), self._stripkey(remote, self._exclude)).pretty_diff_console() if diff: print diff raise AssertionJsonDiffError()
def assertDiff(self): local_files = glob.iglob(os.path.join(self._local_path, '*')) remote_files = glob.iglob(os.path.join(self._remote_path, '*')) for local, remote in itertools.izip_longest(local_files, remote_files, fillvalue={}): if self._filetype == "json": local_json = [ self._stripkey(record, self._exclude) for record in json.load(open(local, 'r')) ] remote_json = [ self._stripkey(record, self._exclude) for record in json.load(open(remote, 'r')) ] diff = XDiff().json_to_json( self._stripkey(local_json, self._exclude), self._stripkey(remote_json, self._exclude)).pretty_diff_console() elif self._filetype == "text": diff = XDiff().text_to_text( self._stripkey(local, self._exclude), self._stripkey(remote, self._exclude)).pretty_diff_console() else: diff = XDiff().file_to_file( self._stripkey(local, self._exclude), self._stripkey(remote, self._exclude)).pretty_diff_console() if diff: self._fileclean(self._remote_path) print diff raise AssertionJsonDiffError() self._fileclean(self._remote_path)
def assertDiff(self): local_files = glob.iglob(os.path.join(self.local_path, '*')) remote_files = glob.iglob(os.path.join(self.local_remote_path, '*')) for local, remote in itertools.izip_longest(local_files, remote_files, fillvalue={}): print "diff file : %s" % os.path.basename(local) with open(local) as local_csv: local_lines = sorted([line for line in local_csv.readlines()]) with open(remote) as remote_csv: remote_lines = sorted( [line for line in remote_csv.readlines()]) diff = XDiff().text_to_text( ''.join(local_lines), ''.join(remote_lines)).pretty_diff_console() if diff: print diff raise AssertionTextDiffError() self._fileclean()
def assertTextEqual(self, textA, textB): diff = XDiff().text_to_text(textA, textB).pretty_diff_console() if diff: print diff raise AssertionTextDiffError()
def assertJsonFileEqual(self, jsonfileA, jsonfileB): diff = XDiff().jsonfile_to_jsonfile(jsonfileA, jsonfileB).pretty_diff_console() if diff: print diff raise AssertionJsonDiffError()
def assertJsonEqual(self, jsonA, jsonB): diff = XDiff().json_to_json(jsonA, jsonB).pretty_diff_console() if diff: print diff raise AssertionJsonDiffError()
def setUp(self): super(XDiffTestCase, self).setUp() self._xdiff = XDiff()
class XDiffTestCase(TestCase): def setUp(self): super(XDiffTestCase, self).setUp() self._xdiff = XDiff() def test_diff_text_to_text(self): expect_result = "@@ -1,4 +1,4 @@\n"\ "-abba\n"\ "+acba\n" self.assertEqual('', self._xdiff.text_to_text('aaaa', 'aaaa').pretty_diff_console()) self.assertEqual(expect_result, self._xdiff.text_to_text('abba', 'acba').pretty_diff_console()) def test_diff_utf8_text_to_text(self): expect_result = "-中国人民\n"\ "+中华人民" diff_result = self._xdiff.text_to_text('中国人民', '中华人民').pretty_diff_console() self.assertTrue(expect_result, diff_result) def test_diff_multiline_text_to_text(self): expect_results = "@@ -2,7 +2,8 @@\n"\ " aaa\n"\ "\n"\ "-bbb\n"\ "+cbba\n" diff_result = self._xdiff.text_to_text('aaaa\nbbb', 'aaaa\ncbba').pretty_diff_console() self.assertTrue(expect_results, diff_result) def test_diff_json_to_json(self): expect_result = """@@ -1,27 +1,27 @@ { - "a": 1, "b": 2 + "a": 2, "b": 3 } """ diff_results = self._xdiff.json_to_json({"a": 1, "b": 2}, {"a":2, "b": 3}).pretty_diff_console() diff_results = diff_results.replace(' ', '') expect_result = expect_result.replace(' ', '') self.assertEqual(expect_result, diff_results) def test_diff_jsonfile_to_jsonfile(self): expect_results = """@@ -11052,45 +11052,46 @@ ", - "disp_name": "捕获点", + "disp_name": "捕获点1", @@ -11247,42 +11247,42 @@ e, - "name": "intf3", + "name": "intf2", @@ -11379,58 +11379,57 @@ ], - "name": "app4", "state": "apply" + "name": "app3", "state": "edit" """ diff_results = self._xdiff.jsonfile_to_jsonfile( os.path.join(FIXTURE_ROOT, 'app_datapath.json'), os.path.join(FIXTURE_ROOT, 'app_datapath.json.new') ).pretty_diff_console() diff_results = diff_results.replace(' ', '') expect_results = expect_results.replace(' ', '') self.assertEqual(expect_results, diff_results) expect_results = """@@-5,23+5,23@@ { -"a":1 +"a":2 """ diff_results = self._xdiff.jsonfile_to_jsonfile( os.path.join(FIXTURE_ROOT, 'app_datapath_multi.json'), os.path.join(FIXTURE_ROOT, 'app_datapath_multi.json.new') ).pretty_diff_console() diff_results = diff_results.replace(' ', '') expect_results = expect_results.replace(' ', '') self.assertEqual(expect_results, diff_results)