|
| 1 | +from unittest.mock import patch |
| 2 | +from urllib.parse import urlparse |
| 3 | + |
| 4 | +from murfey.client.contexts.sxt import SXTContext |
| 5 | +from murfey.client.instance_environment import MurfeyInstanceEnvironment |
| 6 | + |
| 7 | + |
| 8 | +def test_sxt_context_initialisation(tmp_path): |
| 9 | + context = SXTContext("zeiss", tmp_path, "") |
| 10 | + assert context._acquisition_software == "zeiss" |
| 11 | + assert context._basepath == tmp_path |
| 12 | + |
| 13 | + |
| 14 | +@patch("requests.post") |
| 15 | +def test_sxt_context_xrm(mock_post, tmp_path): |
| 16 | + """Currently nothing happens with an xrm file""" |
| 17 | + env = MurfeyInstanceEnvironment( |
| 18 | + url=urlparse("http://localhost:8000"), |
| 19 | + client_id=0, |
| 20 | + sources=[tmp_path], |
| 21 | + default_destinations={tmp_path: str(tmp_path)}, |
| 22 | + instrument_name="", |
| 23 | + visit="test", |
| 24 | + murfey_session=1, |
| 25 | + ) |
| 26 | + context = SXTContext("zeiss", tmp_path, "") |
| 27 | + return_value = context.post_transfer( |
| 28 | + tmp_path / "example.xrm", |
| 29 | + required_position_files=[], |
| 30 | + required_strings=["fractions"], |
| 31 | + environment=env, |
| 32 | + ) |
| 33 | + assert return_value |
| 34 | + mock_post.assert_not_called() |
| 35 | + |
| 36 | + |
| 37 | +@patch("requests.post") |
| 38 | +@patch("murfey.client.contexts.sxt.Inspector") |
| 39 | +@patch("murfey.client.contexts.sxt.open_txrm") |
| 40 | +@patch("murfey.client.contexts.sxt.read_stream") |
| 41 | +def test_sxt_context_txrm( |
| 42 | + mock_read_stream, mock_open_txrm, mock_inspector, mock_post, tmp_path |
| 43 | +): |
| 44 | + mock_post().status_code = 200 |
| 45 | + mock_read_stream.side_effect = [ |
| 46 | + [-55, -25, 5, 35, 65], # Angles |
| 47 | + [0.01001], # Pixel size |
| 48 | + [1024], # Image Width |
| 49 | + [2048], # Image Height |
| 50 | + [1.5], # Exposure time |
| 51 | + [1000], # Mag |
| 52 | + [5], # Image count |
| 53 | + ] |
| 54 | + |
| 55 | + env = MurfeyInstanceEnvironment( |
| 56 | + url=urlparse("http://localhost:8000"), |
| 57 | + client_id=0, |
| 58 | + sources=[tmp_path], |
| 59 | + default_destinations={tmp_path: str(tmp_path / "destination")}, |
| 60 | + instrument_name="", |
| 61 | + visit="test", |
| 62 | + murfey_session=1, |
| 63 | + ) |
| 64 | + context = SXTContext("zeiss", tmp_path, "") |
| 65 | + context.post_transfer( |
| 66 | + tmp_path / "example.txrm", |
| 67 | + required_position_files=[], |
| 68 | + required_strings=["fractions"], |
| 69 | + environment=env, |
| 70 | + ) |
| 71 | + |
| 72 | + mock_open_txrm.assert_called_once_with( |
| 73 | + tmp_path / "example.txrm", load_images=False, load_reference=False, strict=False |
| 74 | + ) |
| 75 | + mock_inspector.assert_called_once() |
| 76 | + |
| 77 | + assert mock_post.call_count == 5 |
| 78 | + mock_post.assert_any_call( |
| 79 | + "http://localhost:8000/workflow/visits/test/sessions/1/register_data_collection_group", |
| 80 | + json={ |
| 81 | + "experiment_type_id": 47, |
| 82 | + "tag": str(tmp_path), |
| 83 | + }, |
| 84 | + headers={"Authorization": "Bearer "}, |
| 85 | + ) |
| 86 | + mock_post.assert_any_call( |
| 87 | + "http://localhost:8000/workflow/visits/test/sessions/1/start_data_collection", |
| 88 | + json={ |
| 89 | + "experiment_type": "sxt", |
| 90 | + "file_extension": ".txrm", |
| 91 | + "acquisition_software": "zeiss", |
| 92 | + "image_directory": f"{tmp_path}/destination", |
| 93 | + "data_collection_tag": "example", |
| 94 | + "source": str(tmp_path), |
| 95 | + "tag": "example", |
| 96 | + "pixel_size_on_image": "100.1", |
| 97 | + "image_size_x": 1024, |
| 98 | + "image_size_y": 2048, |
| 99 | + "magnification": 1000, |
| 100 | + "voltage": 0, |
| 101 | + }, |
| 102 | + headers={"Authorization": "Bearer "}, |
| 103 | + ) |
| 104 | + mock_post.assert_any_call( |
| 105 | + "http://localhost:8000/workflow/visits/test/sessions/1/register_processing_job", |
| 106 | + json={ |
| 107 | + "tag": "example", |
| 108 | + "source": str(tmp_path), |
| 109 | + "recipe": "sxt-tomo-align", |
| 110 | + "experiment_type": "sxt", |
| 111 | + }, |
| 112 | + headers={"Authorization": "Bearer "}, |
| 113 | + ) |
| 114 | + mock_post.assert_any_call( |
| 115 | + "http://localhost:8000/workflow/sxt/visits/test/sessions/1/sxt_tilt_series", |
| 116 | + json={ |
| 117 | + "session_id": 1, |
| 118 | + "tag": "example", |
| 119 | + "source": str(tmp_path), |
| 120 | + "pixel_size": 100.1, |
| 121 | + "tilt_offset": 5, |
| 122 | + "txrm": str(tmp_path / "destination/example.txrm"), |
| 123 | + }, |
| 124 | + headers={"Authorization": "Bearer "}, |
| 125 | + ) |
0 commit comments