Skip to content

Commit 403d44e

Browse files
jbmchucktiangolo
andauthored
📝 Update docs, use offset in example with limit and where (#273)
Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent a1caaa0 commit 403d44e

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

docs/tutorial/limit-and-offset.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ Of course, you can also combine `.limit()` and `.offset()` with `.where()` and o
271271

272272
</details>
273273

274-
## Run the Program with Limit and Where on the Command Line
274+
## Run the Program with Limit, Offset, and Where on the Command Line
275275

276276
If we run it on the command line, it will find all the heroes in the database with an age above 32. That would normally be 4 heroes.
277277

278-
But we are limiting the results to only get the first 3:
278+
But we are starting to include after an offset of 1 (so we don't count the first one), and we are limiting the results to only get the first 2 after that:
279279

280280
<div class="termy">
281281

@@ -284,18 +284,17 @@ $ python app.py
284284

285285
// Previous output omitted 🙈
286286

287-
// Select with WHERE and LIMIT
287+
// Select with WHERE and LIMIT and OFFSET
288288
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
289289
FROM hero
290290
WHERE hero.age > ?
291291
LIMIT ? OFFSET ?
292-
INFO Engine [no key 0.00022s] (32, 3, 0)
292+
INFO Engine [no key 0.00022s] (32, 2, 1)
293293

294-
// Print the heroes received, only 3
294+
// Print the heroes received, only 2
295295
[
296-
Hero(age=35, secret_name='Trevor Challa', id=5, name='Black Lion'),
297-
Hero(age=36, secret_name='Steve Weird', id=6, name='Dr. Weird'),
298-
Hero(age=48, secret_name='Tommy Sharp', id=3, name='Rusty-Man')
296+
Hero(age=36, id=6, name='Dr. Weird', secret_name='Steve Weird'),
297+
Hero(age=48, id=3, name='Rusty-Man', secret_name='Tommy Sharp')
299298
]
300299
```
301300

docs_src/tutorial/offset_and_limit/tutorial004.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def create_heroes():
4343

4444
def select_heroes():
4545
with Session(engine) as session:
46-
statement = select(Hero).where(Hero.age > 32).limit(3)
46+
statement = select(Hero).where(Hero.age > 32).offset(1).limit(2)
4747
results = session.exec(statement)
4848
heroes = results.all()
4949
print(heroes)

tests/test_tutorial/test_limit_and_offset/test_tutorial004.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44

55
from ...conftest import get_testing_print_function
66

7-
expected_calls = [
8-
[
9-
[
10-
{"id": 5, "name": "Black Lion", "secret_name": "Trevor Challa", "age": 35},
11-
{"id": 6, "name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36},
12-
{"id": 3, "name": "Rusty-Man", "secret_name": "Tommy Sharp", "age": 48},
13-
]
14-
]
15-
]
16-
177

188
def test_tutorial(clear_sqlmodel):
199
from docs_src.tutorial.offset_and_limit import tutorial004 as mod
@@ -26,4 +16,11 @@ def test_tutorial(clear_sqlmodel):
2616

2717
with patch("builtins.print", new=new_print):
2818
mod.main()
29-
assert calls == expected_calls
19+
assert calls == [
20+
[
21+
[
22+
{"name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36, "id": 6},
23+
{"name": "Rusty-Man", "secret_name": "Tommy Sharp", "age": 48, "id": 3},
24+
]
25+
]
26+
]

0 commit comments

Comments
 (0)