• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6import unittest
7
8from . import _test_utils
9import brotli
10
11
12class TestCompress(_test_utils.TestCase):
13
14    VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)}
15
16    def _check_decompression(self, test_data, **kwargs):
17        # Only dictionary is supported as a kwarg to brotli.decompress.
18        if 'dictionary' in kwargs:
19            kwargs = {'dictionary': kwargs['dictionary']}
20        else:
21            kwargs = {}
22        # Write decompression to temp file and verify it matches the original.
23        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
24        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
25        original = test_data
26        with open(temp_uncompressed, 'wb') as out_file:
27            with open(temp_compressed, 'rb') as in_file:
28                out_file.write(brotli.decompress(in_file.read(), **kwargs))
29        self.assertFilesMatch(temp_uncompressed, original)
30
31    def _compress(self, test_data, **kwargs):
32        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
33        with open(temp_compressed, 'wb') as out_file:
34            with open(test_data, 'rb') as in_file:
35                out_file.write(brotli.compress(in_file.read(), **kwargs))
36
37    def _test_compress(self, test_data, **kwargs):
38        self._compress(test_data, **kwargs)
39        self._check_decompression(test_data, **kwargs)
40
41    def _test_compress_custom_dictionary(self, test_data, **kwargs):
42        with open(test_data, 'rb') as in_file:
43            dictionary = in_file.read()
44        kwargs['dictionary'] = dictionary
45        self._compress(test_data, **kwargs)
46        self._check_decompression(test_data, **kwargs)
47
48
49_test_utils.generate_test_methods(TestCompress, variants=TestCompress.VARIANTS)
50
51if __name__ == '__main__':
52    unittest.main()
53