import os
import pytest
import time
from fastapi.testclient import TestClient
from sqlmodel import Session, create_engine
from main import app
from database import get_db
from settings import Settings
from alembic.command import upgrade, downgrade
from alembic.config import Config


@pytest.fixture(autouse=True)
def slow_down_tests():
    yield
    time.sleep(1)


@pytest.fixture(scope="session")
def apply_migrations():
    config = Config("alembic.ini")
    upgrade(config, "head")
    yield
    downgrade(config, "base")


@pytest.fixture(name="session")
def session_fixture(apply_migrations: None):
    settings = Settings()
    engine = create_engine(
        settings.db_uri_test, connect_args={"check_same_thread": False}
    )
    with Session(engine) as session:
        yield session


@pytest.fixture(name="client")
def client_fixture(session: Session):
    def get_session_override():
        return session
    app.dependency_overrides[get_db] = get_session_override
    client = TestClient(app)
    yield client
    app.dependency_overrides.clear()


def pytest_configure(config):
    """
    Allows plugins and conftest files to perform initial configuration.
    This hook is called for every plugin and initial conftest
    file after command line options have been parsed.
    """

    pytest.access_token = None


def pytest_sessionstart(session):
    """
    Called after the Session object has been created and
    before performing collection and entering the run test loop.
    """


def pytest_sessionfinish(session, exitstatus):
    """
    Called after whole test run finished, right before
    returning the exit status to the system.
    """


def pytest_unconfigure(config):
    """
    called before test process is exited.
    """

    # Delete DB file
    db_file = os.path.join('.', 'test.db')
    if os.path.exists(db_file):
        os.remove(db_file)

alembic.config.Config alembic.command.upgrade os.remove sqlmodel.create_engine os.path.join sqlmodel.Session main.app.dependency_overrides.clear pytest.fixture os.path.exists time.sleep fastapi.testclient.TestClient alembic.command.downgrade settings.Settings
[((285, 313), 'pytest.fixture', 'pytest.fixture', ([], {'autouse': '(True)'}), '(autouse=True)\n', (299, 313), False, 'import pytest\n'), ((368, 399), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (382, 399), False, 'import pytest\n'), ((530, 560), 'pytest.fixture', 'pytest.fixture', ([], {'name': '"""session"""'}), "(name='session')\n", (544, 560), False, 'import pytest\n'), ((800, 829), 'pytest.fixture', 'pytest.fixture', ([], {'name': '"""client"""'}), "(name='client')\n", (814, 829), False, 'import pytest\n'), ((351, 364), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (361, 364), False, 'import time\n'), ((437, 458), 'alembic.config.Config', 'Config', (['"""alembic.ini"""'], {}), "('alembic.ini')\n", (443, 458), False, 'from alembic.config import Config\n'), ((463, 486), 'alembic.command.upgrade', 'upgrade', (['config', '"""head"""'], {}), "(config, 'head')\n", (470, 486), False, 'from alembic.command import upgrade, downgrade\n'), ((501, 526), 'alembic.command.downgrade', 'downgrade', (['config', '"""base"""'], {}), "(config, 'base')\n", (510, 526), False, 'from alembic.command import upgrade, downgrade\n'), ((621, 631), 'settings.Settings', 'Settings', ([], {}), '()\n', (629, 631), False, 'from settings import Settings\n'), ((645, 723), 'sqlmodel.create_engine', 'create_engine', (['settings.db_uri_test'], {'connect_args': "{'check_same_thread': False}"}), "(settings.db_uri_test, connect_args={'check_same_thread': False})\n", (658, 723), False, 'from sqlmodel import Session, create_engine\n'), ((996, 1011), 'fastapi.testclient.TestClient', 'TestClient', (['app'], {}), '(app)\n', (1006, 1011), False, 'from fastapi.testclient import TestClient\n'), ((1033, 1065), 'main.app.dependency_overrides.clear', 'app.dependency_overrides.clear', ([], {}), '()\n', (1063, 1065), False, 'from main import app\n'), ((1801, 1829), 'os.path.join', 'os.path.join', (['"""."""', '"""test.db"""'], {}), "('.', 'test.db')\n", (1813, 1829), False, 'import os\n'), ((1837, 1860), 'os.path.exists', 'os.path.exists', (['db_file'], {}), '(db_file)\n', (1851, 1860), False, 'import os\n'), ((747, 762), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (754, 762), False, 'from sqlmodel import Session, create_engine\n'), ((1870, 1888), 'os.remove', 'os.remove', (['db_file'], {}), '(db_file)\n', (1879, 1888), False, 'import os\n')]