|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +from sqlmodel import Session, select |
| 4 | + |
| 5 | +from murfey.util.db import DataCollectionGroup, GridSquare |
| 6 | +from murfey.util.models import GridSquareParameters |
| 7 | +from murfey.workflows.spa import flush_spa_preprocess |
| 8 | +from tests import engine |
| 9 | + |
| 10 | + |
| 11 | +@mock.patch("murfey.workflows.spa.flush_spa_preprocess._transport_object") |
| 12 | +def test_register_grid_square_update_add_locations(mock_transport, start_postgres): |
| 13 | + """Test the updating of an existing grid square""" |
| 14 | + # Create a grid square to update |
| 15 | + grid_square = GridSquare( |
| 16 | + id=1, |
| 17 | + name=101, |
| 18 | + session_id=2, |
| 19 | + tag="session_tag", |
| 20 | + ) |
| 21 | + with Session(engine) as murfey_db: |
| 22 | + murfey_db.add(grid_square) |
| 23 | + murfey_db.commit() |
| 24 | + |
| 25 | + # Parameters to update with |
| 26 | + new_parameters = GridSquareParameters( |
| 27 | + tag="session_tag", |
| 28 | + x_location=1.1, |
| 29 | + y_location=1.2, |
| 30 | + x_stage_position=1.3, |
| 31 | + y_stage_position=1.4, |
| 32 | + ) |
| 33 | + |
| 34 | + # Run the registration |
| 35 | + with Session(engine) as murfey_db: |
| 36 | + flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db) |
| 37 | + |
| 38 | + # Check this would have updated ispyb |
| 39 | + mock_transport.do_update_grid_square.assert_called_with(1, new_parameters) |
| 40 | + |
| 41 | + # Confirm the database was updated |
| 42 | + with Session(engine) as murfey_db: |
| 43 | + grid_square_final_parameters = murfey_db.exec(select(GridSquare)).one() |
| 44 | + assert grid_square_final_parameters.x_location == new_parameters.x_location |
| 45 | + assert grid_square_final_parameters.y_location == new_parameters.y_location |
| 46 | + assert ( |
| 47 | + grid_square_final_parameters.x_stage_position == new_parameters.x_stage_position |
| 48 | + ) |
| 49 | + assert ( |
| 50 | + grid_square_final_parameters.y_stage_position == new_parameters.y_stage_position |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@mock.patch("murfey.workflows.spa.flush_spa_preprocess._transport_object") |
| 55 | +def test_register_grid_square_update_add_nothing(mock_transport, start_postgres): |
| 56 | + """Test the updating of an existing grid square, but with nothing to update with""" |
| 57 | + # Create a grid square to update |
| 58 | + grid_square = GridSquare( |
| 59 | + id=1, |
| 60 | + name=101, |
| 61 | + session_id=2, |
| 62 | + tag="session_tag", |
| 63 | + x_location=0.1, |
| 64 | + y_location=0.2, |
| 65 | + x_stage_position=0.3, |
| 66 | + y_stage_position=0.4, |
| 67 | + ) |
| 68 | + with Session(engine) as murfey_db: |
| 69 | + murfey_db.add(grid_square) |
| 70 | + murfey_db.commit() |
| 71 | + |
| 72 | + # Parameters to update with |
| 73 | + new_parameters = GridSquareParameters(tag="session_tag") |
| 74 | + |
| 75 | + # Run the registration |
| 76 | + with Session(engine) as murfey_db: |
| 77 | + flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db) |
| 78 | + |
| 79 | + # Check this would have updated ispyb |
| 80 | + mock_transport.do_update_grid_square.assert_called_with(1, new_parameters) |
| 81 | + |
| 82 | + # Confirm the database was not updated |
| 83 | + with Session(engine) as murfey_db: |
| 84 | + grid_square_final_parameters = murfey_db.exec(select(GridSquare)).one() |
| 85 | + assert grid_square_final_parameters.x_location == 0.1 |
| 86 | + assert grid_square_final_parameters.y_location == 0.2 |
| 87 | + assert grid_square_final_parameters.x_stage_position == 0.3 |
| 88 | + assert grid_square_final_parameters.y_stage_position == 0.4 |
| 89 | + |
| 90 | + |
| 91 | +@mock.patch("murfey.workflows.spa.flush_spa_preprocess._transport_object") |
| 92 | +def test_register_grid_square_insert_with_ispyb( |
| 93 | + mock_transport, start_postgres, tmp_path |
| 94 | +): |
| 95 | + # Create a data collection group for lookups |
| 96 | + grid_square = DataCollectionGroup( |
| 97 | + id=1, |
| 98 | + session_id=2, |
| 99 | + tag="session_tag", |
| 100 | + atlas_id=90, |
| 101 | + ) |
| 102 | + with Session(engine) as murfey_db: |
| 103 | + murfey_db.add(grid_square) |
| 104 | + murfey_db.commit() |
| 105 | + |
| 106 | + # Set the ispyb return |
| 107 | + mock_transport.do_insert_grid_square.return_value = { |
| 108 | + "return_value": 1, |
| 109 | + "success": True, |
| 110 | + } |
| 111 | + |
| 112 | + # Parameters to update with |
| 113 | + new_parameters = GridSquareParameters( |
| 114 | + tag="session_tag", |
| 115 | + x_location=1.1, |
| 116 | + y_location=1.2, |
| 117 | + x_stage_position=1.3, |
| 118 | + y_stage_position=1.4, |
| 119 | + readout_area_x=2048, |
| 120 | + readout_area_y=1024, |
| 121 | + thumbnail_size_x=512, |
| 122 | + thumbnail_size_y=256, |
| 123 | + pixel_size=1.02, |
| 124 | + image=f"{tmp_path}/image_path", |
| 125 | + angle=12.5, |
| 126 | + ) |
| 127 | + |
| 128 | + # Run the registration |
| 129 | + with Session(engine) as murfey_db: |
| 130 | + flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db) |
| 131 | + |
| 132 | + # Check this would have updated ispyb |
| 133 | + mock_transport.do_insert_grid_square.assert_called_with(90, 101, new_parameters) |
| 134 | + |
| 135 | + # Confirm the database entry was made |
| 136 | + with Session(engine) as murfey_db: |
| 137 | + grid_square_final_parameters = murfey_db.exec(select(GridSquare)).one() |
| 138 | + assert grid_square_final_parameters.id == 1 |
| 139 | + assert grid_square_final_parameters.name == 101 |
| 140 | + assert grid_square_final_parameters.session_id == 2 |
| 141 | + assert grid_square_final_parameters.tag == "session_tag" |
| 142 | + assert grid_square_final_parameters.x_location == 1.1 |
| 143 | + assert grid_square_final_parameters.y_location == 1.2 |
| 144 | + assert grid_square_final_parameters.x_stage_position == 1.3 |
| 145 | + assert grid_square_final_parameters.y_stage_position == 1.4 |
| 146 | + assert grid_square_final_parameters.readout_area_x == 2048 |
| 147 | + assert grid_square_final_parameters.readout_area_y == 1024 |
| 148 | + assert grid_square_final_parameters.thumbnail_size_x == 512 |
| 149 | + assert grid_square_final_parameters.thumbnail_size_y == 256 |
| 150 | + assert grid_square_final_parameters.pixel_size == 1.02 |
| 151 | + assert grid_square_final_parameters.image == f"{tmp_path}/image_path" |
0 commit comments