from typing import Optional

from sqlmodel import Field, Session, SQLModel, create_engine


def test_query(clear_sqlmodel):
    class Hero(SQLModel, table=True):
        id: Optional[int] = Field(default=None, primary_key=True)
        name: str
        secret_name: str
        age: Optional[int] = None

    hero_1 = Hero(name="Deadpond", secret_name="<NAME>")

    engine = create_engine("sqlite://")

    SQLModel.metadata.create_all(engine)
    with Session(engine) as session:
        session.add(hero_1)
        session.commit()
        session.refresh(hero_1)

    with Session(engine) as session:
        query_hero = session.query(Hero).first()
        assert query_hero
        assert query_hero.name == hero_1.name

sqlmodel.SQLModel.metadata.create_all sqlmodel.Field sqlmodel.create_engine sqlmodel.Session
[((377, 403), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite://"""'], {}), "('sqlite://')\n", (390, 403), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((409, 445), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (437, 445), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((190, 227), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (195, 227), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((455, 470), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (462, 470), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n'), ((578, 593), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (585, 593), False, 'from sqlmodel import Field, Session, SQLModel, create_engine\n')]