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 kwargs = {} 18 # Write decompression to temp file and verify it matches the original. 19 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 20 temp_compressed = _test_utils.get_temp_compressed_name(test_data) 21 original = test_data 22 with open(temp_uncompressed, 'wb') as out_file: 23 with open(temp_compressed, 'rb') as in_file: 24 out_file.write(brotli.decompress(in_file.read(), **kwargs)) 25 self.assertFilesMatch(temp_uncompressed, original) 26 27 def _compress(self, test_data, **kwargs): 28 temp_compressed = _test_utils.get_temp_compressed_name(test_data) 29 with open(temp_compressed, 'wb') as out_file: 30 with open(test_data, 'rb') as in_file: 31 out_file.write(brotli.compress(in_file.read(), **kwargs)) 32 33 def _test_compress(self, test_data, **kwargs): 34 self._compress(test_data, **kwargs) 35 self._check_decompression(test_data, **kwargs) 36 37 38_test_utils.generate_test_methods(TestCompress, variants=TestCompress.VARIANTS) 39 40if __name__ == '__main__': 41 unittest.main() 42