Skip to content

Commit 0383f1f

Browse files
committed
test: change test to only catch function behavior.
1 parent b2cc5c5 commit 0383f1f

File tree

6 files changed

+33
-62
lines changed

6 files changed

+33
-62
lines changed

docs/examples/example.tiff

64.2 KB
Binary file not shown.

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
@pytest.fixture
88
def user_filesystem(tmp_path):
99
base_dir = Path(tmp_path)
10-
input_dir = base_dir / "input_dir"
10+
cwd_dir = base_dir / "cwd_dir"
1111
home_dir = base_dir / "home_dir"
1212
test_dir = base_dir / "test_dir"
13-
for dir in (input_dir, home_dir, test_dir):
13+
for dir in (cwd_dir, home_dir, test_dir):
1414
dir.mkdir(parents=True, exist_ok=True)
1515

1616
home_config_data = {
@@ -22,7 +22,7 @@ def user_filesystem(tmp_path):
2222

2323
yield {
2424
"base": base_dir,
25-
"input": input_dir,
25+
"cwd": cwd_dir,
2626
"home": home_dir,
2727
"test": test_dir,
2828
}

tests/cwd/example.tiff

64.2 KB
Binary file not shown.

tests/home/example.tiff

64.2 KB
Binary file not shown.

tests/test/example.tiff

64.2 KB
Binary file not shown.

tests/test_loadimage.py

Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2+
import shutil
23
from pathlib import Path
3-
from unittest import mock
44

55
import numpy as np
66
import pytest
@@ -9,69 +9,40 @@
99

1010
PROJECT_ROOT = Path(__file__).resolve().parents[1]
1111

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),
3824
]
3925

4026

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
5330

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)
5536

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)
5939

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)
6740
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)
7647
finally:
77-
os.chdir(cwd)
48+
os.chdir(old_cwd)

0 commit comments

Comments
 (0)