Skip to content

Commit dfa5efb

Browse files
committed
Add tests for '_get_source' and '_file_transferred_to' for the CLEMContext
1 parent 1f46138 commit dfa5efb

1 file changed

Lines changed: 133 additions & 6 deletions

File tree

tests/client/contexts/test_clem.py

Lines changed: 133 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,141 @@
1-
def test_post_transfer_lif_data():
2-
pass
1+
from pathlib import Path
2+
from unittest.mock import MagicMock
33

4+
import pytest
45

5-
def test_post_transfer_tiff_data():
6-
pass
6+
from murfey.client.context import _file_transferred_to, _get_source
7+
8+
visit_name = "cm12345-6"
9+
project_name = "2025_06_30_10_00_00--grid_1001"
10+
example_file_paths = [
11+
f"{project_name}{path}"
12+
for path in [
13+
# TIFF files
14+
"/TileScan 1/Position 1--Z00.tif",
15+
"/TileScan 1/Position 1--C00.tif",
16+
"/TileScan 1/Position 1--Stage00.tif",
17+
"/TileScan 1/Position 1--Z00--C00.tif",
18+
"/TileScan 1/Position 1--Stage00--C00.tif",
19+
"/TileScan 1/Position 1--Stage00--Z00.tif",
20+
"/TileScan 1/Position 1--Stage00--Z00--C00.tif",
21+
"/TileScan 1/Position 1_ICC--Z00.tif",
22+
"/TileScan 1/Position 1_Lng_LVCC--C00.tif",
23+
"/TileScan 1/Position 1_Lng_SVCC--Stage00.tif",
24+
"/TileScan 1/Position 1_ICC--Z00--C00.tif",
25+
"/TileScan 1/Position 1_Lng_LVCC--Stage00--C00.tif",
26+
"/TileScan 1/Position 1_Lng_SVCC--Stage00--Z00.tif",
27+
"/TileScan 1/Metadata/Position 1.xlif",
28+
"/Series001--Z00--C00.tif",
29+
"/Series001--Stage00--C00.tif",
30+
"/Series001--Stage00--Z00.tif",
31+
"/Series001--Stage00--Z00--C00.tif",
32+
"/Series001_ICC--Z00.tif",
33+
"/Series001_Lng_LVCC--C00.tif",
34+
"/Series001_Lng_SVCC--Stage00.tif",
35+
"/Metadata/Series001_Lng_LVCC.xlif",
36+
"/Image 1_ICC--Z00--C00.tif",
37+
"/Image 1_Lng_LVCC--Stage00--C00.tif",
38+
"/Image 1_Lng_SVCC--Stage00--Z00.tif",
39+
"/Image 1--Z00.tif",
40+
"/Image 1--C00.tif",
41+
"/Image 1--Stage00.tif",
42+
"/Image 1--Stage00--Z00--C00.tif",
43+
"/Metadata/Image 1_ICC.xlif",
44+
# LIF file
45+
".lif",
46+
]
47+
]
48+
49+
50+
def create_tiff_dataset(
51+
visit_dir: Path,
52+
series_name: str,
53+
num_tiles: int | None = None,
54+
num_frames: int | None = None,
55+
num_channels: int | None = None,
56+
):
57+
"""
58+
Creates mock files mimicking the folder structure and naming pattern of actual
59+
CLEM TIFF datasets.
60+
"""
61+
62+
# Construct the TIFF files
63+
tiff_files = []
64+
for c in range(num_channels or 1):
65+
for t in range(num_tiles or 1):
66+
for z in range(num_frames or 1):
67+
# Construct name of file based on the presence of tiles, frames, and channels
68+
file_name = series_name
69+
if num_tiles is not None:
70+
file_name += f"--Stage{str(t).zfill(2)}"
71+
if num_frames is not None:
72+
file_name += f"--Z{str(z).zfill(2)}"
73+
if num_channels is not None:
74+
file_name += f"--C{str(c).zfill(2)}"
75+
file_name += ".tif"
76+
file = visit_dir / "images" / file_name
77+
file.touch(exist_ok=True)
78+
tiff_files.append(file)
79+
80+
# Construct the metadata file
81+
file_name = f"{series_name}.xlif"
82+
metadata_file = visit_dir / "images" / file_name
83+
# Insert "Metadata" before the file name
84+
metadata_file = metadata_file.parent / "Metadata" / metadata_file.name
85+
metadata_file.touch(exist_ok=True)
86+
87+
return tiff_files, metadata_file
788

889

9-
def test_process_lif_file():
90+
@pytest.fixture
91+
def visit_dir(
92+
tmp_path: Path,
93+
):
94+
visit_dir = tmp_path / visit_name
95+
visit_dir.mkdir(parents=True, exist_ok=True)
96+
return visit_dir
97+
98+
99+
@pytest.mark.parametrize("file_path", example_file_paths)
100+
def test_get_source(
101+
tmp_path: Path,
102+
visit_dir: Path,
103+
file_path: str,
104+
):
105+
# Mock the MurfeyInstanceEnvironment
106+
mock_environment = MagicMock()
107+
mock_environment.sources = [
108+
visit_dir,
109+
tmp_path / "another_dir",
110+
]
111+
# Check that the correct source directory is found
112+
assert _get_source(visit_dir / "images" / file_path, mock_environment) == visit_dir
113+
114+
115+
@pytest.mark.parametrize("file_path", example_file_paths)
116+
def test_file_transferred_to(tmp_path: Path, visit_dir: Path, file_path: str):
117+
# Create the client-side file
118+
file = visit_dir / "images" / file_path
119+
120+
# Mock the environment
121+
mock_environment = MagicMock()
122+
mock_environment.default_destinations = {visit_dir: "current_year"}
123+
mock_environment.visit = visit_name
124+
125+
# Iterate across the FIB files to compare against
126+
destination_dir = tmp_path / "clem" / "data" / "current_year" / visit_name
127+
# Work out what the expected destination will be
128+
assert _file_transferred_to(
129+
environment=mock_environment,
130+
source=visit_dir,
131+
file_path=file,
132+
rsync_basepath=tmp_path / "clem" / "data",
133+
) == destination_dir / file.relative_to(visit_dir)
134+
135+
136+
def test_post_transfer_lif_data():
10137
pass
11138

12139

13-
def test_process_tiff_file():
140+
def test_post_transfer_tiff_data():
14141
pass

0 commit comments

Comments
 (0)