11import json
2- import os
32from pathlib import Path
43
5- import numpy as np
64import pytest
75
8- from diffpy .srxplanar .loadimage import LoadImage
96
10-
11- # ----------------------------------------------------------------------
12- # Filesystem fixture following the structured format
13- # ----------------------------------------------------------------------
147@pytest .fixture
158def 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 )
0 commit comments