-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
364 lines (305 loc) · 11.3 KB
/
Copy pathconftest.py
File metadata and controls
364 lines (305 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from __future__ import annotations
import json
import os
from configparser import ConfigParser
from pathlib import Path
from typing import Any, Generator, Type, TypeVar
import ispyb
import pytest
from ispyb.sqlalchemy import BLSession, ExperimentType, Person, Proposal, url
from sqlalchemy import Engine, RootTransaction, and_, create_engine, event, select
from sqlalchemy.exc import InterfaceError
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.orm import Session as SQLAlchemySession, sessionmaker
from sqlmodel import Session as SQLModelSession, SQLModel
from murfey.util.db import Session as MurfeySession
@pytest.fixture(scope="session")
def session_tmp_path(tmp_path_factory) -> Path:
"""
Creates a temporary path that persists for the entire test session
"""
return tmp_path_factory.mktemp("session_tmp")
@pytest.fixture(scope="session")
def mock_client_configuration() -> ConfigParser:
"""
Returns the client-side configuration file as a pre-loaded ConfigParser object.
"""
config = ConfigParser()
config["Murfey"] = {
"instrument_name": "murfey",
"server": "http://0.0.0.0:8000",
"token": "pneumonoultramicroscopicsilicovolcanoconiosis",
}
return config
@pytest.fixture(scope="session")
def mock_ispyb_credentials(session_tmp_path: Path) -> Path:
creds_file = session_tmp_path / "ispyb_creds.cfg"
ispyb_config = ConfigParser()
# Use values from the GitHub workflow ISPyB config file
ispyb_config["ispyb_sqlalchemy"] = {
"username": "ispyb_api_sqlalchemy",
"password": "password_5678",
"host": "localhost",
"port": "3306",
"database": "ispybtest",
}
with open(creds_file, "w") as file:
ispyb_config.write(file)
return creds_file
@pytest.fixture(scope="session")
def mock_security_configuration(
session_tmp_path: Path,
mock_ispyb_credentials: Path,
) -> Path:
config_file = session_tmp_path / "security_config.yaml"
security_config = {
"murfey_db_credentials": "/path/to/murfey_db_credentials",
"crypto_key": "crypto_key",
"auth_key": "auth_key",
"auth_algorithm": "auth_algorithm",
"rabbitmq_credentials": "/path/to/rabbitmq.yaml",
"feedback_queue": "murfey_feedback",
"ispyb_credentials": str(mock_ispyb_credentials),
}
with open(config_file, "w") as f:
json.dump(security_config, f)
return config_file
"""
=======================================================================================
Database-related helper functions and classes
=======================================================================================
"""
class ExampleVisit:
"""
This class stores information that will be common to all database entries for a
particular Murfey session, to enable ease of replication when creating database
fixtures.
"""
# Visit-related (ISPyB & Murfey)
instrument_name = "i01"
proposal_code = "cm"
proposal_number = 12345
visit_number = 6
# Murfey-specific
murfey_session_id = 1
# Person (ISPyB)
given_name = "Eliza"
family_name = "Murfey"
login = "murfey123"
class ISPyBTableValues:
"""
Visit-independent default values for ISPyB tables
"""
# ExperimentType (ISPyB)
experiment_types = {
"Tomography": 36,
"Single Particle": 37,
"Atlas": 44,
"CLEM": 45,
"FIB": 46,
}
SQLAlchemyTable = TypeVar("SQLAlchemyTable", bound=DeclarativeMeta)
def get_or_create_db_entry(
session: SQLAlchemySession | SQLModelSession,
table: Type[SQLAlchemyTable],
lookup_kwargs: dict[str, Any] = {},
insert_kwargs: dict[str, Any] = {},
) -> SQLAlchemyTable:
"""
Helper function to facilitate looking up or creating SQLAlchemy table entries.
Returns the entry if a match based on the lookup criteria is found, otherwise
creates and returns a new entry.
"""
# if lookup kwargs are provided, check if entry exists
if lookup_kwargs:
conditions = [
getattr(table, key) == value for key, value in lookup_kwargs.items()
]
# Use 'exec()' for SQLModel sessions
if isinstance(session, SQLModelSession):
entry = session.exec(select(table).where(and_(*conditions))).first()
# Use 'execute()' for SQLAlchemy sessions
elif isinstance(session, SQLAlchemySession):
entry = (
session.execute(select(table).where(and_(*conditions)))
.scalars()
.first()
)
else:
raise TypeError("Unexpected Session type")
if entry:
return entry
# If not present, create and return new entry
# Use new kwargs if provided; otherwise, use lookup kwargs
insert_kwargs = insert_kwargs or lookup_kwargs
entry = table(**insert_kwargs)
session.add(entry)
session.commit()
return entry
def restart_savepoint(
session: SQLAlchemySession | SQLModelSession, transaction: RootTransaction
):
"""
Re-establish a SAVEPOINT after a nested transaction is committed or rolled back.
This helps to maintain isolation across different test cases.
"""
if transaction.nested and not transaction._parent.nested:
session.begin_nested()
"""
=======================================================================================
Fixtures for setting up test ISPyB database
=======================================================================================
These were adapted from the tests found at:
https://github.com/DiamondLightSource/ispyb-api/blob/main/tests/conftest.py
"""
@pytest.fixture(scope="session")
def ispyb_db_connection(mock_ispyb_credentials):
with ispyb.open(mock_ispyb_credentials) as connection:
yield connection
@pytest.fixture(scope="session")
def ispyb_engine(mock_ispyb_credentials):
engine = create_engine(
url=url(mock_ispyb_credentials), connect_args={"use_pure": True}
)
yield engine
engine.dispose()
@pytest.fixture(scope="session")
def ispyb_db_session_factory(ispyb_engine):
return sessionmaker(bind=ispyb_engine, expire_on_commit=False)
@pytest.fixture(scope="session")
def seed_ispyb_db(ispyb_db_session_factory):
try:
# Populate the ISPyB table with some initial values
# Return existing table entry if already present
ispyb_db_session: SQLAlchemySession = ispyb_db_session_factory()
person_db_entry = get_or_create_db_entry(
session=ispyb_db_session,
table=Person,
lookup_kwargs={
"givenName": ExampleVisit.given_name,
"familyName": ExampleVisit.family_name,
"login": ExampleVisit.login,
},
)
proposal_db_entry = get_or_create_db_entry(
session=ispyb_db_session,
table=Proposal,
lookup_kwargs={
"personId": person_db_entry.personId,
"proposalCode": ExampleVisit.proposal_code,
"proposalNumber": str(ExampleVisit.proposal_number),
},
)
_ = get_or_create_db_entry(
session=ispyb_db_session,
table=BLSession,
lookup_kwargs={
"proposalId": proposal_db_entry.proposalId,
"beamLineName": ExampleVisit.instrument_name,
"visit_number": ExampleVisit.visit_number,
},
)
_ = [
get_or_create_db_entry(
session=ispyb_db_session,
table=ExperimentType,
lookup_kwargs={
"experimentTypeId": id,
"name": name,
"proposalType": "em",
"active": 1,
},
)
for name, id in ISPyBTableValues.experiment_types.items()
]
ispyb_db_session.close()
except InterfaceError:
# If this fails in the GitHub test environment, raise it as a genuine error
if os.getenv("GITHUB_ACTIONS") == "true":
raise InterfaceError
pytest.skip("ISPyB database has not been set up; skipping test")
@pytest.fixture
def ispyb_db_session(
ispyb_db_session_factory,
ispyb_engine: Engine,
seed_ispyb_db,
) -> Generator[SQLAlchemySession, None, None]:
"""
Returns a test-safe session that wraps each test in a rollback-safe save point.
"""
connection = ispyb_engine.connect()
transaction = connection.begin() # Outer transaction
session: SQLAlchemySession = ispyb_db_session_factory(bind=connection)
session.begin_nested() # Save point for test
# Trigger the restart_savepoint function after the end of the transaction
event.listen(session, "after_transaction_end", restart_savepoint)
try:
yield session
finally:
session.close()
transaction.rollback()
connection.close()
"""
=======================================================================================
Fixtures for setting up test Murfey database
=======================================================================================
"""
@pytest.fixture(scope="session")
def murfey_db_url():
try:
return (
f"postgresql+psycopg2://{os.environ['POSTGRES_USER']}:{os.environ['POSTGRES_PASSWORD']}"
f"@{os.environ['POSTGRES_HOST']}:{os.environ['POSTGRES_PORT']}/{os.environ['POSTGRES_DB']}"
)
# Skip Murfey database-related tests if the environment for it hasn't been set up
except KeyError:
# If this fails in the GitHub test environment, raise it as a genuine error
if os.getenv("GITHUB_ACTIONS") == "true":
raise KeyError
pytest.skip("Murfey PostgreSQL database has not been set up; skipping test")
return ""
@pytest.fixture(scope="session")
def murfey_db_engine(murfey_db_url):
engine = create_engine(murfey_db_url)
SQLModel.metadata.create_all(engine)
yield engine
engine.dispose()
@pytest.fixture(scope="session")
def murfey_db_session_factory(murfey_db_engine):
return sessionmaker(
bind=murfey_db_engine, expire_on_commit=False, class_=SQLModelSession
)
@pytest.fixture(scope="session")
def seed_murfey_db(murfey_db_session_factory):
# Populate Murfey database with initial values
session: SQLModelSession = murfey_db_session_factory()
_ = get_or_create_db_entry(
session=session,
table=MurfeySession,
lookup_kwargs={
"id": ExampleVisit.murfey_session_id,
"name": f"{ExampleVisit.proposal_code}{ExampleVisit.proposal_number}-{ExampleVisit.visit_number}",
},
)
session.close()
@pytest.fixture
def murfey_db_session(
murfey_db_session_factory,
murfey_db_engine: Engine,
seed_murfey_db,
) -> Generator[SQLModelSession, None, None]:
"""
Returns a test-safe session that wraps each test in a rollback-safe save point
"""
connection = murfey_db_engine.connect()
transaction = connection.begin()
session: SQLModelSession = murfey_db_session_factory(bind=connection)
session.begin_nested() # Save point for test
# Trigger the restart_savepoint function after the end of the transaction
event.listen(session, "after_transaction_end", restart_savepoint)
try:
yield session
finally:
session.close()
transaction.rollback()
connection.close()