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 