# https://itnext.io/setting-up-transactional-tests-with-pytest-and-sqlalchemy-b2d726347629

import pytest
import sys
sys.path.append("app")
import models

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker



@pytest.fixture(scope="session")
def connection():
    engine = create_engine(
        "mysql+mysqldb://musicmuster_testing:musicmuster_testing@"
        "localhost/musicmuster_testing"
    )
    return engine.connect()


@pytest.fixture(scope="session")
def setup_database(connection):
    from app.models import Base     # noqa E402
    Base.metadata.bind = connection
    Base.metadata.create_all()
    # seed_database()

    yield

    Base.metadata.drop_all()


@pytest.fixture
def session(setup_database, connection):
    transaction = connection.begin()
    yield scoped_session(
        sessionmaker(autocommit=False, autoflush=False, bind=connection)
    )
    transaction.rollback()
