Coverage for tests/test_compression.py: 100%
113 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-02-28 11:40 +0100
« prev ^ index » next coverage.py v7.6.10, created at 2025-02-28 11:40 +0100
1import tempfile
2import typing as t
3from contextlib import chdir
4from pathlib import Path
6from edwh_files_plugin.compression import Compression, Gzip, Nocompression, Pigz, Zip
8DATA = "x" * int(1e9)
11def run_test_with_file(compressor: Compression, extension: str, decompressor: t.Optional[Compression] = None):
12 decompressor = decompressor or compressor
13 assert compressor.is_available()
14 assert decompressor.is_available()
16 with tempfile.TemporaryDirectory(prefix="pytest_file") as d:
17 dir_path = Path(d)
18 bigfile = dir_path / "myfile.txt"
19 bigfile.write_text(DATA)
21 lilfile = dir_path / f"myfile.{extension}"
23 assert compressor.compress(bigfile, lilfile)
25 assert lilfile.exists()
26 assert lilfile.stat().st_size < bigfile.stat().st_size
28 unzipped = dir_path / "unzipped.txt"
29 assert decompressor.decompress(lilfile, unzipped)
31 assert unzipped.read_text() == DATA
34def run_test_with_folder(compressor: Compression, extension: str, decompressor: t.Optional[Compression] = None):
35 decompressor = decompressor or compressor
36 assert compressor.is_available()
37 assert decompressor.is_available()
39 with tempfile.TemporaryDirectory(prefix="pytest_folder") as d:
40 parent_d = Path(d)
41 child_d = parent_d / "somefolder"
42 child_d.mkdir()
44 bigfile = child_d / "raw.txt"
45 bigfile.write_text(DATA)
47 file2 = child_d / "small.txt"
48 file2.write_text("-")
50 lilfile = parent_d / f"compressed.{extension}"
52 assert compressor.compress(child_d, lilfile)
54 assert lilfile.exists()
55 assert lilfile.stat().st_size < bigfile.stat().st_size
57 unzipped_d = parent_d / "somefolder2"
58 unzipped_d.mkdir()
59 assert decompressor.decompress(lilfile, unzipped_d)
61 unzipped = unzipped_d / "raw.txt"
62 assert unzipped.read_text() == DATA
65def test_zip():
66 zip_compression = Compression.for_extension("zip")
67 assert isinstance(zip_compression, Zip)
68 run_test_with_file(zip_compression, "zip")
69 run_test_with_folder(zip_compression, "zip")
72def test_gzip():
73 gzip_compression = Gzip()
74 assert isinstance(gzip_compression, Gzip)
75 run_test_with_file(gzip_compression, "gz")
76 run_test_with_folder(gzip_compression, "tgz")
77 run_test_with_folder(gzip_compression, "tar.gz")
80def test_pigz():
81 pigz_compression = Compression.for_extension("gz")
82 assert isinstance(pigz_compression, Pigz) # pigz > gz
83 run_test_with_file(pigz_compression, "gz")
84 run_test_with_folder(pigz_compression, "tgz")
85 run_test_with_folder(pigz_compression, "tar.gz")
88def test_gzip_pigz_cross():
89 gz_compression = Gzip()
90 pigz_compression = Pigz()
91 run_test_with_file(pigz_compression, "gz", decompressor=gz_compression)
92 run_test_with_folder(pigz_compression, "tgz", decompressor=gz_compression)
94 run_test_with_file(gz_compression, "gz", decompressor=pigz_compression)
95 run_test_with_folder(gz_compression, "tgz", decompressor=pigz_compression)
98def test_noop():
99 class Noop(Compression, extension="noop"): ...
101 # false because it is not available
102 assert not Compression.for_extension("noop")
103 assert not Noop.is_available()
105 assert not Compression.for_extension("fake")
108def test_nocompression():
109 compressor = Nocompression()
111 # test file:
112 assert compressor.is_available()
114 with tempfile.TemporaryDirectory(prefix="pytest_file") as d:
115 dir_path = Path(d)
116 bigfile = dir_path / "myfile.txt"
117 bigfile.write_text(DATA)
119 lilfile = dir_path / f"myfile.txt"
121 assert compressor.compress(bigfile)
123 assert lilfile.exists()
124 assert lilfile.stat().st_size == bigfile.stat().st_size
126 unzipped = dir_path / "myfile.txt"
127 assert compressor.decompress(lilfile)
129 assert unzipped.read_text() == DATA
131 # test folder:
132 with tempfile.TemporaryDirectory(prefix="pytest_folder") as d:
133 parent_d = Path(d)
134 child_d = parent_d / "somefolder"
135 child_d.mkdir()
137 bigfile = child_d / "raw.txt"
138 bigfile.write_text(DATA)
140 file2 = child_d / "small.txt"
141 file2.write_text("-")
143 lilfile = parent_d / f"somefolder.tar"
145 assert compressor.compress(child_d)
147 assert lilfile.exists()
149 unzipped_d = parent_d / "somefolder"
150 assert compressor.decompress(lilfile)
152 unzipped = unzipped_d / "raw.txt"
153 assert unzipped.read_text() == DATA
156def test_best():
157 compressor = Compression.best()
158 assert isinstance(compressor, Pigz)
161def test_compress_decompress_without_filename():
162 c = Compression.best()
164 with tempfile.TemporaryDirectory() as d, chdir(d):
165 p = Path(d)
166 t = p / "file.txt"
167 t.write_text("--------------------")
169 assert c.compress(".")
170 assert c.compress(t)
172 assert c.decompress(p.with_suffix(".tgz"))
173 assert c.decompress(t.with_suffix(".txt.gz"))