from typing import Optional

from fastapi import FastAPI
from sqlmodel import Session, SQLModel, create_engine

from .abstractions import MultipleModels, register_endpoints


class HeroBase(SQLModel):
    name: str
    secret_name: str
    age: Optional[int] = None


class HeroRead(HeroBase):
    id: int


hero_models = MultipleModels(path="/heroes/", base=HeroBase, response=HeroRead)
Hero = hero_models.table  # Shim to avoid changing tests.

sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)


def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


def get_session():
    with Session(engine) as session:
        yield session


app = FastAPI()

register_endpoints(app, models=hero_models, get_session=get_session)

sqlmodel.SQLModel.metadata.create_all sqlmodel.Session sqlmodel.create_engine fastapi.FastAPI
[((579, 642), 'sqlmodel.create_engine', 'create_engine', (['sqlite_url'], {'echo': '(True)', 'connect_args': 'connect_args'}), '(sqlite_url, echo=True, connect_args=connect_args)\n', (592, 642), False, 'from sqlmodel import Session, SQLModel, create_engine\n'), ((802, 811), 'fastapi.FastAPI', 'FastAPI', ([], {}), '()\n', (809, 811), False, 'from fastapi import FastAPI\n'), ((677, 713), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (705, 713), False, 'from sqlmodel import Session, SQLModel, create_engine\n'), ((744, 759), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (751, 759), False, 'from sqlmodel import Session, SQLModel, create_engine\n')]