MCPcopy Index your code
hub / github.com/fastapi/sqlmodel / test_fields

Function test_fields

tests/test_select_typing.py:5–62  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3
4
5def test_fields() -> None:
6 class Hero(SQLModel, table=True):
7 id: int | None = Field(default=None, primary_key=True)
8 name: str
9 secret_name: str
10 age: int | None = None
11 food: str | None = None
12
13 engine = create_engine(
14 "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
15 )
16
17 SQLModel.metadata.create_all(engine)
18
19 with Session(engine) as session:
20 session.add(Hero(name="Deadpond", secret_name="Dive Wilson"))
21 session.add(
22 Hero(name="Spider-Boy", secret_name="Pedro Parqueador", food="pizza")
23 )
24 session.add(Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48))
25
26 session.commit()
27
28 # check typing of select with 3 fields
29 with Session(engine) as session:
30 statement_3 = select(Hero.id, Hero.name, Hero.secret_name)
31 results_3 = session.exec(statement_3)
32 for hero_3 in results_3:
33 assert len(hero_3) == 3
34 name_3: str = hero_3[1]
35 assert type(name_3) is str
36 assert type(hero_3[0]) is int
37 assert type(hero_3[2]) is str
38
39 # check typing of select with 4 fields
40 with Session(engine) as session:
41 statement_4 = select(Hero.id, Hero.name, Hero.secret_name, Hero.age)
42 results_4 = session.exec(statement_4)
43 for hero_4 in results_4:
44 assert len(hero_4) == 4
45 name_4: str = hero_4[1]
46 assert type(name_4) is str
47 assert type(hero_4[0]) is int
48 assert type(hero_4[2]) is str
49 assert type(hero_4[3]) in [int, type(None)]
50
51 # check typing of select with 5 fields: currently runs but doesn't pass mypy
52 # with Session(engine) as session:
53 # statement_5 = select(Hero.id, Hero.name, Hero.secret_name, Hero.age, Hero.food)
54 # results_5 = session.exec(statement_5)
55 # for hero_5 in results_5:
56 # assert len(hero_5) == 5
57 # name_5: str = hero_5[1]
58 # assert type(name_5) is str
59 # assert type(hero_5[0]) is int
60 # assert type(hero_5[2]) is str
61 # assert type(hero_5[3]) in [int, type(None)]
62 # assert type(hero_5[4]) in [str, type(None)]

Callers

nothing calls this directly

Calls 4

SessionClass · 0.90
selectFunction · 0.90
HeroClass · 0.70
execMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…