Last active
January 7, 2022 08:25
-
-
Save fmind/184609dd8b1519893945e133d3639a9e to your computer and use it in GitHub Desktop.
Snippets for Pytest fixtures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Declare fixtures for SQLAlchemy (session and engine).""" | |
# IMPORTS | |
import sqlalchemy as sql | |
from sqlalchemy import orm | |
from project import models # TODO | |
# FIXTURES | |
@pytest.fixture(scope="function") | |
def engine() -> sql.engine.Engine: | |
"""Create an SQLAlchemy engine for testing purposes.""" | |
engine = sql.create_engine("sqlite:///:memory:") | |
return engine | |
@pytest.fixture(scope="function") | |
def session(engine: sql.engine.Engine) -> T.Iterator[orm.Session]: | |
"""Create an SQLAlchemy session for testing purposes.""" | |
models.Base.metadata.create_all(engine) | |
with orm.Session(engine) as session_: | |
yield session_ | |
session_.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment