import os.path

import pytest
from alembic.command import upgrade
from alembic.config import Config

from app.database import sa, create_engine, create_db_session
from app.models import Base


HERE = os.path.dirname(os.path.abspath(__file__))
ALEMBIC_CONFIG = os.path.join(HERE, "..", "..", "alembic.ini")


def apply_migrations(engine):
    """Applies all alembic migrations."""
    from alembic.config import Config
    from alembic import command

    alembic_cfg = Config(ALEMBIC_CONFIG)
    with engine.begin() as connection:
        alembic_cfg.attributes['connection'] = connection
        command.upgrade(alembic_cfg, "head")


@pytest.fixture(scope='session')
def engine(request):
    """Session-wide test database."""
    # create a SQLite DB in memory
    _engine = create_engine("sqlite://")

    # setup models in DB
    Base.metadata.create_all(_engine)

    return _engine


@pytest.fixture(scope='function')
def session(engine, request):
    """Creates a new database session for a test."""
    _session = create_db_session(engine)
    connection = engine.connect()
    transaction = connection.begin()

    def fin():
        transaction.rollback()
        connection.close()

    request.addfinalizer(fin)
    return _session

import os.path

import pytest
from alembic.command import upgrade
from alembic.config import Config

from app.database import db as _db
from app.database import create_engine, create_db_session
from app.models import Base


HERE = os.path.dirname(os.path.abspath(__file__))
ALEMBIC_CONFIG = os.path.join(HERE, "..", "..", "alembic.ini")


def apply_migrations(engine):
    """Applies all alembic migrations."""
    from alembic.config import Config
    from alembic import command

    alembic_cfg = Config(ALEMBIC_CONFIG)
    with engine.begin() as connection:
        alembic_cfg.attributes['connection'] = connection
        command.upgrade(alembic_cfg, "head")


@pytest.fixture(scope='session')
def db(request):
    """Session-wide test database."""
    # create a SQLite DB in memory
    _db.engine = create_engine("sqlite://")

    # setup models in DB
    Base.metadata.create_all(_db.engine)

    return _db


@pytest.fixture(scope='function')
def session(db, request):
    """Creates a new database session for a test."""
    _session = create_db_session(db.engine)
    connection = db.engine.connect()
    transaction = connection.begin()

    def fin():
        transaction.rollback()
        connection.close()

    request.addfinalizer(fin)
    return _session

Change engine fixture to use db obj
Change engine fixture to use db obj

  import os.path
  
  import pytest
  from alembic.command import upgrade
  from alembic.config import Config
  
+ from app.database import db as _db
- from app.database import sa, create_engine, create_db_session
+ from app.database import create_engine, create_db_session
  from app.models import Base
  
  
  HERE = os.path.dirname(os.path.abspath(__file__))
  ALEMBIC_CONFIG = os.path.join(HERE, "..", "..", "alembic.ini")
  
  
  def apply_migrations(engine):
      """Applies all alembic migrations."""
      from alembic.config import Config
      from alembic import command
  
      alembic_cfg = Config(ALEMBIC_CONFIG)
      with engine.begin() as connection:
          alembic_cfg.attributes['connection'] = connection
          command.upgrade(alembic_cfg, "head")
  
  
  @pytest.fixture(scope='session')
- def engine(request):
+ def db(request):
      """Session-wide test database."""
      # create a SQLite DB in memory
-     _engine = create_engine("sqlite://")
+     _db.engine = create_engine("sqlite://")
  
      # setup models in DB
-     Base.metadata.create_all(_engine)
+     Base.metadata.create_all(_db.engine)
  
-     return _engine
+     return _db
  
  
  @pytest.fixture(scope='function')
- def session(engine, request):
+ def session(db, request):
      """Creates a new database session for a test."""
-     _session = create_db_session(engine)
+     _session = create_db_session(db.engine)
-     connection = engine.connect()
+     connection = db.engine.connect()
      transaction = connection.begin()
  
      def fin():
          transaction.rollback()
          connection.close()
  
      request.addfinalizer(fin)
      return _session
  
Change engine fixture to use db obj
## Code Before:
import os.path

import pytest
from alembic.command import upgrade
from alembic.config import Config

from app.database import sa, create_engine, create_db_session
from app.models import Base


HERE = os.path.dirname(os.path.abspath(__file__))
ALEMBIC_CONFIG = os.path.join(HERE, "..", "..", "alembic.ini")


def apply_migrations(engine):
    """Applies all alembic migrations."""
    from alembic.config import Config
    from alembic import command

    alembic_cfg = Config(ALEMBIC_CONFIG)
    with engine.begin() as connection:
        alembic_cfg.attributes['connection'] = connection
        command.upgrade(alembic_cfg, "head")


@pytest.fixture(scope='session')
def engine(request):
    """Session-wide test database."""
    # create a SQLite DB in memory
    _engine = create_engine("sqlite://")

    # setup models in DB
    Base.metadata.create_all(_engine)

    return _engine


@pytest.fixture(scope='function')
def session(engine, request):
    """Creates a new database session for a test."""
    _session = create_db_session(engine)
    connection = engine.connect()
    transaction = connection.begin()

    def fin():
        transaction.rollback()
        connection.close()

    request.addfinalizer(fin)
    return _session

## Instruction:
Change engine fixture to use db obj
## Code After:
import os.path

import pytest
from alembic.command import upgrade
from alembic.config import Config

from app.database import db as _db
from app.database import create_engine, create_db_session
from app.models import Base


HERE = os.path.dirname(os.path.abspath(__file__))
ALEMBIC_CONFIG = os.path.join(HERE, "..", "..", "alembic.ini")


def apply_migrations(engine):
    """Applies all alembic migrations."""
    from alembic.config import Config
    from alembic import command

    alembic_cfg = Config(ALEMBIC_CONFIG)
    with engine.begin() as connection:
        alembic_cfg.attributes['connection'] = connection
        command.upgrade(alembic_cfg, "head")


@pytest.fixture(scope='session')
def db(request):
    """Session-wide test database."""
    # create a SQLite DB in memory
    _db.engine = create_engine("sqlite://")

    # setup models in DB
    Base.metadata.create_all(_db.engine)

    return _db


@pytest.fixture(scope='function')
def session(db, request):
    """Creates a new database session for a test."""
    _session = create_db_session(db.engine)
    connection = db.engine.connect()
    transaction = connection.begin()

    def fin():
        transaction.rollback()
        connection.close()

    request.addfinalizer(fin)
    return _session
