|
a |
|
b/tests/test_cli.py |
|
|
1 |
import sys |
|
|
2 |
import warnings |
|
|
3 |
from pathlib import Path |
|
|
4 |
|
|
|
5 |
import pytest |
|
|
6 |
from click.testing import CliRunner |
|
|
7 |
|
|
|
8 |
from wsic import cli |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
def test_convert_timeout(samples_path, tmp_path): |
|
|
12 |
"""Check that CLI convert raises IOError when reading times out.""" |
|
|
13 |
runner = CliRunner() |
|
|
14 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
15 |
in_path = str(samples_path / "XYC.jp2") |
|
|
16 |
out_path = str(Path(td) / "XYC.tiff") |
|
|
17 |
warnings.simplefilter("ignore") |
|
|
18 |
with pytest.raises(IOError, match="timed out"): |
|
|
19 |
runner.invoke( |
|
|
20 |
cli.convert, |
|
|
21 |
["-i", in_path, "-o", out_path, "--timeout", "0"], |
|
|
22 |
catch_exceptions=False, |
|
|
23 |
) |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
def test_thumbnail(samples_path, tmp_path): |
|
|
27 |
"""Check that CLI thumbnail works.""" |
|
|
28 |
runner = CliRunner() |
|
|
29 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
30 |
in_path = samples_path / "XYC.jp2" |
|
|
31 |
out_path = Path(td) / "XYC.jpeg" |
|
|
32 |
runner.invoke( |
|
|
33 |
cli.thumbnail, |
|
|
34 |
["-i", str(in_path), "-o", str(out_path), "-s", "512", "512"], |
|
|
35 |
catch_exceptions=False, |
|
|
36 |
) |
|
|
37 |
assert out_path.exists() |
|
|
38 |
assert out_path.is_file() |
|
|
39 |
assert out_path.stat().st_size > 0 |
|
|
40 |
|
|
|
41 |
|
|
|
42 |
def test_thumbnail_downsample(samples_path, tmp_path): |
|
|
43 |
"""Check that CLI thumbnail works with downsample option.""" |
|
|
44 |
runner = CliRunner() |
|
|
45 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
46 |
in_path = samples_path / "XYC.jp2" |
|
|
47 |
out_path = Path(td) / "XYC.jpeg" |
|
|
48 |
result = runner.invoke( |
|
|
49 |
cli.thumbnail, |
|
|
50 |
["-i", str(in_path), "-o", str(out_path), "-d", "16"], |
|
|
51 |
catch_exceptions=False, |
|
|
52 |
) |
|
|
53 |
assert result.exit_code == 0 |
|
|
54 |
assert out_path.exists() |
|
|
55 |
assert out_path.is_file() |
|
|
56 |
assert out_path.stat().st_size > 0 |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
def test_thumbnail_no_cv2(samples_path, tmp_path, monkeypatch): |
|
|
60 |
"""Check that CLI thumbnail works without OpenCV (cv2).""" |
|
|
61 |
monkeypatch.setitem(sys.modules, "cv2", None) |
|
|
62 |
with pytest.raises(ImportError): |
|
|
63 |
import cv2 # noqa # skipcq |
|
|
64 |
runner = CliRunner() |
|
|
65 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
66 |
in_path = samples_path / "XYC.jp2" |
|
|
67 |
out_path = Path(td) / "XYC.jpeg" |
|
|
68 |
runner.invoke( |
|
|
69 |
cli.thumbnail, |
|
|
70 |
["-i", str(in_path), "-o", str(out_path), "-s", "512", "512"], |
|
|
71 |
catch_exceptions=False, |
|
|
72 |
) |
|
|
73 |
assert out_path.exists() |
|
|
74 |
assert out_path.is_file() |
|
|
75 |
assert out_path.stat().st_size > 0 |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
def test_help(): |
|
|
79 |
"""Test the help output.""" |
|
|
80 |
runner = CliRunner() |
|
|
81 |
help_result = runner.invoke(cli.main, ["--help"]) |
|
|
82 |
assert help_result.exit_code == 0 |
|
|
83 |
assert "Console script for wsic." in help_result.output |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
def test_transcode_svs_to_zarr(samples_path, tmp_path): |
|
|
87 |
"""Test the CLI for transcoding SVS to zarr.""" |
|
|
88 |
runner = CliRunner() |
|
|
89 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
90 |
in_path = str(samples_path / "CMU-1-Small-Region.svs") |
|
|
91 |
out_path = str(Path(td) / "CMU-1-Small-Region.zarr") |
|
|
92 |
result = runner.invoke( |
|
|
93 |
cli.transcode, |
|
|
94 |
["-i", in_path, "-o", out_path], |
|
|
95 |
catch_exceptions=False, |
|
|
96 |
) |
|
|
97 |
assert result.exit_code == 0 |
|
|
98 |
|
|
|
99 |
|
|
|
100 |
def test_transcode_svs_to_tiff(samples_path, tmp_path): |
|
|
101 |
"""Test the CLI for transcoding SVS to (tiled) TIFF.""" |
|
|
102 |
runner = CliRunner() |
|
|
103 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
104 |
in_path = str(samples_path / "CMU-1-Small-Region.svs") |
|
|
105 |
out_path = str(Path(td) / "CMU-1-Small-Region.tiff") |
|
|
106 |
result = runner.invoke( |
|
|
107 |
cli.transcode, |
|
|
108 |
["-i", in_path, "-o", out_path], |
|
|
109 |
catch_exceptions=False, |
|
|
110 |
) |
|
|
111 |
assert result.exit_code == 0 |
|
|
112 |
|
|
|
113 |
|
|
|
114 |
def test_transcode_dicom_to_tiff(samples_path, tmp_path): |
|
|
115 |
"""Test the CLI for transcoding DICOM to (tiled) TIFF.""" |
|
|
116 |
runner = CliRunner() |
|
|
117 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
118 |
in_path = str(samples_path / "CMU-1-Small-Region") |
|
|
119 |
out_path = str(Path(td) / "CMU-1-Small-Region.tiff") |
|
|
120 |
result = runner.invoke( |
|
|
121 |
cli.transcode, |
|
|
122 |
["-i", in_path, "-o", out_path], |
|
|
123 |
catch_exceptions=False, |
|
|
124 |
) |
|
|
125 |
assert result.exit_code == 0 |
|
|
126 |
|
|
|
127 |
|
|
|
128 |
def test_convert_jp2_to_tiff(samples_path, tmp_path): |
|
|
129 |
"""Test the CLI for converting JP2 to tiled TIFF.""" |
|
|
130 |
runner = CliRunner() |
|
|
131 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
132 |
in_path = str(samples_path / "XYC.jp2") |
|
|
133 |
out_path = str(Path(td) / "XYC.tiff") |
|
|
134 |
result = runner.invoke( |
|
|
135 |
cli.convert, |
|
|
136 |
["-i", in_path, "-o", out_path], |
|
|
137 |
catch_exceptions=False, |
|
|
138 |
) |
|
|
139 |
assert result.exit_code == 0 |
|
|
140 |
|
|
|
141 |
|
|
|
142 |
def test_convert_jp2_to_zarr(samples_path, tmp_path): |
|
|
143 |
"""Test the CLI for converting JP2 to zarr.""" |
|
|
144 |
runner = CliRunner() |
|
|
145 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
146 |
in_path = str(samples_path / "XYC.jp2") |
|
|
147 |
out_path = str(Path(td) / "XYC.zarr") |
|
|
148 |
result = runner.invoke( |
|
|
149 |
cli.convert, |
|
|
150 |
["-i", in_path, "-o", out_path], |
|
|
151 |
catch_exceptions=False, |
|
|
152 |
) |
|
|
153 |
assert result.exit_code == 0 |
|
|
154 |
|
|
|
155 |
|
|
|
156 |
def test_transcode_bad_input_file_ext(samples_path, tmp_path): |
|
|
157 |
"""Test the CLI for transcoding with a bad input file extension.""" |
|
|
158 |
runner = CliRunner() |
|
|
159 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
160 |
# in_path must exists but not be a valid input |
|
|
161 |
in_path = str(samples_path / "XYC.jp2") |
|
|
162 |
out_path = str(Path(td) / "XYC.tiff") |
|
|
163 |
result = runner.invoke( |
|
|
164 |
cli.transcode, |
|
|
165 |
["-i", in_path, "-o", out_path], |
|
|
166 |
catch_exceptions=False, |
|
|
167 |
) |
|
|
168 |
assert result.exit_code == 2 |
|
|
169 |
|
|
|
170 |
|
|
|
171 |
def test_transcode_bad_output_file_ext(samples_path, tmp_path): |
|
|
172 |
"""Check that CLI raises click.BadParameter when output file extension is bad.""" |
|
|
173 |
runner = CliRunner() |
|
|
174 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
175 |
in_path = samples_path / "XYC-half-mpp.tiff" |
|
|
176 |
out_path = Path(td) / "XYC.foo" |
|
|
177 |
result = runner.invoke( |
|
|
178 |
cli.transcode, |
|
|
179 |
["-i", str(in_path), "-o", str(out_path)], |
|
|
180 |
catch_exceptions=False, |
|
|
181 |
) |
|
|
182 |
assert result.exit_code == 2 |
|
|
183 |
|
|
|
184 |
|
|
|
185 |
def test_convert_svs_to_zarr_zip(samples_path, tmp_path): |
|
|
186 |
"""Test the CLI for converting SVS to zipped zarr.""" |
|
|
187 |
runner = CliRunner() |
|
|
188 |
with runner.isolated_filesystem(temp_dir=tmp_path) as td: |
|
|
189 |
in_path = str(samples_path / "CMU-1-Small-Region.svs") |
|
|
190 |
out_path = str(Path(td) / "CMU-1-Small-Region.zarr.zip") |
|
|
191 |
result = runner.invoke( |
|
|
192 |
cli.convert, |
|
|
193 |
["-i", in_path, "-o", out_path, "-s", "zip"], |
|
|
194 |
catch_exceptions=False, |
|
|
195 |
) |
|
|
196 |
assert result.exit_code == 0 |
|
|
197 |
|
|
|
198 |
import zipfile |
|
|
199 |
|
|
|
200 |
with zipfile.ZipFile(out_path, "r") as zf: |
|
|
201 |
zf.testzip() |