@@ -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"""
0 commit comments