Skip to content

Commit 8f298d8

Browse files
committed
Replaced use of 'start_postgres' with test-safe 'murfey_db_session' fixtures
1 parent bd06a43 commit 8f298d8

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

tests/workflows/spa/test_flush_spa_preprocess.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from murfey.util.db import DataCollectionGroup, GridSquare
66
from murfey.util.models import GridSquareParameters
77
from murfey.workflows.spa import flush_spa_preprocess
8-
from tests import murfey_db_engine
98

109

1110
@mock.patch("murfey.workflows.spa.flush_spa_preprocess._transport_object")
12-
def test_register_grid_square_update_add_locations(mock_transport, start_postgres):
11+
def test_register_grid_square_update_add_locations(
12+
mock_transport, murfey_db_session: Session
13+
):
1314
"""Test the updating of an existing grid square"""
1415
# Create a grid square to update
1516
grid_square = GridSquare(
@@ -18,9 +19,8 @@ def test_register_grid_square_update_add_locations(mock_transport, start_postgre
1819
session_id=2,
1920
tag="session_tag",
2021
)
21-
with Session(murfey_db_engine) as murfey_db:
22-
murfey_db.add(grid_square)
23-
murfey_db.commit()
22+
murfey_db_session.add(grid_square)
23+
murfey_db_session.commit()
2424

2525
# Parameters to update with
2626
new_parameters = GridSquareParameters(
@@ -32,15 +32,13 @@ def test_register_grid_square_update_add_locations(mock_transport, start_postgre
3232
)
3333

3434
# Run the registration
35-
with Session(murfey_db_engine) as murfey_db:
36-
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db)
35+
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db_session)
3736

3837
# Check this would have updated ispyb
3938
mock_transport.do_update_grid_square.assert_called_with(1, new_parameters)
4039

4140
# Confirm the database was updated
42-
with Session(murfey_db_engine) as murfey_db:
43-
grid_square_final_parameters = murfey_db.exec(select(GridSquare)).one()
41+
grid_square_final_parameters = murfey_db_session.exec(select(GridSquare)).one()
4442
assert grid_square_final_parameters.x_location == new_parameters.x_location
4543
assert grid_square_final_parameters.y_location == new_parameters.y_location
4644
assert (
@@ -52,7 +50,9 @@ def test_register_grid_square_update_add_locations(mock_transport, start_postgre
5250

5351

5452
@mock.patch("murfey.workflows.spa.flush_spa_preprocess._transport_object")
55-
def test_register_grid_square_update_add_nothing(mock_transport, start_postgres):
53+
def test_register_grid_square_update_add_nothing(
54+
mock_transport, murfey_db_session: Session
55+
):
5656
"""Test the updating of an existing grid square, but with nothing to update with"""
5757
# Create a grid square to update
5858
grid_square = GridSquare(
@@ -65,23 +65,20 @@ def test_register_grid_square_update_add_nothing(mock_transport, start_postgres)
6565
x_stage_position=0.3,
6666
y_stage_position=0.4,
6767
)
68-
with Session(murfey_db_engine) as murfey_db:
69-
murfey_db.add(grid_square)
70-
murfey_db.commit()
68+
murfey_db_session.add(grid_square)
69+
murfey_db_session.commit()
7170

7271
# Parameters to update with
7372
new_parameters = GridSquareParameters(tag="session_tag")
7473

7574
# Run the registration
76-
with Session(murfey_db_engine) as murfey_db:
77-
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db)
75+
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db_session)
7876

7977
# Check this would have updated ispyb
8078
mock_transport.do_update_grid_square.assert_called_with(1, new_parameters)
8179

8280
# Confirm the database was not updated
83-
with Session(murfey_db_engine) as murfey_db:
84-
grid_square_final_parameters = murfey_db.exec(select(GridSquare)).one()
81+
grid_square_final_parameters = murfey_db_session.exec(select(GridSquare)).one()
8582
assert grid_square_final_parameters.x_location == 0.1
8683
assert grid_square_final_parameters.y_location == 0.2
8784
assert grid_square_final_parameters.x_stage_position == 0.3
@@ -90,7 +87,7 @@ def test_register_grid_square_update_add_nothing(mock_transport, start_postgres)
9087

9188
@mock.patch("murfey.workflows.spa.flush_spa_preprocess._transport_object")
9289
def test_register_grid_square_insert_with_ispyb(
93-
mock_transport, start_postgres, tmp_path
90+
mock_transport, murfey_db_session, tmp_path
9491
):
9592
# Create a data collection group for lookups
9693
grid_square = DataCollectionGroup(
@@ -99,9 +96,8 @@ def test_register_grid_square_insert_with_ispyb(
9996
tag="session_tag",
10097
atlas_id=90,
10198
)
102-
with Session(murfey_db_engine) as murfey_db:
103-
murfey_db.add(grid_square)
104-
murfey_db.commit()
99+
murfey_db_session.add(grid_square)
100+
murfey_db_session.commit()
105101

106102
# Set the ispyb return
107103
mock_transport.do_insert_grid_square.return_value = {
@@ -126,15 +122,13 @@ def test_register_grid_square_insert_with_ispyb(
126122
)
127123

128124
# Run the registration
129-
with Session(murfey_db_engine) as murfey_db:
130-
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db)
125+
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db_session)
131126

132127
# Check this would have updated ispyb
133128
mock_transport.do_insert_grid_square.assert_called_with(90, 101, new_parameters)
134129

135130
# Confirm the database entry was made
136-
with Session(murfey_db_engine) as murfey_db:
137-
grid_square_final_parameters = murfey_db.exec(select(GridSquare)).one()
131+
grid_square_final_parameters = murfey_db_session.exec(select(GridSquare)).one()
138132
assert grid_square_final_parameters.id == 1
139133
assert grid_square_final_parameters.name == 101
140134
assert grid_square_final_parameters.session_id == 2

0 commit comments

Comments
 (0)