| 1 |
import os |
| 2 |
import pathlib |
| 3 |
import zipfile |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import inflate64 |
| 8 |
|
| 9 |
testdata_path = pathlib.Path(os.path.dirname(__file__)).joinpath("data") |
| 10 |
srcdata = testdata_path.joinpath("src.zip") |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize("fname", |
| 14 |
["test-file.1", |
| 15 |
"test-file.2", |
| 16 |
"test-file.3", |
| 17 |
"test-file.4", |
| 18 |
"test-file.5", |
| 19 |
"test-file.6", |
| 20 |
"test-file.7", |
| 21 |
"test-file.8", |
| 22 |
"test-file.9", |
| 23 |
"test-file.10", |
| 24 |
"test-file.11", |
| 25 |
"test-file.12", |
| 26 |
"test-file.13", |
| 27 |
"test-file.14", |
| 28 |
"test-file.15", |
| 29 |
"test-file.16", |
| 30 |
"test-file.17", |
| 31 |
"test-file.18", |
| 32 |
"test-file.19", |
| 33 |
"test-file.20", |
| 34 |
]) |
| 35 |
def test_compress_n(tmp_path, fname): |
| 36 |
""" |
| 37 |
Compress test with various data. |
| 38 |
:param tmp_path: fixture. |
| 39 |
:param fname: target file name. |
| 40 |
""" |
| 41 |
with zipfile.ZipFile(srcdata) as f: |
| 42 |
data = f.read(fname) |
| 43 |
expected_len = len(data) |
| 44 |
compressor = inflate64.Deflater() |
| 45 |
assert not compressor.eof |
| 46 |
compressed = compressor.deflate(data) |
| 47 |
compressed += compressor.flush() |
| 48 |
assert compressor.eof |
| 49 |
with tmp_path.joinpath(fname).open("wb") as f: |
| 50 |
f.write(compressed) |
| 51 |
decompressor = inflate64.Inflater() |
| 52 |
extracted = decompressor.inflate(compressed) |
| 53 |
result_len = len(extracted) |
| 54 |
assert result_len == expected_len |
| 55 |
assert extracted == data |
| 56 |
|
| 57 |
|
| 58 |
def test_compress_larger(tmp_path): |
| 59 |
""" |
| 60 |
Compression test with larger size of data. |
| 61 |
:param tmp_path: fixture. |
| 62 |
""" |
| 63 |
fname = "10000SalesRecords.csv" |
| 64 |
BS = 8192 |
| 65 |
|
| 66 |
compressor = inflate64.Deflater() |
| 67 |
with tmp_path.joinpath(fname).open(mode="wb") as target: |
| 68 |
with zipfile.ZipFile(srcdata) as myzip: |
| 69 |
with myzip.open(fname) as myfile: |
| 70 |
data = myfile.read(BS) |
| 71 |
while len(data) > 0: |
| 72 |
target.write(compressor.deflate(data)) |
| 73 |
data = myfile.read(BS) |
| 74 |
target.write(compressor.flush()) |
| 75 |
|