|
2 | 2 | from configparser import ConfigParser |
3 | 3 | from pathlib import Path |
4 | 4 |
|
| 5 | +import ispyb |
5 | 6 | import pytest |
| 7 | +from ispyb.sqlalchemy import url |
| 8 | +from sqlalchemy import create_engine |
| 9 | +from sqlalchemy.orm import scoped_session, sessionmaker |
6 | 10 | from sqlmodel import Session |
7 | 11 |
|
8 | 12 | from murfey.util.db import Session as MurfeySession |
@@ -70,3 +74,40 @@ def mock_security_configuration( |
70 | 74 | with open(config_file, "w") as f: |
71 | 75 | json.dump(security_config, f) |
72 | 76 | 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