-
Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathconftest.py
49 lines (36 loc) · 1.33 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from os import environ
import pytest
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
@pytest.fixture(scope="session")
def db_engine():
return create_engine(environ["DATABASE_URL"])
@pytest.fixture(scope="session")
def db_sessionfactory():
return sessionmaker()
@pytest.fixture
def db_session(db_engine, db_sessionfactory):
"""
Return the SQLAlchemy database session.
This returns a session that is wrapped in an external transaction that is
rolled back after each test, so tests can't make database changes that
affect later tests. Even if the test (or the code under test) calls
session.commit() this won't touch the external transaction.
Recipe adapted from:
https://docs.sqlalchemy.org/en/20/orm/session_transaction.html#joining-a-session-into-an-external-transaction-such-as-for-test-suites
"""
connection = db_engine.connect()
transaction = connection.begin()
session = db_sessionfactory(
bind=connection, join_transaction_mode="create_savepoint"
)
try:
yield session
finally:
session.close()
transaction.rollback()
connection.close()
@pytest.fixture
def db_session_replica(db_session):
db_session.execute(text("SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;"))
return db_session