def test_compressor_init(self): with self.assertRaisesRegex(AttributeError, 'has no attribute \'write\''): Compressor('1') # non-callable write attribute class Empty(object): write = 1 with self.assertRaises(TypeError): Compressor(Empty()) # cannot use context without fp with self.assertRaises(ValueError): with Compressor() as _: # noqa (unused variable) pass
def __fp_test(self, in_raw=LONG_INPUT, **kwargs): in_bytes = BytesIO(in_raw) out_bytes = BytesIO() with Compressor(out_bytes, **kwargs) as compressor: try: while True: compressor.update(in_bytes.read(1024)) # raised by compressor.update() on empty data argument except Lz4FramedNoDataError: pass self.assertEqual(decompress(out_bytes.getvalue()), in_raw)
def test_decompress_invalid_input(self): with self.assertRaisesLz4FramedError(LZ4F_ERROR_frameType_unknown): decompress(b'invalidheader') with self.assertRaisesRegex(ValueError, 'frame incomplete'): decompress(compress(SHORT_INPUT)[:-5]) # incomplete data (length not specified in header) with BytesIO() as out: with Compressor(out) as compressor: compressor.update(SHORT_INPUT) output = out.getvalue() with self.assertRaisesRegex(ValueError, 'frame incomplete'): decompress(output[:-20])
def test_compressor__no_fp(self): in_bytes = BytesIO(LONG_INPUT) out_bytes = BytesIO() compressor = Compressor() try: while True: out_bytes.write(compressor.update(in_bytes.read(1024))) # raised by compressor.update() on empty data argument except Lz4FramedNoDataError: pass out_bytes.write(compressor.end()) self.assertEqual(decompress(out_bytes.getvalue()), LONG_INPUT)
def do_compress(in_stream, out_stream): read = in_stream.read read_size = get_block_size() try: with Compressor(out_stream) as compressor: try: while True: compressor.update(read(read_size)) # empty read result supplied to update() except Lz4FramedNoDataError: pass # input stream exception except EOFError: pass except Lz4FramedError as ex: __error('Compression error: %s' % ex) return 8 return 0