Debian Codegraph @

python/tests/compressor_test.py

raw symbols SCIP
1 # Copyright 2016 The Brotli Authors. All rights reserved.
2 #
3 # Distributed under MIT license.
4 # See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
6 import functools
7 import unittest
8
9 import brotli
10
11 from . import _test_utils
12
13
14 # Do not inherit from TestCase here to ensure that test methods
15 # are not run automatically and instead are run as part of a specific
16 # configuration below.
17 class _TestCompressor(object):
18
19 CHUNK_SIZE = 2048
20
21 def tearDown(self):
22 self.compressor = None
23 # super().tearDown() # Requires Py3+
24
25 def _check_decompression(self, test_data):
26 # Write decompression to temp file and verify it matches the original.
27 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
28 temp_compressed = _test_utils.get_temp_compressed_name(test_data)
29 original = test_data
30 with open(temp_uncompressed, 'wb') as out_file:
31 with open(temp_compressed, 'rb') as in_file:
32 out_file.write(brotli.decompress(in_file.read()))
33 self.assert_files_match(temp_uncompressed, original)
34
35 def _test_single_process(self, test_data):
36 # Write single-shot compression to temp file.
37 temp_compressed = _test_utils.get_temp_compressed_name(test_data)
38 with open(temp_compressed, 'wb') as out_file:
39 with open(test_data, 'rb') as in_file:
40 out_file.write(self.compressor.process(in_file.read()))
41 out_file.write(self.compressor.finish())
42 self._check_decompression(test_data)
43
44 def _test_multiple_process(self, test_data):
45 # Write chunked compression to temp file.
46 temp_compressed = _test_utils.get_temp_compressed_name(test_data)
47 with open(temp_compressed, 'wb') as out_file:
48 with open(test_data, 'rb') as in_file:
49 read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE)
50 for data in iter(read_chunk, b''):
51 out_file.write(self.compressor.process(data))
52 out_file.write(self.compressor.finish())
53 self._check_decompression(test_data)
54
55 def _test_multiple_process_and_flush(self, test_data):
56 # Write chunked and flushed compression to temp file.
57 temp_compressed = _test_utils.get_temp_compressed_name(test_data)
58 with open(temp_compressed, 'wb') as out_file:
59 with open(test_data, 'rb') as in_file:
60 read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE)
61 for data in iter(read_chunk, b''):
62 out_file.write(self.compressor.process(data))
63 out_file.write(self.compressor.flush())
64 out_file.write(self.compressor.finish())
65 self._check_decompression(test_data)
66
67
68 _test_utils.generate_test_methods(_TestCompressor)
69
70
71 class TestCompressorQuality1(_TestCompressor, _test_utils.TestCase):
72
73 def setUp(self):
74 # super().setUp() # Requires Py3+
75 self.compressor = brotli.Compressor(quality=1)
76
77
78 class TestCompressorQuality6(_TestCompressor, _test_utils.TestCase):
79
80 def setUp(self):
81 # super().setUp() # Requires Py3+
82 self.compressor = brotli.Compressor(quality=6)
83
84
85 class TestCompressorQuality9(_TestCompressor, _test_utils.TestCase):
86
87 def setUp(self):
88 # super().setUp() # Requires Py3+
89 self.compressor = brotli.Compressor(quality=9)
90
91
92 class TestCompressorQuality11(_TestCompressor, _test_utils.TestCase):
93
94 def setUp(self):
95 # super().setUp() # Requires Py3+
96 self.compressor = brotli.Compressor(quality=11)
97
98
99 if __name__ == '__main__':
100 unittest.main()