Skip to content

Commit 162288c

Browse files
committed
Murfey database uses SQLModel commands; recreate Murfey databaes fixtures as SQLModel sessions
1 parent f065efc commit 162288c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

tests/conftest.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
import os
35
from configparser import ConfigParser
@@ -11,6 +13,7 @@
1113
from sqlalchemy.ext.declarative import DeclarativeMeta
1214
from sqlalchemy.orm import Session as SQLAlchemySession
1315
from sqlalchemy.orm import sessionmaker
16+
from sqlmodel import Session as SQLModelSession
1417
from sqlmodel import SQLModel
1518

1619
from murfey.util.db import Session as MurfeySession
@@ -108,7 +111,7 @@ class ExampleVisit:
108111

109112

110113
def get_or_create_db_entry(
111-
session: SQLAlchemySession,
114+
session: SQLAlchemySession | SQLModelSession,
112115
table: Type[SQLAlchemyTable],
113116
lookup_kwargs: dict[str, Any] = {},
114117
insert_kwargs: dict[str, Any] = {},
@@ -139,7 +142,9 @@ def get_or_create_db_entry(
139142
return entry
140143

141144

142-
def restart_savepoint(session: SQLAlchemySession, transaction: RootTransaction):
145+
def restart_savepoint(
146+
session: SQLAlchemySession | SQLModelSession, transaction: RootTransaction
147+
):
143148
"""
144149
Re-establish a SAVEPOINT after a nested transaction is committed or rolled back.
145150
This helps to maintain isolation across different test cases.
@@ -260,37 +265,39 @@ def murfey_db_engine():
260265

261266
@pytest.fixture(scope="session")
262267
def murfey_db_session_factory(murfey_db_engine):
263-
return sessionmaker(bind=murfey_db_engine, expire_on_commit=False)
268+
return sessionmaker(
269+
bind=murfey_db_engine, expire_on_commit=False, class_=SQLModelSession
270+
)
264271

265272

266273
@pytest.fixture(scope="session")
267274
def seed_murfey_db(murfey_db_session_factory):
268275
# Populate Murfey database with initial values
269-
murfey_session: SQLAlchemySession = murfey_db_session_factory()
276+
session: SQLModelSession = murfey_db_session_factory()
270277
_ = get_or_create_db_entry(
271-
session=murfey_session,
278+
session=session,
272279
table=MurfeySession,
273280
lookup_kwargs={
274281
"id": ExampleVisit.murfey_session_id,
275282
"name": f"{ExampleVisit.proposal_code}{ExampleVisit.proposal_number}-{ExampleVisit.visit_number}",
276283
},
277284
)
278-
murfey_session.close()
285+
session.close()
279286

280287

281288
@pytest.fixture
282289
def murfey_db_session(
283290
murfey_db_session_factory,
284291
murfey_db_engine: Engine,
285292
seed_murfey_db,
286-
) -> Generator[SQLAlchemySession, None, None]:
293+
) -> Generator[SQLModelSession, None, None]:
287294
"""
288295
Returns a test-safe session that wraps each test in a rollback-safe save point
289296
"""
290297
connection = murfey_db_engine.connect()
291298
transaction = connection.begin()
292299

293-
session: SQLAlchemySession = murfey_db_session_factory(bind=connection)
300+
session: SQLModelSession = murfey_db_session_factory(bind=connection)
294301
session.begin_nested() # Save point for test
295302

296303
# Trigger the restart_savepoint function after the end of the transaction

0 commit comments

Comments
 (0)