Skip to content

Commit 2f63156

Browse files
committed
Added unit test for the FIBContext for the AutoTEM post transfer workflow
1 parent c190238 commit 2f63156

1 file changed

Lines changed: 85 additions & 5 deletions

File tree

tests/client/contexts/test_fib.py

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,51 @@
1414
# -------------------------------------------------------------------------------------
1515
# FIBContext test utilty functions and fixtures
1616
# -------------------------------------------------------------------------------------
17+
num_lamellae = 5
1718

1819

1920
@pytest.fixture
2021
def visit_dir(tmp_path: Path):
2122
return tmp_path / "visit"
2223

2324

25+
@pytest.fixture
26+
def fib_autotem_dc_images(visit_dir: Path):
27+
stages = ("Rough-Milling", "Polishing-1", "Polishing-2")
28+
images_per_stage = 2
29+
30+
# Create images as Murfey would expect to find them in the DCImages folder
31+
image_list = []
32+
for l in range(num_lamellae):
33+
for s, stage in enumerate(stages):
34+
for i in range(images_per_stage):
35+
lamella_folder = "Lamella"
36+
if l > 0:
37+
lamella_folder += f" ({l + 1})"
38+
# Continuously increment seconds count between files
39+
timestamp = (
40+
f"2025-05-10-12-34-{str(0 + i + (s * images_per_stage)).zfill(2)}"
41+
)
42+
file = (
43+
visit_dir
44+
/ f"autotem/visit/Sites/{lamella_folder}"
45+
/ "DCImages/DCM_2025-05-10-12-34-00.125"
46+
/ f"{timestamp}-{stage}-dc_rescan-image-.png"
47+
)
48+
if not file.exists():
49+
file.parent.mkdir(parents=True, exist_ok=True)
50+
file.touch()
51+
image_list.append(file)
52+
return image_list
53+
54+
2455
@pytest.fixture
2556
def fib_maps_images(visit_dir: Path):
2657
image_list = []
2758
for i in range(4):
2859
name = "Electron Snapshot"
2960
if i > 0:
30-
name += f" ({i})"
61+
name += f" ({i + 1})"
3162
file = visit_dir / "maps/visit/LayersData/Layer" / f"{name}.tiff"
3263
if not file.exists():
3364
file.parent.mkdir(parents=True, exist_ok=True)
@@ -97,8 +128,52 @@ def test_file_transferred_to(
97128
) == destination_dir / file.relative_to(visit_dir)
98129

99130

100-
def test_fib_autotem_context():
101-
pass
131+
def test_fib_autotem_context(
132+
mocker: MockerFixture,
133+
tmp_path: Path,
134+
visit_dir: Path,
135+
fib_autotem_dc_images: list[Path],
136+
):
137+
# Mock the environment
138+
mock_environment = MagicMock()
139+
140+
# Create a list of destinations
141+
destination_dir = tmp_path / "fib" / "data" / "current_year" / "visit"
142+
destination_files = [
143+
destination_dir / file.relative_to(visit_dir) for file in fib_autotem_dc_images
144+
]
145+
146+
# Mock the functions used in 'post_transfer'
147+
mock_get_source = mocker.patch(
148+
"murfey.client.contexts.fib._get_source", return_value=tmp_path
149+
)
150+
mock_file_transferred_to = mocker.patch(
151+
"murfey.client.contexts.fib._file_transferred_to", side_effect=destination_files
152+
)
153+
mock_capture_post = mocker.patch("murfey.client.contexts.fib.capture_post")
154+
155+
# Initialise the FIBContext
156+
basepath = tmp_path
157+
context = FIBContext(
158+
acquisition_software="autotem",
159+
basepath=basepath,
160+
machine_config={},
161+
token="",
162+
)
163+
164+
# Parse images one-by-one and check that expected calls were made
165+
for file in fib_autotem_dc_images:
166+
context.post_transfer(file, environment=mock_environment)
167+
mock_get_source.assert_called_with(file, mock_environment)
168+
mock_file_transferred_to.assert_called_with(
169+
environment=mock_environment,
170+
source=basepath,
171+
file_path=file,
172+
rsync_basepath=Path(""),
173+
)
174+
assert mock_capture_post.call_count == len(fib_autotem_dc_images)
175+
assert len(context._milling) == num_lamellae
176+
assert len(context._lamellae) == num_lamellae
102177

103178

104179
def test_fib_maps_context(
@@ -139,8 +214,13 @@ def test_fib_maps_context(
139214
# Parse images one-by-one
140215
for file in fib_maps_images:
141216
context.post_transfer(file, environment=mock_environment)
142-
assert mock_get_source.call_count == len(fib_maps_images)
143-
assert mock_file_transferred_to.call_count == len(fib_maps_images)
217+
mock_get_source.assert_called_with(file, mock_environment)
218+
mock_file_transferred_to.assert_called_with(
219+
environment=mock_environment,
220+
source=basepath,
221+
file_path=file,
222+
rsync_basepath=Path(""),
223+
)
144224
assert mock_register_fib_atlas.call_count == len(fib_maps_images)
145225
for dst in destination_files:
146226
mock_register_fib_atlas.assert_any_call(

0 commit comments

Comments
 (0)