|
1 | 1 | import json |
| 2 | +import os |
2 | 3 | from pathlib import Path |
3 | 4 |
|
| 5 | +import numpy as np |
4 | 6 | import pytest |
5 | 7 |
|
| 8 | +from diffpy.srxplanar.loadimage import LoadImage |
6 | 9 |
|
| 10 | + |
| 11 | +# ---------------------------------------------------------------------- |
| 12 | +# Filesystem fixture following the structured format |
| 13 | +# ---------------------------------------------------------------------- |
7 | 14 | @pytest.fixture |
8 | 15 | def user_filesystem(tmp_path): |
9 | 16 | base_dir = Path(tmp_path) |
| 17 | + input_dir = base_dir / "input_dir" |
10 | 18 | 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) |
14 | 22 |
|
15 | | - home_config_data = { "username": "home_username", "email": "[email protected]"} |
| 23 | + home_config_data = { |
| 24 | + "username": "home_username", |
| 25 | + |
| 26 | + } |
16 | 27 | with open(home_dir / "diffpyconfig.json", "w") as f: |
17 | 28 | json.dump(home_config_data, f) |
18 | 29 |
|
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