Skip to content

Commit e6777bc

Browse files
committed
Rename code example, add _py310 version
1 parent bd737cc commit e6777bc

5 files changed

Lines changed: 93 additions & 29 deletions

File tree

docs/tutorial/where.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ We get `Black Lion` here too because although the age is not *strictly* less tha
583583

584584
Finally, we can use `in_` to get the rows where a column is a member of a collection of values:
585585

586-
{* ./docs_src/tutorial/where/tutorial0065.py ln[44:49] hl[46] *}
586+
{* ./docs_src/tutorial/where/tutorial006b_py310.py ln[42:47] hl[44] *}
587587

588588
In this case, we match `Deadpond` since it's part of the collections of names.
589589
We don't have any hero called `Ratman`, so it does not match any hero.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from sqlmodel import Field, Session, SQLModel, col, create_engine, select
2+
3+
4+
class Hero(SQLModel, table=True):
5+
id: int | None = Field(default=None, primary_key=True)
6+
name: str
7+
secret_name: str
8+
age: int | None = None
9+
10+
11+
sqlite_file_name = "database.db"
12+
sqlite_url = f"sqlite:///{sqlite_file_name}"
13+
14+
engine = create_engine(sqlite_url, echo=True)
15+
16+
17+
def create_db_and_tables():
18+
SQLModel.metadata.create_all(engine)
19+
20+
21+
def create_heroes():
22+
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
23+
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
24+
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
25+
hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32)
26+
hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35)
27+
hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36)
28+
hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93)
29+
30+
with Session(engine) as session:
31+
session.add(hero_1)
32+
session.add(hero_2)
33+
session.add(hero_3)
34+
session.add(hero_4)
35+
session.add(hero_5)
36+
session.add(hero_6)
37+
session.add(hero_7)
38+
39+
session.commit()
40+
41+
42+
def select_heroes():
43+
with Session(engine) as session:
44+
statement = select(Hero).where(col(Hero.name).in_(["Deadpond", "Ratman"]))
45+
results = session.exec(statement)
46+
for hero in results:
47+
print(hero)
48+
49+
50+
def main():
51+
create_db_and_tables()
52+
create_heroes()
53+
select_heroes()
54+
55+
56+
if __name__ == "__main__":
57+
main()
File renamed without changes.

tests/test_tutorial/test_where/test_tutorial0065.py

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import importlib
2+
from types import ModuleType
3+
4+
import pytest
5+
from sqlmodel import create_engine
6+
7+
from ...conftest import PrintMock, needs_py310
8+
9+
10+
@pytest.fixture(
11+
name="mod",
12+
params=[
13+
pytest.param("tutorial006b_py39"),
14+
pytest.param("tutorial006b_py310", marks=needs_py310),
15+
],
16+
)
17+
def get_module(request: pytest.FixtureRequest) -> ModuleType:
18+
mod = importlib.import_module(f"docs_src.tutorial.where.{request.param}")
19+
mod.sqlite_url = "sqlite://"
20+
mod.engine = create_engine(mod.sqlite_url)
21+
return mod
22+
23+
24+
def test_tutorial(print_mock: PrintMock, mod: ModuleType):
25+
mod.main()
26+
assert print_mock.calls == [
27+
[
28+
{
29+
"name": "Deadpond",
30+
"secret_name": "Dive Wilson",
31+
"age": None,
32+
"id": 1,
33+
}
34+
]
35+
]

0 commit comments

Comments
 (0)