Skip to content

Commit ee04386

Browse files
committed
Rearranged functions and removed 'attach_event_listener()' as a function
1 parent 6b6575c commit ee04386

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

tests/conftest.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ def mock_security_configuration(
7777

7878
"""
7979
=======================================================================================
80-
Fixtures for setting up mock ISPyB database
80+
Database-related helper functions and classes
8181
=======================================================================================
82-
These were adapted from the tests found at:
83-
https://github.com/DiamondLightSource/ispyb-api/blob/main/tests/conftest.py
8482
"""
8583

8684

@@ -103,26 +101,6 @@ class ExampleVisit:
103101
login = "murfey123"
104102

105103

106-
@pytest.fixture(scope="session")
107-
def ispyb_db_connection(mock_ispyb_credentials):
108-
with ispyb.open(mock_ispyb_credentials) as connection:
109-
yield connection
110-
111-
112-
@pytest.fixture(scope="session")
113-
def ispyb_engine(mock_ispyb_credentials):
114-
ispyb_engine = create_engine(
115-
url=url(mock_ispyb_credentials), connect_args={"use_pure": True}
116-
)
117-
yield ispyb_engine
118-
ispyb_engine.dispose()
119-
120-
121-
@pytest.fixture(scope="session")
122-
def ispyb_db_session_factory(ispyb_engine):
123-
return sessionmaker(bind=ispyb_engine, expire_on_commit=False)
124-
125-
126104
SQLAlchemyTable = TypeVar("SQLAlchemyTable", bound=DeclarativeMeta)
127105

128106

@@ -158,6 +136,44 @@ def get_or_create_db_entry(
158136
return entry
159137

160138

139+
def restart_savepoint(session: SQLAlchemySession, transaction: RootTransaction):
140+
"""
141+
Re-establish a SAVEPOINT after a nested transaction is committed or rolled back.
142+
This helps to maintain isolation across different test cases.
143+
"""
144+
if transaction.nested and not transaction._parent.nested:
145+
session.begin_nested()
146+
147+
148+
"""
149+
=======================================================================================
150+
Fixtures for setting up test ISPyB database
151+
=======================================================================================
152+
These were adapted from the tests found at:
153+
https://github.com/DiamondLightSource/ispyb-api/blob/main/tests/conftest.py
154+
"""
155+
156+
157+
@pytest.fixture(scope="session")
158+
def ispyb_db_connection(mock_ispyb_credentials):
159+
with ispyb.open(mock_ispyb_credentials) as connection:
160+
yield connection
161+
162+
163+
@pytest.fixture(scope="session")
164+
def ispyb_engine(mock_ispyb_credentials):
165+
engine = create_engine(
166+
url=url(mock_ispyb_credentials), connect_args={"use_pure": True}
167+
)
168+
yield engine
169+
engine.dispose()
170+
171+
172+
@pytest.fixture(scope="session")
173+
def ispyb_db_session_factory(ispyb_engine):
174+
return sessionmaker(bind=ispyb_engine, expire_on_commit=False)
175+
176+
161177
@pytest.fixture(scope="session")
162178
def seed_ispyb_db(ispyb_db_session_factory):
163179

@@ -194,39 +210,23 @@ def seed_ispyb_db(ispyb_db_session_factory):
194210
ispyb_db_session.close()
195211

196212

197-
def restart_savepoint(session: SQLAlchemySession, transaction: RootTransaction):
198-
"""
199-
Re-establish a SAVEPOINT after a nested transaction is committed or rolled back.
200-
This helps to maintain isolation across different test cases.
201-
"""
202-
if transaction.nested and not transaction._parent.nested:
203-
session.begin_nested()
204-
205-
206-
def attach_event_listener(session: SQLAlchemySession):
207-
"""
208-
Attach the restart_savepoint function as an event listener for after_transaction_end
209-
"""
210-
event.listen(session, "after_transaction_end", restart_savepoint)
211-
212-
213213
@pytest.fixture
214214
def ispyb_db_session(
215215
ispyb_db_session_factory,
216216
ispyb_engine: Engine,
217217
seed_ispyb_db,
218218
) -> Generator[SQLAlchemySession, None, None]:
219219
"""
220-
Returns a test-safe session that wraps each test in a rollback-safe SAVEPOINT.
220+
Returns a test-safe session that wraps each test in a rollback-safe save point.
221221
"""
222222
connection = ispyb_engine.connect()
223223
transaction = connection.begin() # Outer transaction
224224

225225
session: SQLAlchemySession = ispyb_db_session_factory(bind=connection)
226226
session.begin_nested() # Save point for test
227227

228-
# Attach the listener to the session for this connection
229-
attach_event_listener(session)
228+
# Trigger the restart_savepoint function after the end of the transaction
229+
event.listen(session, "after_transaction_end", restart_savepoint)
230230

231231
try:
232232
yield session
@@ -238,7 +238,7 @@ def ispyb_db_session(
238238

239239
"""
240240
=======================================================================================
241-
Fixtures for setting up mock Murfey database
241+
Fixtures for setting up test Murfey database
242242
=======================================================================================
243243
"""
244244

0 commit comments

Comments
 (0)