Skip to content

Commit a475490

Browse files
committed
Updated fixture name from 'ispyb_db' to 'ispyb_db_session'
1 parent af829e7 commit a475490

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

tests/conftest.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ def get_or_create_db_entry(
128128
insert_kwargs: dict[str, Any] = {},
129129
) -> SQLAlchemyTable:
130130
"""
131-
Helper function to facilitate looking up SQLAlchemy tables for
132-
matching entries. Returns the entry if it exists and creates it
133-
if it doesn't.
131+
Helper function to facilitate looking up or creating SQLAlchemy table entries.
132+
Returns the entry if a match based on the lookup criteria is found, otherwise
133+
creates and returns a new entry.
134134
"""
135135

136136
# if lookup kwargs are provided, check if entry exists
@@ -143,6 +143,7 @@ def get_or_create_db_entry(
143143
)
144144
if entry:
145145
return entry
146+
146147
# If not present, create and return new entry
147148
# Use new kwargs if provided; otherwise, use lookup kwargs
148149
insert_kwargs = insert_kwargs or lookup_kwargs
@@ -153,14 +154,14 @@ def get_or_create_db_entry(
153154

154155

155156
@pytest.fixture(scope="session")
156-
def ispyb_session_factory(ispyb_engine):
157+
def ispyb_db_session_factory(ispyb_engine):
157158
factory = scoped_session(sessionmaker(bind=ispyb_engine))
158-
ispyb_db = factory()
159159

160160
# Populate the ISPyB table with some initial values
161161
# Return existing table entry if already present
162+
ispyb_db_session = factory()
162163
person_db_entry = get_or_create_db_entry(
163-
session=ispyb_db,
164+
session=ispyb_db_session,
164165
table=Person,
165166
lookup_kwargs={
166167
"givenName": ExampleVisit.given_name,
@@ -169,37 +170,41 @@ def ispyb_session_factory(ispyb_engine):
169170
},
170171
)
171172
proposal_db_entry = get_or_create_db_entry(
172-
session=ispyb_db,
173+
session=ispyb_db_session,
173174
table=Proposal,
174175
lookup_kwargs={
175176
"personId": person_db_entry.personId,
176177
"proposalCode": ExampleVisit.proposal_code,
177178
"proposalNumber": str(ExampleVisit.proposal_number),
178179
},
179180
)
180-
bl_session_db_entry = get_or_create_db_entry(
181-
session=ispyb_db,
181+
_ = get_or_create_db_entry(
182+
session=ispyb_db_session,
182183
table=BLSession,
183184
lookup_kwargs={
184185
"proposalId": proposal_db_entry.proposalId,
185186
"beamLineName": ExampleVisit.instrument_name,
186187
"visit_number": ExampleVisit.visit_number,
187188
},
188189
)
189-
ispyb_db.add(bl_session_db_entry)
190-
ispyb_db.commit()
191-
192-
ispyb_db.close()
190+
ispyb_db_session.close()
193191
return factory # Return its current state
194192

195193

196194
@pytest.fixture
197-
def ispyb_db(ispyb_session_factory) -> Generator[SQLAlchemySession, None, None]:
195+
def ispyb_db_session(
196+
ispyb_db_session_factory,
197+
) -> Generator[SQLAlchemySession, None, None]:
198+
198199
# Get a new session from the session factory
199-
ispyb_db: SQLAlchemySession = ispyb_session_factory()
200-
yield ispyb_db
201-
ispyb_db.rollback()
202-
ispyb_db.close()
200+
ispyb_db_session: SQLAlchemySession = ispyb_db_session_factory()
201+
202+
# Let other function run
203+
yield ispyb_db_session
204+
205+
# Tidy up after function is complete
206+
ispyb_db_session.rollback()
207+
ispyb_db_session.close()
203208

204209

205210
"""

tests/server/test_ispyb.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88

99
def test_get_session_id(
10-
ispyb_db: Session,
10+
ispyb_db_session: Session,
1111
):
1212
# Manually get the BLSession ID for comparison
1313
query = (
14-
ispyb_db.execute(
14+
ispyb_db_session.execute(
1515
select(BLSession)
1616
.join(Proposal)
1717
.where(BLSession.proposalId == Proposal.proposalId)
@@ -30,17 +30,17 @@ def test_get_session_id(
3030
proposal_code=ExampleVisit.proposal_code,
3131
proposal_number=str(ExampleVisit.proposal_number),
3232
visit_number=str(ExampleVisit.visit_number),
33-
db=ispyb_db,
33+
db=ispyb_db_session,
3434
)
3535
assert query == result
3636

3737

3838
def test_get_proposal_id(
39-
ispyb_db: Session,
39+
ispyb_db_session: Session,
4040
):
4141
# Manually query the Proposal ID
4242
query = (
43-
ispyb_db.execute(
43+
ispyb_db_session.execute(
4444
select(Proposal)
4545
.where(Proposal.proposalCode == ExampleVisit.proposal_code)
4646
.where(Proposal.proposalNumber == ExampleVisit.proposal_number)
@@ -53,6 +53,6 @@ def test_get_proposal_id(
5353
result = get_proposal_id(
5454
proposal_code=ExampleVisit.proposal_code,
5555
proposal_number=ExampleVisit.proposal_number,
56-
db=ispyb_db,
56+
db=ispyb_db_session,
5757
)
5858
assert query == result

0 commit comments

Comments
 (0)