Skip to content

Commit 14d1cf2

Browse files
committed
Tests for registering grid squares
1 parent 5742636 commit 14d1cf2

File tree

1 file changed

+166
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)