From: Frank Brehm Date: Thu, 11 May 2017 16:48:58 +0000 (+0200) Subject: Adding test methods for reading compressed files. X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;ds=inline;p=pixelpark%2Ftrace-maillog.git Adding test methods for reading compressed files. --- diff --git a/test/test_any_uncompress.py b/test/test_any_uncompress.py index 0d97b34..f67cf1a 100755 --- a/test/test_any_uncompress.py +++ b/test/test_any_uncompress.py @@ -118,6 +118,65 @@ class TestAnyUncompress(LogtraceTestcase): e = cm.exception LOG.debug("%s raised: %s", e.__class__.__name__, e) + # ------------------------------------------------------------------------- + def test_reading(self): + + from trace_maillog import any_uncompress_file + from trace_maillog.any_uncompress_file import AnyUncompressFile + from trace_maillog.any_uncompress_file import InvalidCompressionError + + LOG.info("Testing reading of usable files...") + + src_file = os.path.join(self.testdata_dir, 'lorem-ipsum.txt') + src_content = '' + open_args = {} + if six.PY3: + open_args = { + 'encoding': 'utf-8', + 'errors': 'surrogateescape', + } + LOG.debug("Reading source file {!r} ...".format(src_file)) + with open(src_file, 'r', **open_args) as fh: + src_content = fh.read() + + for fname in self.usable_files: + LOG.debug("Testing {!r} ...".format(fname)) + afile = None + if fname.endswith('.zip'): + afile = os.path.basename(fname).replace('.zip', '') + tgt_content = '' + with any_uncompress_file.open(fname, afile, text=True) as fh: + tgt_content = fh.read() + if not isinstance(tgt_content, str) and six.PY3: + tgt_content = tgt_content.decode(**open_args) + self.assertEqual(tgt_content, src_content) + + # ------------------------------------------------------------------------- + def test_reading_binary(self): + + from trace_maillog import any_uncompress_file + from trace_maillog.any_uncompress_file import AnyUncompressFile + from trace_maillog.any_uncompress_file import InvalidCompressionError + + LOG.info("Testing reading of binary usable files...") + + src_file = os.path.join(self.testdata_dir, 'lorem-ipsum.txt') + src_content = '' + open_args = {} + LOG.debug("Reading source file {!r} ...".format(src_file)) + with open(src_file, 'rb') as fh: + src_content = fh.read() + + for fname in self.usable_files: + LOG.debug("Testing {!r} ...".format(fname)) + afile = None + if fname.endswith('.zip'): + afile = os.path.basename(fname).replace('.zip', '') + tgt_content = '' + with any_uncompress_file.open(fname, afile, text=False) as fh: + tgt_content = fh.read() + self.assertEqual(tgt_content, src_content) + # ============================================================================= @@ -136,6 +195,8 @@ if __name__ == '__main__': suite.addTest(TestAnyUncompress('test_import', verbose)) suite.addTest(TestAnyUncompress('test_open', verbose)) + suite.addTest(TestAnyUncompress('test_reading', verbose)) + suite.addTest(TestAnyUncompress('test_reading_binary', verbose)) runner = unittest.TextTestRunner(verbosity=verbose)