<reponame>baloo1379/transposition-api
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session

from app.database.connection import Base


@pytest.fixture(scope='session')
def db_engine(request):
    """yields a SQLAlchemy engine which is suppressed after the test session"""
    db_url = 'sqlite:///./database/test_database.sqlite3'
    engine_ = create_engine(db_url, echo=True, connect_args={"check_same_thread": False})

    yield engine_

    engine_.dispose()


@pytest.fixture(scope='session')
def db_tables(db_engine):
    """create tables which are dropped after the test session"""
    Base.metadata.create_all(db_engine)
    yield
    Base.metadata.drop_all(db_engine)


@pytest.fixture(scope='session')
def db_session_factory(db_engine):
    """returns a SQLAlchemy scoped session factory"""
    return scoped_session(sessionmaker(bind=db_engine))


@pytest.fixture(scope='session')
def db_session(db_session_factory, db_tables):
    """yields a SQLAlchemy connection which is rollbacked after the test"""
    session_ = db_session_factory()

    yield session_

    session_.rollback()
    session_.close()
