|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | """Tests for `tiatoolbox` package.""" |
4 | | - |
5 | 4 | import pytest |
6 | 5 |
|
7 | | -from click.testing import CliRunner |
8 | | - |
9 | | -from tiatoolbox import tiatoolbox |
| 6 | +from tiatoolbox.dataloader.slide_info import slide_info |
| 7 | +from tiatoolbox import utils |
10 | 8 | from tiatoolbox import cli |
| 9 | +from tiatoolbox import __version__ |
| 10 | + |
| 11 | +from click.testing import CliRunner |
| 12 | +import requests |
| 13 | +import os |
| 14 | +import pathlib |
11 | 15 |
|
12 | 16 |
|
13 | 17 | @pytest.fixture |
14 | | -def response(): |
15 | | - """Sample pytest fixture. |
| 18 | +def _response_ndpi(request): |
| 19 | + """ |
| 20 | + Sample pytest fixture for ndpi images |
| 21 | + Download ndpi image for pytest |
| 22 | + """ |
| 23 | + ndpi_file_path = pathlib.Path(__file__).parent.joinpath("CMU-1.ndpi") |
| 24 | + if not pathlib.Path.is_file(ndpi_file_path): |
| 25 | + r = requests.get( |
| 26 | + "http://openslide.cs.cmu.edu/download/openslide-testdata" |
| 27 | + "/Hamamatsu/CMU-1.ndpi" |
| 28 | + ) |
| 29 | + with open(ndpi_file_path, "wb") as f: |
| 30 | + f.write(r.content) |
| 31 | + |
| 32 | + def close_ndpi(): |
| 33 | + if pathlib.Path.is_file(ndpi_file_path): |
| 34 | + os.remove(str(ndpi_file_path)) |
| 35 | + |
| 36 | + request.addfinalizer(close_ndpi) |
| 37 | + return _response_ndpi |
16 | 38 |
|
17 | | - See more at: http://doc.pytest.org/en/latest/fixture.html |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def _response_svs(request): |
18 | 42 | """ |
19 | | - # import requests |
20 | | - # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') |
| 43 | + Sample pytest fixture for svs images |
| 44 | + Download ndpi image for pytest |
| 45 | + """ |
| 46 | + svs_file_path = pathlib.Path(__file__).parent.joinpath("CMU-1.svs") |
| 47 | + if not pathlib.Path.is_file(svs_file_path): |
| 48 | + r = requests.get( |
| 49 | + "http://openslide.cs.cmu.edu/download/openslide-testdata" |
| 50 | + "/Hamamatsu/CMU-1.ndpi" |
| 51 | + ) |
| 52 | + with open(svs_file_path, "wb") as f: |
| 53 | + f.write(r.content) |
| 54 | + |
| 55 | + def close_ndpi(): |
| 56 | + if pathlib.Path.is_file(svs_file_path): |
| 57 | + os.remove(str(svs_file_path)) |
| 58 | + |
| 59 | + request.addfinalizer(close_ndpi) |
| 60 | + return _response_svs |
21 | 61 |
|
22 | 62 |
|
23 | | -def test_content(response): |
24 | | - """Sample pytest test function with the pytest fixture as an argument.""" |
25 | | - # from bs4 import BeautifulSoup |
26 | | - # assert 'GitHub' in BeautifulSoup(response.content).title.string |
| 63 | +def test_slide_info(_response_ndpi, _response_svs): |
| 64 | + """pytest for slide_info as a python function""" |
| 65 | + file_types = ("*.ndpi", "*.svs", "*.mrxs") |
| 66 | + files_all = utils.misc.grab_files_from_dir( |
| 67 | + input_path=str(pathlib.Path(r".")), file_types=file_types, |
| 68 | + ) |
| 69 | + slide_params = slide_info(input_path=files_all, workers=2) |
27 | 70 |
|
| 71 | + for slide_param in slide_params: |
| 72 | + utils.misc.save_yaml(slide_param, slide_param["file_name"] + ".yaml") |
28 | 73 |
|
29 | | -def test_command_line_interface(): |
30 | | - """Test the CLI.""" |
| 74 | + |
| 75 | +def test_command_line_help_interface(): |
| 76 | + """Test the CLI help""" |
31 | 77 | runner = CliRunner() |
32 | 78 | result = runner.invoke(cli.main) |
33 | 79 | assert result.exit_code == 0 |
34 | | - assert "tiatoolbox.cli.main" in result.output |
35 | 80 | help_result = runner.invoke(cli.main, ["--help"]) |
36 | 81 | assert help_result.exit_code == 0 |
37 | | - assert "--help Show this message and exit." in help_result.output |
| 82 | + assert help_result.output == result.output |
| 83 | + |
| 84 | + |
| 85 | +def test_command_line_version(): |
| 86 | + """pytest for version check""" |
| 87 | + runner = CliRunner() |
| 88 | + version_result = runner.invoke(cli.main, ["-V"]) |
| 89 | + assert __version__ in version_result.output |
| 90 | + |
| 91 | + |
| 92 | +def test_command_line_slide_info(_response_ndpi, _response_svs): |
| 93 | + """Test the Slide information CLI.""" |
| 94 | + runner = CliRunner() |
| 95 | + slide_info_result = runner.invoke( |
| 96 | + cli.main, |
| 97 | + [ |
| 98 | + "slide-info", |
| 99 | + "--wsi_input", |
| 100 | + ".", |
| 101 | + "--file_types", |
| 102 | + '"*.ndpi, *.svs"', |
| 103 | + "--workers", |
| 104 | + "2", |
| 105 | + ], |
| 106 | + ) |
| 107 | + |
| 108 | + assert slide_info_result.exit_code == 0 |
0 commit comments