|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from murfey.server.ispyb import TransportManager |
| 7 | +from murfey.workflows.clem.process_raw_tiffs import zocalo_cluster_request |
| 8 | + |
| 9 | +# Set up variables |
| 10 | +session_id = 0 |
| 11 | +instrument_name = "clem" |
| 12 | +root_folder = "images" |
| 13 | +visit_name = "cm12345-6" |
| 14 | +area_name = "test_area" |
| 15 | +feedback_queue = "murfey_feedback" |
| 16 | + |
| 17 | +# Properties for TIFF images |
| 18 | +num_z = 5 |
| 19 | +num_c = 3 |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def raw_dir(tmp_path: Path): |
| 24 | + raw_dir = tmp_path / visit_name / root_folder |
| 25 | + raw_dir.mkdir(parents=True, exist_ok=True) |
| 26 | + return raw_dir |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def tiff_list(raw_dir: Path): |
| 31 | + tiff_list = [ |
| 32 | + raw_dir / area_name / f"test_series--Z{str(z).zfill(2)}--C{str(c).zfill(2)}.tif" |
| 33 | + for z in range(num_z) |
| 34 | + for c in range(num_c) |
| 35 | + ] |
| 36 | + for file in tiff_list: |
| 37 | + if not file.exists(): |
| 38 | + file.touch() |
| 39 | + return tiff_list |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def metadata(raw_dir: Path): |
| 44 | + metadata = raw_dir / area_name / "Metadata" / "test_series.xlif" |
| 45 | + if not metadata.exists(): |
| 46 | + metadata.touch() |
| 47 | + return metadata |
| 48 | + |
| 49 | + |
| 50 | +def test_zocalo_cluster_request( |
| 51 | + tiff_list: list[Path], |
| 52 | + metadata: Path, |
| 53 | + raw_dir: Path, |
| 54 | +): |
| 55 | + |
| 56 | + # Create a mock tranpsort object |
| 57 | + mock_transport = MagicMock(spec=TransportManager) |
| 58 | + mock_transport.feedback_queue = feedback_queue |
| 59 | + |
| 60 | + # Run the function with the listed parameters |
| 61 | + zocalo_cluster_request( |
| 62 | + tiff_list=tiff_list, |
| 63 | + root_folder=root_folder, |
| 64 | + session_id=session_id, |
| 65 | + instrument_name=instrument_name, |
| 66 | + metadata=metadata, |
| 67 | + messenger=mock_transport, |
| 68 | + ) |
| 69 | + |
| 70 | + # Construct the recipe that we expect to send |
| 71 | + job_name = "--".join( |
| 72 | + [ |
| 73 | + p.replace(" ", "_") if " " in p else p |
| 74 | + for p in ( |
| 75 | + tiff_list[0].parent.relative_to(raw_dir) |
| 76 | + / tiff_list[0].stem.split("--")[0] |
| 77 | + ).parts |
| 78 | + ] |
| 79 | + ) |
| 80 | + sent_recipe = { |
| 81 | + "recipes": ["clem-tiff-to-stack"], |
| 82 | + "parameters": { |
| 83 | + # Job parameters |
| 84 | + "tiff_list": "null", |
| 85 | + "tiff_file": f"{str(tiff_list[0])}", |
| 86 | + "root_folder": root_folder, |
| 87 | + "metadata": f"{str(metadata)}", |
| 88 | + # Other recipe parameters |
| 89 | + "session_dir": f"{str(raw_dir.parent)}", |
| 90 | + "session_id": session_id, |
| 91 | + "job_name": job_name, |
| 92 | + "feedback_queue": feedback_queue, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + # Check that it sends the expected recipe |
| 97 | + mock_transport.send.assert_called_once_with( |
| 98 | + "processing_recipe", |
| 99 | + sent_recipe, |
| 100 | + new_connection=True, |
| 101 | + ) |
0 commit comments