Skip to content

Commit 9a28a4b

Browse files
committed
tests: add test to loadimage
1 parent 3f4f892 commit 9a28a4b

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

src/diffpy/srxplanar/loadimage.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ def loadImage(self, filename):
8383
filenamefull = found_files[0] if found_files else None
8484

8585
if filenamefull is None or not filenamefull.exists():
86-
print(f"Warning: file not found: {filename}")
86+
print(
87+
f"Warning: file not found: {filename}, "
88+
f"Please rerun specifying a valid filename."
89+
)
8790
return np.zeros((100, 100))
8891

8992
image = np.zeros((100, 100))

tests/conftest.py

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,114 @@
11
import json
2+
import os
23
from pathlib import Path
34

5+
import numpy as np
46
import pytest
57

8+
from diffpy.srxplanar.loadimage import LoadImage
69

10+
11+
# ----------------------------------------------------------------------
12+
# Filesystem fixture following the structured format
13+
# ----------------------------------------------------------------------
714
@pytest.fixture
815
def user_filesystem(tmp_path):
916
base_dir = Path(tmp_path)
17+
input_dir = base_dir / "input_dir"
1018
home_dir = base_dir / "home_dir"
11-
home_dir.mkdir(parents=True, exist_ok=True)
12-
cwd_dir = base_dir / "cwd_dir"
13-
cwd_dir.mkdir(parents=True, exist_ok=True)
19+
test_dir = base_dir / "test_dir"
20+
for d in (input_dir, home_dir, test_dir):
21+
d.mkdir(parents=True, exist_ok=True)
1422

15-
home_config_data = {"username": "home_username", "email": "[email protected]"}
23+
home_config_data = {
24+
"username": "home_username",
25+
"email": "[email protected]",
26+
}
1627
with open(home_dir / "diffpyconfig.json", "w") as f:
1728
json.dump(home_config_data, f)
1829

19-
yield tmp_path
30+
yield {
31+
"base": base_dir,
32+
"input": input_dir,
33+
"home": home_dir,
34+
"test": test_dir,
35+
}
36+
37+
38+
# ----------------------------------------------------------------------
39+
# LoadImage test setup
40+
# ----------------------------------------------------------------------
41+
@pytest.fixture(scope="module")
42+
def example_tif():
43+
"""Locate the example TIFF file relative to repo root."""
44+
tif_path = Path("../docs/examples/KFe2As2-00838.tif").resolve()
45+
if not tif_path.exists():
46+
pytest.skip(f"Example TIFF not found at {tif_path}")
47+
return tif_path
48+
49+
50+
@pytest.fixture(scope="module")
51+
def reference_matrix(example_tif):
52+
"""Load reference matrix directly from the example TIFF once per
53+
session."""
54+
cfg = type("Cfg", (), {"fliphorizontal": True, "flipvertical": False})()
55+
loader = LoadImage(cfg)
56+
return loader.loadImage(example_tif)
57+
58+
59+
@pytest.fixture
60+
def loader():
61+
cfg = type("Cfg", (), {"fliphorizontal": True, "flipvertical": False})()
62+
return LoadImage(cfg)
63+
64+
65+
# ----------------------------------------------------------------------
66+
# Unified test
67+
# ----------------------------------------------------------------------
68+
69+
70+
def test_load_image_all_cases(
71+
loader, example_tif, user_filesystem, monkeypatch
72+
):
73+
home_dir = user_filesystem["home"]
74+
test_dir = user_filesystem["test"]
75+
monkeypatch.setenv("HOME", str(home_dir))
76+
77+
# Load once as ground truth
78+
expected = loader.loadImage(example_tif)
79+
80+
# ---- Case 1: absolute path ----
81+
abs_tif = home_dir / "diffpy.srxplanar/docs/examples/KFe2As2-00838.tif"
82+
abs_tif.parent.mkdir(parents=True, exist_ok=True)
83+
abs_tif.write_bytes(example_tif.read_bytes())
84+
result_abs = loader.loadImage(abs_tif)
85+
assert np.array_equal(result_abs, expected)
86+
87+
# ---- Case 2 & 3: CWD and relative ----
88+
cwd_tif = test_dir / example_tif.name
89+
cwd_tif.write_bytes(example_tif.read_bytes())
90+
91+
rel_dir = test_dir / "diffpy.srxplanar/docs/examples"
92+
rel_dir.mkdir(parents=True, exist_ok=True)
93+
rel_tif = rel_dir / example_tif.name
94+
rel_tif.write_bytes(example_tif.read_bytes())
95+
96+
cwd = Path.cwd()
97+
os.chdir(test_dir)
98+
try:
99+
result_cwd = loader.loadImage(example_tif.name)
100+
assert np.array_equal(result_cwd, expected)
101+
102+
relative_path = (
103+
Path("diffpy.srxplanar/docs/examples") / example_tif.name
104+
)
105+
result_rel = loader.loadImage(relative_path)
106+
assert np.array_equal(result_rel, expected)
107+
108+
# ---- Case 4: missing file ----
109+
result_missing = loader.loadImage("nonexistent_file.tif")
110+
assert result_missing.shape == (100, 100)
111+
assert np.all(result_missing == 0)
112+
113+
finally:
114+
os.chdir(cwd)

0 commit comments

Comments
 (0)