Skip to content

Commit b8d2b4f

Browse files
committed
Updated test for FIBContext to handle new drift correction FIB image logic
1 parent 8518690 commit b8d2b4f

1 file changed

Lines changed: 89 additions & 8 deletions

File tree

tests/client/contexts/test_fib.py

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
_number_from_name,
1717
_parse_boolean,
1818
)
19+
from murfey.util.models import LamellaSiteInfo
1920

2021
# Mock session values
2122
num_lamellae = 5
@@ -595,27 +596,49 @@ def test_fib_autotem_context_projectdata(
595596

596597
@pytest.mark.parametrize(
597598
"test_params",
598-
( # Use environment? | Find source? | Find destination?
599-
(True, True, True),
600-
(False, True, True),
601-
(True, False, True),
602-
(True, True, False),
599+
(
600+
# Early exits
601+
# No MurfeyInstanceEnvironment
602+
(False, True, True, True, True, True, True),
603+
# No source
604+
(True, False, True, True, True, True, True),
605+
# No destination
606+
(True, True, False, True, True, True, True),
607+
# No site info
608+
(True, True, True, False, True, True, True),
609+
# No project name
610+
(True, True, True, True, False, True, True),
611+
# No stage position
612+
(True, True, True, True, True, False, True),
613+
# No stage position values
614+
(True, True, True, True, True, True, False),
615+
# Successful case
616+
(True, True, True, True, True, True, True),
603617
),
604618
)
605619
def test_fib_autotem_context_drift_correction_images(
606620
mocker: MockerFixture,
607-
test_params: tuple[bool, bool, bool],
621+
test_params: tuple[bool, bool, bool, bool, bool, bool, bool],
608622
tmp_path: Path,
609623
visit_dir: Path,
610624
fib_autotem_dc_images: list[Path],
611625
):
612626
# Unpack test params
613-
use_env, find_source, find_dst = test_params
627+
(
628+
use_env,
629+
find_source,
630+
find_dst,
631+
has_site_info,
632+
has_project_name,
633+
has_stage_position,
634+
has_stage_values,
635+
) = test_params
614636

615637
# Mock the environment
616638
mock_environment = None
617639
if use_env:
618640
mock_environment = MagicMock()
641+
mock_environment.visit = visit_name
619642

620643
# Mock the logger to check if specific logs are triggered
621644
mock_logger = mocker.patch("murfey.client.contexts.fib.logger")
@@ -649,6 +672,23 @@ def test_fib_autotem_context_drift_correction_images(
649672
token="",
650673
)
651674

675+
# Create the Pydantic model for each site and add metadata
676+
for i in range(num_lamellae):
677+
lamella_num = i + 1
678+
metadata_dict = {
679+
"site_name": f"Lamella ({lamella_num})",
680+
"site_number": lamella_num,
681+
}
682+
if has_project_name:
683+
metadata_dict["project_name"] = project_name
684+
if has_stage_position:
685+
stage_dict: dict[str, dict] = {"preparation": {}}
686+
if has_stage_values:
687+
stage_dict["preparation"] = {"x": 0.003}
688+
metadata_dict["stage_info"] = stage_dict
689+
if has_site_info:
690+
context._site_info[lamella_num] = LamellaSiteInfo(**metadata_dict)
691+
652692
# Parse images one-by-one and check that expected calls were made
653693
for file in fib_autotem_dc_images:
654694
context.post_transfer(file, environment=mock_environment)
@@ -660,6 +700,22 @@ def test_fib_autotem_context_drift_correction_images(
660700
mock_logger.warning.assert_called_with(
661701
f"File {file.name!r} not found on storage system"
662702
)
703+
elif not has_site_info:
704+
mock_logger.debug.assert_called_with(
705+
f"No metadata found for site {lamella_num} yet"
706+
)
707+
elif not has_project_name:
708+
mock_logger.warning.assert_called_with(
709+
f"No project name associated with site {lamella_num}"
710+
)
711+
elif not has_stage_position:
712+
mock_logger.warning.assert_called_with(
713+
f"No stage position information associated with site {lamella_num}"
714+
)
715+
elif not has_stage_values:
716+
mock_logger.warning.assert_called_with(
717+
f"Could not determine slot number of site {lamella_num}"
718+
)
663719
else:
664720
mock_get_source.assert_called_with(file, mock_environment)
665721
mock_file_transferred_to.assert_called_with(
@@ -668,9 +724,34 @@ def test_fib_autotem_context_drift_correction_images(
668724
file_path=file,
669725
rsync_basepath=Path(""),
670726
)
671-
assert mock_capture_post.call_count == len(fib_autotem_dc_images)
672727
assert len(context._drift_correction_images) == num_lamellae
673728

729+
for i in range(num_lamellae):
730+
lamella_num = i + 1
731+
# The '_site_info' attribute should now be populated
732+
assert (
733+
context._site_info[lamella_num].stage_info.preparation.slot_number == 2
734+
)
735+
736+
# The output file should point to 'grid_2' for a positive x stage position
737+
output_file = (
738+
tmp_path
739+
/ "fib"
740+
/ "data"
741+
/ "current_year"
742+
/ visit_name
743+
/ "processed"
744+
/ project_name
745+
/ "grid_2"
746+
/ "drift_correction"
747+
/ f"lamella_{lamella_num}.gif"
748+
)
749+
assert (
750+
context._drift_correction_images[lamella_num].output_file == output_file
751+
)
752+
# 'capture_post' should be called for every image
753+
assert mock_capture_post.call_count == len(destination_files)
754+
674755

675756
def test_fib_maps_context(
676757
mocker: MockerFixture,

0 commit comments

Comments
 (0)