| 1 |
import os |
| 2 |
import pytest |
| 3 |
import threading |
| 4 |
import lz4.frame as lz4frame |
| 5 |
|
| 6 |
test_data = [ |
| 7 |
b'', |
| 8 |
(128 * (32 * os.urandom(32))), |
| 9 |
(5 * 128 * os.urandom(1024)), |
| 10 |
] |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture( |
| 14 |
params=test_data, |
| 15 |
ids=[ |
| 16 |
'data' + str(i) for i in range(len(test_data)) |
| 17 |
] |
| 18 |
) |
| 19 |
def data(request): |
| 20 |
return request.param |
| 21 |
|
| 22 |
|
| 23 |
compression_levels = [ |
| 24 |
(lz4frame.COMPRESSIONLEVEL_MIN), |
| 25 |
|
| 26 |
|
| 27 |
] |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture( |
| 31 |
params=compression_levels |
| 32 |
) |
| 33 |
def compression_level(request): |
| 34 |
return request.param |
| 35 |
|
| 36 |
|
| 37 |
def test_lz4frame_open_write(tmp_path, data): |
| 38 |
thread_id = threading.get_native_id() |
| 39 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wb') as fp: |
| 40 |
fp.write(data) |
| 41 |
|
| 42 |
|
| 43 |
def test_lz4frame_open_write_read_defaults(tmp_path, data): |
| 44 |
thread_id = threading.get_native_id() |
| 45 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wb') as fp: |
| 46 |
fp.write(data) |
| 47 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='r') as fp: |
| 48 |
data_out = fp.read() |
| 49 |
assert data_out == data |
| 50 |
|
| 51 |
|
| 52 |
def test_lz4frame_open_write_read_text(tmp_path): |
| 53 |
data = u'This is a test string' |
| 54 |
thread_id = threading.get_native_id() |
| 55 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wt') as fp: |
| 56 |
fp.write(data) |
| 57 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='rt') as fp: |
| 58 |
data_out = fp.read() |
| 59 |
assert data_out == data |
| 60 |
|
| 61 |
|
| 62 |
def test_lz4frame_open_write_read_text_iter(tmp_path): |
| 63 |
data = u'This is a test string' |
| 64 |
thread_id = threading.get_native_id() |
| 65 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wt') as fp: |
| 66 |
fp.write(data) |
| 67 |
data_out = '' |
| 68 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='rt') as fp: |
| 69 |
for line in fp: |
| 70 |
data_out += line |
| 71 |
assert data_out == data |
| 72 |
|
| 73 |
|
| 74 |
def test_lz4frame_open_write_read( |
| 75 |
tmp_path, |
| 76 |
data, |
| 77 |
compression_level, |
| 78 |
block_linked, |
| 79 |
block_checksum, |
| 80 |
block_size, |
| 81 |
content_checksum, |
| 82 |
auto_flush, |
| 83 |
store_size, |
| 84 |
return_bytearray): |
| 85 |
|
| 86 |
kwargs = {} |
| 87 |
|
| 88 |
if store_size is True: |
| 89 |
kwargs['source_size'] = len(data) |
| 90 |
|
| 91 |
kwargs['compression_level'] = compression_level |
| 92 |
kwargs['block_size'] = block_size |
| 93 |
kwargs['block_linked'] = block_linked |
| 94 |
kwargs['content_checksum'] = content_checksum |
| 95 |
kwargs['block_checksum'] = block_checksum |
| 96 |
kwargs['auto_flush'] = auto_flush |
| 97 |
kwargs['return_bytearray'] = return_bytearray |
| 98 |
kwargs['mode'] = 'wb' |
| 99 |
|
| 100 |
thread_id = threading.get_native_id() |
| 101 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', **kwargs) as fp: |
| 102 |
fp.write(data) |
| 103 |
|
| 104 |
with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='r') as fp: |
| 105 |
data_out = fp.read() |
| 106 |
|
| 107 |
assert data_out == data |
| 108 |
|
| 109 |
|
| 110 |
def test_lz4frame_flush(tmp_path): |
| 111 |
data_1 = b"This is a..." |
| 112 |
data_2 = b" test string!" |
| 113 |
thread_id = threading.get_native_id() |
| 114 |
|
| 115 |
with lz4frame.open(tmp_path / f"testfile_{thread_id}", mode="w") as fp_write: |
| 116 |
fp_write.write(data_1) |
| 117 |
fp_write.flush() |
| 118 |
|
| 119 |
fp_write.write(data_2) |
| 120 |
|
| 121 |
with lz4frame.open(tmp_path / f"testfile_{thread_id}", mode="r") as fp_read: |
| 122 |
assert fp_read.read() == data_1 |
| 123 |
|
| 124 |
fp_write.flush() |
| 125 |
|
| 126 |
with lz4frame.open(tmp_path / f"testfile_{thread_id}", mode="r") as fp_read: |
| 127 |
assert fp_read.read() == data_1 + data_2 |