Skip to content

Commit 5825457

Browse files
committed
Added a class to 'conftest.py' storing the database parameters needed for an example visit; populated the ISPyB database in the fixture instead of in the test
1 parent 7282ebf commit 5825457

File tree

1 file changed

+73
-18
lines changed

1 file changed

+73
-18
lines changed

tests/conftest.py

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import json
22
from configparser import ConfigParser
33
from pathlib import Path
4+
from typing import Generator
45

56
import ispyb
67
import pytest
7-
from ispyb.sqlalchemy import url
8+
from ispyb.sqlalchemy import BLSession, Person, Proposal, url
89
from sqlalchemy import create_engine
910
from sqlalchemy.orm import scoped_session, sessionmaker
1011
from sqlmodel import Session
@@ -22,17 +23,6 @@ def session_tmp_path(tmp_path_factory) -> Path:
2223
return tmp_path_factory.mktemp("session_tmp")
2324

2425

25-
@pytest.fixture
26-
def start_postgres():
27-
clear(murfey_db_url)
28-
setup(murfey_db_url)
29-
30-
murfey_session = MurfeySession(id=2, name="cm12345-6")
31-
with Session(murfey_db_engine) as murfey_db:
32-
murfey_db.add(murfey_session)
33-
murfey_db.commit()
34-
35-
3626
@pytest.fixture(scope="session")
3727
def mock_client_configuration() -> ConfigParser:
3828
"""
@@ -93,8 +83,27 @@ def mock_security_configuration(
9383
"""
9484

9585

86+
class ExampleVisit:
87+
"""
88+
This is a class to store information that will common to all database entries for
89+
a particular Murfey session, to enable ease of replication when creating database
90+
fixtures.
91+
"""
92+
93+
# Visit-related (ISPyB & Murfey)
94+
instrument_name = "i01"
95+
proposal_code = "cm"
96+
proposal_number = 12345
97+
visit_number = 6
98+
99+
# Person (ISPyB)
100+
given_name = "Eliza"
101+
family_name = "Murfey"
102+
login = "murfey123"
103+
104+
96105
@pytest.fixture(scope="session")
97-
def ispyb_db(mock_ispyb_credentials):
106+
def ispyb_db_connection(mock_ispyb_credentials):
98107
with ispyb.open(mock_ispyb_credentials) as connection:
99108
yield connection
100109

@@ -114,8 +123,54 @@ def ispyb_session_factory(ispyb_engine):
114123

115124

116125
@pytest.fixture
117-
def ispyb_session(ispyb_session_factory):
118-
ispyb_session = ispyb_session_factory()
119-
yield ispyb_session
120-
ispyb_session.rollback()
121-
ispyb_session.close()
126+
def ispyb_db(ispyb_session_factory) -> Generator[Session, None, None]:
127+
# Get a new session from the session factory
128+
ispyb_db: Session = ispyb_session_factory()
129+
130+
# Populate the ISPyB table with some default values
131+
person_db_entry = Person(
132+
givenName=ExampleVisit.given_name,
133+
familyName=ExampleVisit.family_name,
134+
login=ExampleVisit.login,
135+
)
136+
proposal_db_entry = Proposal(
137+
personId=person_db_entry.personId,
138+
proposalCode=ExampleVisit.proposal_code,
139+
proposalNumber=str(ExampleVisit.proposal_number),
140+
)
141+
bl_session_db_entry = BLSession(
142+
proposalId=proposal_db_entry.proposalId,
143+
beamLineName=ExampleVisit.instrument_name,
144+
visit_number=ExampleVisit.visit_number,
145+
)
146+
ispyb_db.add_all(
147+
[
148+
person_db_entry,
149+
proposal_db_entry,
150+
bl_session_db_entry,
151+
]
152+
)
153+
ispyb_db.commit()
154+
yield ispyb_db # Yield the Session and pass processing over to other function
155+
156+
# Tidying up
157+
ispyb_db.rollback()
158+
ispyb_db.close()
159+
160+
161+
"""
162+
=======================================================================================
163+
Fixtures for setting up mock Murfey database
164+
=======================================================================================
165+
"""
166+
167+
168+
@pytest.fixture
169+
def start_postgres():
170+
clear(murfey_db_url)
171+
setup(murfey_db_url)
172+
173+
murfey_session = MurfeySession(id=2, name="cm12345-6")
174+
with Session(murfey_db_engine) as murfey_db:
175+
murfey_db.add(murfey_session)
176+
murfey_db.commit()

0 commit comments

Comments
 (0)