Skip to content

Commit b2cc5c5

Browse files
committed
test: change test format.
1 parent 9a28a4b commit b2cc5c5

File tree

2 files changed

+79
-88
lines changed

2 files changed

+79
-88
lines changed

tests/conftest.py

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
import json
2-
import os
32
from pathlib import Path
43

5-
import numpy as np
64
import pytest
75

8-
from diffpy.srxplanar.loadimage import LoadImage
96

10-
11-
# ----------------------------------------------------------------------
12-
# Filesystem fixture following the structured format
13-
# ----------------------------------------------------------------------
147
@pytest.fixture
158
def user_filesystem(tmp_path):
169
base_dir = Path(tmp_path)
1710
input_dir = base_dir / "input_dir"
1811
home_dir = base_dir / "home_dir"
1912
test_dir = base_dir / "test_dir"
20-
for d in (input_dir, home_dir, test_dir):
21-
d.mkdir(parents=True, exist_ok=True)
13+
for dir in (input_dir, home_dir, test_dir):
14+
dir.mkdir(parents=True, exist_ok=True)
2215

2316
home_config_data = {
2417
"username": "home_username",
@@ -33,82 +26,3 @@ def user_filesystem(tmp_path):
3326
"home": home_dir,
3427
"test": test_dir,
3528
}
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)

tests/test_loadimage.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import os
2+
from pathlib import Path
3+
from unittest import mock
4+
5+
import numpy as np
6+
import pytest
7+
8+
from diffpy.srxplanar.loadimage import LoadImage
9+
10+
PROJECT_ROOT = Path(__file__).resolve().parents[1]
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"),
38+
]
39+
40+
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)
53+
54+
expected = loader.loadImage(expected_path)
55+
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())
59+
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+
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)
76+
finally:
77+
os.chdir(cwd)

0 commit comments

Comments
 (0)