# A file containing fixtures for testing
# Fixtures defined here are available for the whole scope
from fastapi.testclient import TestClient
import pytest
import os
from ..main import app, session
from sqlmodel import SQLModel, Session, create_engine
from sqlmodel.pool import StaticPool
from ..utils import get_session


db_name = "test_db.sqlite"
test_con = f"sqlite:///{db_name}"
test_engine = create_engine(
    test_con, connect_args={"check_same_thread": False}, echo=True
)


@pytest.fixture(name="create_db", scope="session")
def create_db():
    # setup
    SQLModel.metadata.create_all(test_engine)
    yield
    # teardown
    os.remove(db_name)


@pytest.fixture(name="session")
def session_fixture(create_db):
    create_db

    with Session(test_engine) as session:
        yield session


@pytest.fixture(name="client")
def client_fixture(session: Session):
    def get_session_override():
        return session

    app.dependency_overrides[get_session] = get_session_override
    client = TestClient(app)
    yield client
    app.dependency_overrides.clear()

sqlmodel.create_engine fastapi.testclient.TestClient pytest.fixture sqlmodel.Session sqlmodel.SQLModel.metadata.create_all os.remove
[((397, 474), 'sqlmodel.create_engine', 'create_engine', (['test_con'], {'connect_args': "{'check_same_thread': False}", 'echo': '(True)'}), "(test_con, connect_args={'check_same_thread': False}, echo=True)\n", (410, 474), False, 'from sqlmodel import SQLModel, Session, create_engine\n'), ((484, 533), 'pytest.fixture', 'pytest.fixture', ([], {'name': '"""create_db"""', 'scope': '"""session"""'}), "(name='create_db', scope='session')\n", (498, 533), False, 'import pytest\n'), ((660, 690), 'pytest.fixture', 'pytest.fixture', ([], {'name': '"""session"""'}), "(name='session')\n", (674, 690), False, 'import pytest\n'), ((805, 834), 'pytest.fixture', 'pytest.fixture', ([], {'name': '"""client"""'}), "(name='client')\n", (819, 834), False, 'import pytest\n'), ((567, 608), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['test_engine'], {}), '(test_engine)\n', (595, 608), False, 'from sqlmodel import SQLModel, Session, create_engine\n'), ((638, 656), 'os.remove', 'os.remove', (['db_name'], {}), '(db_name)\n', (647, 656), False, 'import os\n'), ((1007, 1022), 'fastapi.testclient.TestClient', 'TestClient', (['app'], {}), '(app)\n', (1017, 1022), False, 'from fastapi.testclient import TestClient\n'), ((747, 767), 'sqlmodel.Session', 'Session', (['test_engine'], {}), '(test_engine)\n', (754, 767), False, 'from sqlmodel import SQLModel, Session, create_engine\n')]