Skip to content

Commit 822050c

Browse files
committed
Added fixtures to create a mock ISPyB session
1 parent 992f484 commit 822050c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/conftest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
from configparser import ConfigParser
33
from pathlib import Path
44

5+
import ispyb
56
import pytest
7+
from ispyb.sqlalchemy import url
8+
from sqlalchemy import create_engine
9+
from sqlalchemy.orm import scoped_session, sessionmaker
610
from sqlmodel import Session
711

812
from murfey.util.db import Session as MurfeySession
@@ -70,3 +74,40 @@ def mock_security_configuration(
7074
with open(config_file, "w") as f:
7175
json.dump(security_config, f)
7276
return config_file
77+
78+
79+
"""
80+
=======================================================================================
81+
Fixtures for setting up mock ISPyB database
82+
=======================================================================================
83+
These were adapted from the tests found at:
84+
https://github.com/DiamondLightSource/ispyb-api/blob/main/tests/conftest.py
85+
"""
86+
87+
88+
@pytest.fixture
89+
def ispyb_db(mock_ispyb_credentials):
90+
with ispyb.open(mock_ispyb_credentials) as connection:
91+
yield connection
92+
93+
94+
@pytest.fixture(scope="session")
95+
def ispyb_engine(mock_ispyb_credentials):
96+
ispyb_engine = create_engine(
97+
url=url(mock_ispyb_credentials), connect_args={"use_pure": True}
98+
)
99+
yield ispyb_engine
100+
ispyb_engine.dispose()
101+
102+
103+
@pytest.fixture(scope="session")
104+
def ispyb_session_factory(ispyb_engine):
105+
return scoped_session(sessionmaker(bind=ispyb_engine))
106+
107+
108+
@pytest.fixture(scope="function")
109+
def ispyb_session(ispyb_session_factory):
110+
ispyb_session = ispyb_session_factory()
111+
yield ispyb_session
112+
ispyb_session.rollback()
113+
ispyb_session.close()

0 commit comments

Comments
 (0)