]> Frank Brehm's Git Trees - pixelpark/trace-maillog.git/commitdiff
Adding test methods for reading compressed files. master
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 May 2017 16:48:58 +0000 (18:48 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 May 2017 16:48:58 +0000 (18:48 +0200)
test/test_any_uncompress.py

index 0d97b349d2cf0c1bf6e104e3d2fc042bafedbed3..f67cf1a54dddefc4a975523b57f46263b617b971 100755 (executable)
@@ -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)