|
1 | 1 | import os |
| 2 | +import shutil |
2 | 3 | from pathlib import Path |
3 | | -from unittest import mock |
4 | 4 |
|
5 | 5 | import numpy as np |
6 | 6 | import pytest |
|
9 | 9 |
|
10 | 10 | PROJECT_ROOT = Path(__file__).resolve().parents[1] |
11 | 11 |
|
12 | | - |
13 | | -def reference_matrix(): |
14 | | - """Load reference matrix directly from the example TIFF.""" |
15 | | - tif_path = PROJECT_ROOT / "docs/examples/KFe2As2-00838.tif" |
16 | | - cfg = type("Cfg", (), {"fliphorizontal": True, "flipvertical": False})() |
17 | | - image_loader = LoadImage(cfg) |
18 | | - return image_loader.loadImage(tif_path) |
19 | | - |
20 | | - |
21 | | -def build_loadimage_path(case_tag, home_dir): |
22 | | - """Return the Path object for LoadImage test cases. |
23 | | -
|
24 | | - case_tag: one of 'abs', 'rel', 'missing' |
25 | | - """ |
26 | | - if case_tag == "abs": |
27 | | - return home_dir / "diffpy.srxplanar/docs/examples/KFe2As2-00838.tif" |
28 | | - elif case_tag == "rel": |
29 | | - return Path("diffpy.srxplanar/docs/examples/KFe2As2-00838.tif") |
30 | | - elif case_tag == "missing": |
31 | | - return Path("nonexistent_file.tif") |
32 | | - |
33 | | - |
34 | | -load_image_param = load_image_params = [ |
35 | | - ("abs", PROJECT_ROOT / "docs/examples/KFe2As2-00838.tif"), |
36 | | - ("rel", PROJECT_ROOT / "docs/examples/KFe2As2-00838.tif"), |
37 | | - ("missing", PROJECT_ROOT / "docs/examples/KFe2As2-00838.tif"), |
| 12 | +load_image_param = [ |
| 13 | + # case 1: just filename of file in current directory. |
| 14 | + # expect function loads tiff file from cwd |
| 15 | + ("example.tiff", True), |
| 16 | + # case 2: absolute file path to file in another directory. |
| 17 | + # expect file is found and correctly read. |
| 18 | + ("home_dir/example.tiff", True), |
| 19 | + # case 3: relative file path to file in another directory. |
| 20 | + # expect file is found and correctly read |
| 21 | + ("./example.tiff", True), |
| 22 | + # case 4: non-existent file that incurred by mistype. |
| 23 | + ("nonexistent_file.tif", False), |
38 | 24 | ] |
39 | 25 |
|
40 | 26 |
|
41 | | -@pytest.mark.parametrize("case_tag,expected_path", load_image_params) |
42 | | -def test_load_image_cases( |
43 | | - user_filesystem, monkeypatch, case_tag, expected_path |
44 | | -): |
45 | | - home_dir = user_filesystem["home"] |
46 | | - test_dir = user_filesystem["test"] |
47 | | - |
48 | | - cfg = type("Cfg", (), {"fliphorizontal": True, "flipvertical": False})() |
49 | | - loader = LoadImage(cfg) |
50 | | - |
51 | | - with mock.patch.dict(os.environ, {"HOME": str(home_dir)}): |
52 | | - expected = loader.loadImage(expected_path) |
| 27 | +@pytest.mark.parametrize("input_path, expected", load_image_param) |
| 28 | +def test_load_image_cases(input_path, expected, user_filesystem): |
| 29 | + base_dir, home_dir, cwd_dir, test_dir = user_filesystem |
53 | 30 |
|
54 | | - expected = loader.loadImage(expected_path) |
| 31 | + # Copy test image into all directories |
| 32 | + for dir in [cwd_dir, home_dir, test_dir]: |
| 33 | + dst = Path(dir) / "example.tiff" |
| 34 | + dst.parent.mkdir(parents=True, exist_ok=True) |
| 35 | + shutil.copy("../docs/examples/example.tiff", dst) |
55 | 36 |
|
56 | | - abs_file = home_dir / "diffpy.srxplanar/docs/examples/KFe2As2-00838.tif" |
57 | | - abs_file.parent.mkdir(parents=True, exist_ok=True) |
58 | | - abs_file.write_bytes(expected_path.read_bytes()) |
| 37 | + old_cwd = Path.cwd() |
| 38 | + os.chdir(home_dir) |
59 | 39 |
|
60 | | - rel_dir = test_dir / "diffpy.srxplanar/docs/examples" |
61 | | - rel_dir.mkdir(parents=True, exist_ok=True) |
62 | | - rel_file = rel_dir / "KFe2As2-00838.tif" |
63 | | - rel_file.write_bytes(expected_path.read_bytes()) |
64 | | - |
65 | | - cwd = Path.cwd() |
66 | | - os.chdir(test_dir) |
67 | 40 | try: |
68 | | - input_path = build_loadimage_path(case_tag, home_dir) |
69 | | - result = loader.loadImage(input_path) |
70 | | - |
71 | | - if case_tag == "missing": |
72 | | - assert result.shape == (100, 100) |
73 | | - assert np.all(result == 0) |
74 | | - else: |
75 | | - assert np.array_equal(result, expected) |
| 41 | + cfg = type( |
| 42 | + "Cfg", (), {"fliphorizontal": True, "flipvertical": False} |
| 43 | + )() |
| 44 | + loader = LoadImage(cfg) |
| 45 | + actual = loader.loadImage(input_path) |
| 46 | + assert isinstance(actual, np.ndarray) |
76 | 47 | finally: |
77 | | - os.chdir(cwd) |
| 48 | + os.chdir(old_cwd) |
0 commit comments