Commit 1b8a14d3 authored by Nadeem Vawda's avatar Nadeem Vawda

Separate tests for gzip.GzipFile and gzip.open.

parent 7e126205
...@@ -33,7 +33,7 @@ class UnseekableIO(io.BytesIO): ...@@ -33,7 +33,7 @@ class UnseekableIO(io.BytesIO):
raise io.UnsupportedOperation raise io.UnsupportedOperation
class TestGzip(unittest.TestCase): class BaseTest(unittest.TestCase):
filename = support.TESTFN filename = support.TESTFN
def setUp(self): def setUp(self):
...@@ -43,6 +43,7 @@ class TestGzip(unittest.TestCase): ...@@ -43,6 +43,7 @@ class TestGzip(unittest.TestCase):
support.unlink(self.filename) support.unlink(self.filename)
class TestGzip(BaseTest):
def test_write(self): def test_write(self):
with gzip.GzipFile(self.filename, 'wb') as f: with gzip.GzipFile(self.filename, 'wb') as f:
f.write(data1 * 50) f.write(data1 * 50)
...@@ -115,14 +116,14 @@ class TestGzip(unittest.TestCase): ...@@ -115,14 +116,14 @@ class TestGzip(unittest.TestCase):
# Bug #1074261 was triggered when reading a file that contained # Bug #1074261 was triggered when reading a file that contained
# many, many members. Create such a file and verify that reading it # many, many members. Create such a file and verify that reading it
# works. # works.
with gzip.open(self.filename, 'wb', 9) as f: with gzip.GzipFile(self.filename, 'wb', 9) as f:
f.write(b'a') f.write(b'a')
for i in range(0, 200): for i in range(0, 200):
with gzip.open(self.filename, "ab", 9) as f: # append with gzip.GzipFile(self.filename, "ab", 9) as f: # append
f.write(b'a') f.write(b'a')
# Try reading the file # Try reading the file
with gzip.open(self.filename, "rb") as zgfile: with gzip.GzipFile(self.filename, "rb") as zgfile:
contents = b"" contents = b""
while 1: while 1:
ztxt = zgfile.read(8192) ztxt = zgfile.read(8192)
...@@ -374,10 +375,9 @@ class TestGzip(unittest.TestCase): ...@@ -374,10 +375,9 @@ class TestGzip(unittest.TestCase):
datac = gzip.compress(data) datac = gzip.compress(data)
self.assertEqual(gzip.decompress(datac), data) self.assertEqual(gzip.decompress(datac), data)
# Test the 'open' convenience function.
def test_open_binary(self): class TestOpen(BaseTest):
# Test explicit binary modes. def test_binary_modes(self):
uncompressed = data1 * 50 uncompressed = data1 * 50
with gzip.open(self.filename, "wb") as f: with gzip.open(self.filename, "wb") as f:
f.write(uncompressed) f.write(uncompressed)
...@@ -392,7 +392,7 @@ class TestGzip(unittest.TestCase): ...@@ -392,7 +392,7 @@ class TestGzip(unittest.TestCase):
file_data = gzip.decompress(f.read()) file_data = gzip.decompress(f.read())
self.assertEqual(file_data, uncompressed * 2) self.assertEqual(file_data, uncompressed * 2)
def test_open_default_binary(self): def test_implicit_binary_modes(self):
# Test implicit binary modes (no "b" or "t" in mode string). # Test implicit binary modes (no "b" or "t" in mode string).
uncompressed = data1 * 50 uncompressed = data1 * 50
with gzip.open(self.filename, "w") as f: with gzip.open(self.filename, "w") as f:
...@@ -408,8 +408,7 @@ class TestGzip(unittest.TestCase): ...@@ -408,8 +408,7 @@ class TestGzip(unittest.TestCase):
file_data = gzip.decompress(f.read()) file_data = gzip.decompress(f.read())
self.assertEqual(file_data, uncompressed * 2) self.assertEqual(file_data, uncompressed * 2)
def test_open_text(self): def test_text_modes(self):
# Test text modes.
uncompressed = data1.decode("ascii") * 50 uncompressed = data1.decode("ascii") * 50
with gzip.open(self.filename, "wt") as f: with gzip.open(self.filename, "wt") as f:
f.write(uncompressed) f.write(uncompressed)
...@@ -424,7 +423,7 @@ class TestGzip(unittest.TestCase): ...@@ -424,7 +423,7 @@ class TestGzip(unittest.TestCase):
file_data = gzip.decompress(f.read()).decode("ascii") file_data = gzip.decompress(f.read()).decode("ascii")
self.assertEqual(file_data, uncompressed * 2) self.assertEqual(file_data, uncompressed * 2)
def test_open_bad_params(self): def test_bad_params(self):
# Test invalid parameter combinations. # Test invalid parameter combinations.
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
gzip.open(self.filename, "wbt") gzip.open(self.filename, "wbt")
...@@ -435,7 +434,7 @@ class TestGzip(unittest.TestCase): ...@@ -435,7 +434,7 @@ class TestGzip(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
gzip.open(self.filename, "rb", newline="\n") gzip.open(self.filename, "rb", newline="\n")
def test_open_with_encoding(self): def test_encoding(self):
# Test non-default encoding. # Test non-default encoding.
uncompressed = data1.decode("ascii") * 50 uncompressed = data1.decode("ascii") * 50
with gzip.open(self.filename, "wt", encoding="utf-16") as f: with gzip.open(self.filename, "wt", encoding="utf-16") as f:
...@@ -446,7 +445,7 @@ class TestGzip(unittest.TestCase): ...@@ -446,7 +445,7 @@ class TestGzip(unittest.TestCase):
with gzip.open(self.filename, "rt", encoding="utf-16") as f: with gzip.open(self.filename, "rt", encoding="utf-16") as f:
self.assertEqual(f.read(), uncompressed) self.assertEqual(f.read(), uncompressed)
def test_open_with_encoding_error_handler(self): def test_encoding_error_handler(self):
# Test with non-default encoding error handler. # Test with non-default encoding error handler.
with gzip.open(self.filename, "wb") as f: with gzip.open(self.filename, "wb") as f:
f.write(b"foo\xffbar") f.write(b"foo\xffbar")
...@@ -454,7 +453,7 @@ class TestGzip(unittest.TestCase): ...@@ -454,7 +453,7 @@ class TestGzip(unittest.TestCase):
as f: as f:
self.assertEqual(f.read(), "foobar") self.assertEqual(f.read(), "foobar")
def test_open_with_newline(self): def test_newline(self):
# Test with explicit newline (universal newline mode disabled). # Test with explicit newline (universal newline mode disabled).
uncompressed = data1.decode("ascii") * 50 uncompressed = data1.decode("ascii") * 50
with gzip.open(self.filename, "wt") as f: with gzip.open(self.filename, "wt") as f:
...@@ -463,7 +462,7 @@ class TestGzip(unittest.TestCase): ...@@ -463,7 +462,7 @@ class TestGzip(unittest.TestCase):
self.assertEqual(f.readlines(), [uncompressed]) self.assertEqual(f.readlines(), [uncompressed])
def test_main(verbose=None): def test_main(verbose=None):
support.run_unittest(TestGzip) support.run_unittest(TestGzip, TestOpen)
if __name__ == "__main__": if __name__ == "__main__":
test_main(verbose=True) test_main(verbose=True)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment