|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +from sqlmodel import Session, select |
| 4 | + |
| 5 | +from murfey.server.api.workflow import register_dc_group |
| 6 | +from murfey.util.db import DataCollectionGroup, SearchMap |
| 7 | +from tests.conftest import ExampleVisit |
| 8 | + |
| 9 | + |
| 10 | +@mock.patch("murfey.server.workflow._transport_object") |
| 11 | +def test_register_dc_group_new_dcg(mock_transport, murfey_db_session: Session): |
| 12 | + """Test the request for a completely new data collection group""" |
| 13 | + mock_transport.feedback_queue = "mock_feedback_queue" |
| 14 | + |
| 15 | + # Request new dcg registration |
| 16 | + dcg_params = { |
| 17 | + "experiment_type_id": 44, |
| 18 | + "tag": "atlas_tag", |
| 19 | + "atlas": "/path/to/Atlas_1.jpg", |
| 20 | + "sample": 1, |
| 21 | + "atlas_pixel_size": 1e-5, |
| 22 | + } |
| 23 | + register_dc_group( |
| 24 | + visit_name="cm12345-6", |
| 25 | + session_id=ExampleVisit.murfey_session_id, |
| 26 | + dcg_params=dcg_params, |
| 27 | + db=murfey_db_session, |
| 28 | + ) |
| 29 | + |
| 30 | + # Check request for registering dcg in ispyb and murfey |
| 31 | + mock_transport.send.assert_called_once_with( |
| 32 | + "mock_feedback_queue", |
| 33 | + { |
| 34 | + "register": "data_collection_group", |
| 35 | + "start_time": mock.ANY, |
| 36 | + "experiment_type_id": dcg_params["experiment_type_id"], |
| 37 | + "tag": dcg_params["tag"], |
| 38 | + "session_id": ExampleVisit.murfey_session_id, |
| 39 | + "atlas": dcg_params["atlas"], |
| 40 | + "sample": dcg_params["sample"], |
| 41 | + "atlas_pixel_size": dcg_params["atlas_pixel_size"], |
| 42 | + "microscope": ExampleVisit.instrument_name, |
| 43 | + "proposal_code": ExampleVisit.proposal_code, |
| 44 | + "proposal_number": ExampleVisit.proposal_number, |
| 45 | + "visit_number": ExampleVisit.visit_number, |
| 46 | + }, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@mock.patch("murfey.server.workflow._transport_object") |
| 51 | +def test_register_dc_group_atlas_to_processing( |
| 52 | + mock_transport, murfey_db_session: Session |
| 53 | +): |
| 54 | + """ |
| 55 | + Test the request to update an existing data collection group |
| 56 | + from atlas type 44 to a processing type with a different tag |
| 57 | + """ |
| 58 | + mock_transport.feedback_queue = "mock_feedback_queue" |
| 59 | + |
| 60 | + # Make sure dcg is present |
| 61 | + dcg = DataCollectionGroup( |
| 62 | + id=1, |
| 63 | + session_id=ExampleVisit.murfey_session_id, |
| 64 | + tag="atlas_tag", |
| 65 | + atlas_id=90, |
| 66 | + atlas_pixel_size=1e-5, |
| 67 | + sample=1, |
| 68 | + atlas="/path/to/Atlas_1.jpg", |
| 69 | + ) |
| 70 | + murfey_db_session.add(dcg) |
| 71 | + murfey_db_session.commit() |
| 72 | + |
| 73 | + # Request new dcg registration with processing experiment type and tag |
| 74 | + dcg_params = { |
| 75 | + "experiment_type_id": 36, |
| 76 | + "tag": "processing_tag", |
| 77 | + "atlas": "/path/to/Atlas_1.jpg", |
| 78 | + "sample": 1, |
| 79 | + "atlas_pixel_size": 1e-5, |
| 80 | + } |
| 81 | + register_dc_group( |
| 82 | + visit_name="cm12345-6", |
| 83 | + session_id=ExampleVisit.murfey_session_id, |
| 84 | + dcg_params=dcg_params, |
| 85 | + db=murfey_db_session, |
| 86 | + ) |
| 87 | + |
| 88 | + # Check request to ispyb for updating the experiment type |
| 89 | + mock_transport.send.assert_called_once_with( |
| 90 | + "mock_feedback_queue", |
| 91 | + { |
| 92 | + "register": "experiment_type_update", |
| 93 | + "experiment_type_id": dcg_params["experiment_type_id"], |
| 94 | + "dcgid": dcg.id, |
| 95 | + }, |
| 96 | + ) |
| 97 | + |
| 98 | + # Check that the tag of the data collection group was updated |
| 99 | + new_dcg = murfey_db_session.exec( |
| 100 | + select(DataCollectionGroup).where(DataCollectionGroup.id == dcg.id) |
| 101 | + ).one() |
| 102 | + assert new_dcg.tag == dcg_params["tag"] |
| 103 | + |
| 104 | + |
| 105 | +@mock.patch("murfey.server.workflow._transport_object") |
| 106 | +def test_register_dc_group_processing_to_atlas( |
| 107 | + mock_transport, murfey_db_session: Session |
| 108 | +): |
| 109 | + """ |
| 110 | + Test the request to update an existing data collection group |
| 111 | + of processing type with a new atlas type 44, which should leave the tag unchanged |
| 112 | + """ |
| 113 | + mock_transport.feedback_queue = "mock_feedback_queue" |
| 114 | + |
| 115 | + # Make sure dcg is present |
| 116 | + dcg = DataCollectionGroup( |
| 117 | + id=1, |
| 118 | + session_id=ExampleVisit.murfey_session_id, |
| 119 | + tag="processing_tag", |
| 120 | + atlas_id=90, |
| 121 | + atlas_pixel_size=1e-5, |
| 122 | + sample=1, |
| 123 | + atlas="/path/to/Atlas_1.jpg", |
| 124 | + ) |
| 125 | + murfey_db_session.add(dcg) |
| 126 | + murfey_db_session.commit() |
| 127 | + |
| 128 | + # Request new dcg registration with atlas experiment type and tag |
| 129 | + dcg_params = { |
| 130 | + "experiment_type_id": 44, |
| 131 | + "tag": "atlas_tag", |
| 132 | + "atlas": "/path/to/Atlas_2.jpg", |
| 133 | + "sample": 1, |
| 134 | + "atlas_pixel_size": 1e-4, |
| 135 | + } |
| 136 | + register_dc_group( |
| 137 | + visit_name="cm12345-6", |
| 138 | + session_id=ExampleVisit.murfey_session_id, |
| 139 | + dcg_params=dcg_params, |
| 140 | + db=murfey_db_session, |
| 141 | + ) |
| 142 | + |
| 143 | + # Check request to ispyb for updating the experiment type |
| 144 | + mock_transport.send.assert_called_once_with( |
| 145 | + "mock_feedback_queue", |
| 146 | + { |
| 147 | + "register": "atlas_update", |
| 148 | + "atlas_id": dcg.atlas_id, |
| 149 | + "atlas": dcg_params["atlas"], |
| 150 | + "sample": dcg_params["sample"], |
| 151 | + "atlas_pixel_size": dcg_params["atlas_pixel_size"], |
| 152 | + "dcgid": dcg.id, |
| 153 | + "session_id": ExampleVisit.murfey_session_id, |
| 154 | + }, |
| 155 | + ) |
| 156 | + |
| 157 | + # Check the data collection group atlas was updated |
| 158 | + new_dcg = murfey_db_session.exec( |
| 159 | + select(DataCollectionGroup).where(DataCollectionGroup.id == dcg.id) |
| 160 | + ).one() |
| 161 | + assert new_dcg.atlas == dcg_params["atlas"] |
| 162 | + assert new_dcg.atlas_pixel_size == dcg_params["atlas_pixel_size"] |
| 163 | + # Check the tag of the data collection group was not updated |
| 164 | + assert new_dcg.tag != dcg_params["tag"] |
| 165 | + |
| 166 | + |
| 167 | +@mock.patch("murfey.server.workflow._transport_object") |
| 168 | +def test_register_dc_group_new_atlas(mock_transport, murfey_db_session: Session): |
| 169 | + """ |
| 170 | + Test the request to update an existing data collection group |
| 171 | + by adding an atlas, using the same tag |
| 172 | + """ |
| 173 | + mock_transport.feedback_queue = "mock_feedback_queue" |
| 174 | + mock_transport.do_insert_atlas.return_value = 5 |
| 175 | + |
| 176 | + # Make sure dcg is present without an atlas id |
| 177 | + dcg = DataCollectionGroup( |
| 178 | + id=1, |
| 179 | + session_id=ExampleVisit.murfey_session_id, |
| 180 | + tag="processing_tag", |
| 181 | + ) |
| 182 | + murfey_db_session.add(dcg) |
| 183 | + murfey_db_session.commit() |
| 184 | + |
| 185 | + # Request new dcg registration with atlas and exisiting tag |
| 186 | + dcg_params = { |
| 187 | + "experiment_type_id": 36, |
| 188 | + "tag": "processing_tag", |
| 189 | + "atlas": "/path/to/Atlas_2.jpg", |
| 190 | + "sample": 1, |
| 191 | + "atlas_pixel_size": 1e-4, |
| 192 | + } |
| 193 | + register_dc_group( |
| 194 | + visit_name="cm12345-6", |
| 195 | + session_id=ExampleVisit.murfey_session_id, |
| 196 | + dcg_params=dcg_params, |
| 197 | + db=murfey_db_session, |
| 198 | + ) |
| 199 | + |
| 200 | + # Check no sends are made by the transport object |
| 201 | + mock_transport.send.assert_not_called() |
| 202 | + |
| 203 | + # Check the call to insert the atlas into ispyb |
| 204 | + atlas_args = mock_transport.do_insert_atlas.call_args_list |
| 205 | + assert len(atlas_args) == 1 |
| 206 | + assert atlas_args[0].dataCollectionGroupId == dcg.id |
| 207 | + assert atlas_args[0].atlasImage == dcg_params["atlas"] |
| 208 | + assert atlas_args[0].pixelSize == dcg_params["atlas_pixel_size"] |
| 209 | + assert atlas_args[0].cassetteSlot == dcg_params["sample"] |
| 210 | + |
| 211 | + # Check the data collection group atlas was updated |
| 212 | + new_dcg = murfey_db_session.exec( |
| 213 | + select(DataCollectionGroup).where(DataCollectionGroup.id == dcg.id) |
| 214 | + ).one() |
| 215 | + assert new_dcg.atlas == dcg_params["atlas"] |
| 216 | + assert new_dcg.sample == dcg_params["sample"] |
| 217 | + assert new_dcg.atlas_pixel_size == dcg_params["atlas_pixel_size"] |
| 218 | + assert new_dcg.tag == dcg_params["tag"] |
| 219 | + assert new_dcg.atlas_id == 5 |
| 220 | + |
| 221 | + |
| 222 | +@mock.patch("murfey.server.workflow.register_search_map_in_database") |
| 223 | +@mock.patch("murfey.server.workflow._transport_object") |
| 224 | +def test_register_dc_group_new_atlas_with_searchmaps( |
| 225 | + mock_register_search_map, mock_transport, murfey_db_session: Session |
| 226 | +): |
| 227 | + """ |
| 228 | + Test the request to update an existing data collection group |
| 229 | + by adding an atlas, using the same tag, and also update search maps |
| 230 | + """ |
| 231 | + mock_transport.feedback_queue = "mock_feedback_queue" |
| 232 | + |
| 233 | + # Make sure dcg is present with an atlas id |
| 234 | + dcg = DataCollectionGroup( |
| 235 | + id=1, |
| 236 | + session_id=ExampleVisit.murfey_session_id, |
| 237 | + tag="processing_tag", |
| 238 | + atlas_id=90, |
| 239 | + atlas_pixel_size=1e-5, |
| 240 | + sample=1, |
| 241 | + atlas="/path/to/Atlas_1.jpg", |
| 242 | + ) |
| 243 | + murfey_db_session.add(dcg) |
| 244 | + murfey_db_session.commit() |
| 245 | + |
| 246 | + # Add some search maps with the dcg tag and one with a different tag |
| 247 | + sm1 = SearchMap( |
| 248 | + id=1, |
| 249 | + session_id=ExampleVisit.murfey_session_id, |
| 250 | + tag="processing_tag", |
| 251 | + name="searchmap1", |
| 252 | + ) |
| 253 | + sm2 = SearchMap( |
| 254 | + id=2, |
| 255 | + session_id=ExampleVisit.murfey_session_id, |
| 256 | + tag="processing_tag", |
| 257 | + name="searchmap2", |
| 258 | + ) |
| 259 | + sm3 = SearchMap( |
| 260 | + id=3, |
| 261 | + session_id=ExampleVisit.murfey_session_id, |
| 262 | + tag="different_tag", |
| 263 | + name="searchmap3", |
| 264 | + ) |
| 265 | + murfey_db_session.add(sm1) |
| 266 | + murfey_db_session.add(sm2) |
| 267 | + murfey_db_session.add(sm3) |
| 268 | + murfey_db_session.commit() |
| 269 | + |
| 270 | + # Request new dcg registration with new atlas tag and sample |
| 271 | + dcg_params = { |
| 272 | + "experiment_type_id": 37, |
| 273 | + "tag": "processing_tag", |
| 274 | + "atlas": "/path/to/Atlas_2.jpg", |
| 275 | + "sample": 2, |
| 276 | + "atlas_pixel_size": 1e-4, |
| 277 | + } |
| 278 | + register_dc_group( |
| 279 | + visit_name="cm12345-6", |
| 280 | + session_id=ExampleVisit.murfey_session_id, |
| 281 | + dcg_params=dcg_params, |
| 282 | + db=murfey_db_session, |
| 283 | + ) |
| 284 | + |
| 285 | + # Check request to ispyb for updating the experiment type |
| 286 | + mock_transport.send.assert_called_once_with( |
| 287 | + "mock_feedback_queue", |
| 288 | + { |
| 289 | + "register": "atlas_update", |
| 290 | + "atlas_id": dcg.atlas_id, |
| 291 | + "atlas": dcg_params["atlas"], |
| 292 | + "sample": dcg_params["sample"], |
| 293 | + "atlas_pixel_size": dcg_params["atlas_pixel_size"], |
| 294 | + "dcgid": dcg.id, |
| 295 | + "session_id": ExampleVisit.murfey_session_id, |
| 296 | + }, |
| 297 | + ) |
| 298 | + |
| 299 | + # Check the data collection group atlas was updated |
| 300 | + new_dcg = murfey_db_session.exec( |
| 301 | + select(DataCollectionGroup).where(DataCollectionGroup.id == dcg.id) |
| 302 | + ).one() |
| 303 | + assert new_dcg.atlas == dcg_params["atlas"] |
| 304 | + assert new_dcg.sample == dcg_params["sample"] |
| 305 | + assert new_dcg.atlas_pixel_size == dcg_params["atlas_pixel_size"] |
| 306 | + assert new_dcg.tag == dcg_params["tag"] |
| 307 | + assert new_dcg.atlas_id == dcg.atlas_id |
| 308 | + |
| 309 | + # Check the search map update calls |
| 310 | + assert mock_register_search_map.call_count == 2 |
| 311 | + mock_register_search_map.assert_any_call( |
| 312 | + ExampleVisit.murfey_session_id, |
| 313 | + "searchmap1", |
| 314 | + mock.ANY, |
| 315 | + murfey_db_session, |
| 316 | + close_db=False, |
| 317 | + ) |
| 318 | + mock_register_search_map.assert_any_call( |
| 319 | + ExampleVisit.murfey_session_id, |
| 320 | + "searchmap2", |
| 321 | + mock.ANY, |
| 322 | + murfey_db_session, |
| 323 | + close_db=False, |
| 324 | + ) |
0 commit comments