|
14 | 14 | # ------------------------------------------------------------------------------------- |
15 | 15 | # FIBContext test utilty functions and fixtures |
16 | 16 | # ------------------------------------------------------------------------------------- |
| 17 | +num_lamellae = 5 |
17 | 18 |
|
18 | 19 |
|
19 | 20 | @pytest.fixture |
20 | 21 | def visit_dir(tmp_path: Path): |
21 | 22 | return tmp_path / "visit" |
22 | 23 |
|
23 | 24 |
|
| 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 | + |
24 | 55 | @pytest.fixture |
25 | 56 | def fib_maps_images(visit_dir: Path): |
26 | 57 | image_list = [] |
27 | 58 | for i in range(4): |
28 | 59 | name = "Electron Snapshot" |
29 | 60 | if i > 0: |
30 | | - name += f" ({i})" |
| 61 | + name += f" ({i + 1})" |
31 | 62 | file = visit_dir / "maps/visit/LayersData/Layer" / f"{name}.tiff" |
32 | 63 | if not file.exists(): |
33 | 64 | file.parent.mkdir(parents=True, exist_ok=True) |
@@ -97,8 +128,52 @@ def test_file_transferred_to( |
97 | 128 | ) == destination_dir / file.relative_to(visit_dir) |
98 | 129 |
|
99 | 130 |
|
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 |
102 | 177 |
|
103 | 178 |
|
104 | 179 | def test_fib_maps_context( |
@@ -139,8 +214,13 @@ def test_fib_maps_context( |
139 | 214 | # Parse images one-by-one |
140 | 215 | for file in fib_maps_images: |
141 | 216 | 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 | + ) |
144 | 224 | assert mock_register_fib_atlas.call_count == len(fib_maps_images) |
145 | 225 | for dst in destination_files: |
146 | 226 | mock_register_fib_atlas.assert_any_call( |
|
0 commit comments