| 1 |
|
| 2 |
""" |
| 3 |
test_simple_compression |
| 4 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
Tests for compression of single chunks. |
| 7 |
""" |
| 8 |
import brotlicffi |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from hypothesis import given, settings |
| 13 |
from hypothesis.strategies import binary, integers, sampled_from, one_of |
| 14 |
|
| 15 |
|
| 16 |
def test_roundtrip_compression_with_files(simple_compressed_file): |
| 17 |
""" |
| 18 |
Roundtripping data through the compressor works correctly. |
| 19 |
""" |
| 20 |
with open(simple_compressed_file[0], 'rb') as f: |
| 21 |
uncompressed_data = f.read() |
| 22 |
|
| 23 |
assert brotlicffi.decompress( |
| 24 |
brotlicffi.compress(uncompressed_data) |
| 25 |
) == uncompressed_data |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.slow |
| 29 |
@settings(deadline=None) |
| 30 |
@given( |
| 31 |
chunk_size=integers(min_value=1, max_value=2**12), |
| 32 |
mode=sampled_from(list(brotlicffi.BrotliEncoderMode)), |
| 33 |
quality=integers(min_value=0, max_value=11), |
| 34 |
lgwin=integers(min_value=10, max_value=24), |
| 35 |
lgblock=one_of( |
| 36 |
integers(min_value=0, max_value=0), |
| 37 |
integers(min_value=16, max_value=24) |
| 38 |
), |
| 39 |
) |
| 40 |
def test_streaming_compression(one_compressed_file, |
| 41 |
chunk_size, |
| 42 |
mode, |
| 43 |
quality, |
| 44 |
lgwin, |
| 45 |
lgblock): |
| 46 |
""" |
| 47 |
Confirm that the streaming compressor works as expected. |
| 48 |
""" |
| 49 |
compressed_chunks = [] |
| 50 |
c = brotlicffi.Compressor( |
| 51 |
mode=mode, quality=quality, lgwin=lgwin, lgblock=lgblock |
| 52 |
) |
| 53 |
with open(one_compressed_file, 'rb') as f: |
| 54 |
while True: |
| 55 |
next_data = f.read(chunk_size) |
| 56 |
if not next_data: |
| 57 |
break |
| 58 |
|
| 59 |
compressed_chunks.append(c.compress(next_data)) |
| 60 |
|
| 61 |
compressed_chunks.append(c.finish()) |
| 62 |
decompressed = brotlicffi.decompress(b''.join(compressed_chunks)) |
| 63 |
with open(one_compressed_file, 'rb') as f: |
| 64 |
assert decompressed == f.read() |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.slow |
| 68 |
@settings(deadline=None) |
| 69 |
@given( |
| 70 |
chunk_size=integers(min_value=1, max_value=2**12), |
| 71 |
mode=sampled_from(list(brotlicffi.BrotliEncoderMode)), |
| 72 |
quality=integers(min_value=0, max_value=11), |
| 73 |
lgwin=integers(min_value=10, max_value=24), |
| 74 |
lgblock=one_of( |
| 75 |
integers(min_value=0, max_value=0), |
| 76 |
integers(min_value=16, max_value=24) |
| 77 |
), |
| 78 |
) |
| 79 |
def test_streaming_compression_flush(one_compressed_file, |
| 80 |
chunk_size, |
| 81 |
mode, |
| 82 |
quality, |
| 83 |
lgwin, |
| 84 |
lgblock): |
| 85 |
""" |
| 86 |
Confirm that the streaming compressor works as expected, including flushes |
| 87 |
after each chunk. |
| 88 |
""" |
| 89 |
compressed_chunks = [] |
| 90 |
c = brotlicffi.Compressor( |
| 91 |
mode=mode, quality=quality, lgwin=lgwin, lgblock=lgblock |
| 92 |
) |
| 93 |
with open(one_compressed_file, 'rb') as f: |
| 94 |
while True: |
| 95 |
next_data = f.read(chunk_size) |
| 96 |
if not next_data: |
| 97 |
break |
| 98 |
|
| 99 |
compressed_chunks.append(c.compress(next_data)) |
| 100 |
compressed_chunks.append(c.flush()) |
| 101 |
|
| 102 |
compressed_chunks.append(c.finish()) |
| 103 |
decompressed = brotlicffi.decompress(b''.join(compressed_chunks)) |
| 104 |
with open(one_compressed_file, 'rb') as f: |
| 105 |
assert decompressed == f.read() |
| 106 |
|
| 107 |
|
| 108 |
@given(binary()) |
| 109 |
def test_compressed_data_roundtrips(s): |
| 110 |
assert brotlicffi.decompress(brotlicffi.compress(s)) == s |
| 111 |
|
| 112 |
|
| 113 |
@given(binary()) |
| 114 |
def test_process_alias(s): |
| 115 |
c1 = brotlicffi.Compressor() |
| 116 |
c2 = brotlicffi.Compressor() |
| 117 |
d1 = brotlicffi.Decompressor() |
| 118 |
d2 = brotlicffi.Decompressor() |
| 119 |
s1 = c1.compress(s) + c1.finish() |
| 120 |
s2 = c2.process(s) + c2.finish() |
| 121 |
assert ( |
| 122 |
(d1.decompress(s1) + d1.finish()) |
| 123 |
== (d2.process(s2) + d2.finish()) |
| 124 |
== s |
| 125 |
) |
| 126 |
|
| 127 |
|
| 128 |
@pytest.mark.parametrize( |
| 129 |
"params", |
| 130 |
[ |
| 131 |
{"mode": 52}, |
| 132 |
{"quality": 52}, |
| 133 |
{"lgwin": 52}, |
| 134 |
{"lgblock": 52}, |
| 135 |
] |
| 136 |
) |
| 137 |
def test_bad_compressor_parameters(params): |
| 138 |
with pytest.raises(brotlicffi.error): |
| 139 |
brotlicffi.Compressor(**params) |