Skip to content

Commit 68e4669

Browse files
authored
docs: use literalinclude instead of code-block on usage (#220)
Convert code blocks in usage docs to use `literalinclude`
1 parent 3061f72 commit 68e4669

File tree

7 files changed

+120
-31
lines changed

7 files changed

+120
-31
lines changed

docs/examples/usage/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from sqlspec import SQLSpec
2+
from sqlspec.adapters.sqlite import SqliteConfig
3+
4+
__all__ = ("test_index_1",)
5+
6+
7+
def test_index_1() -> None:
8+
db_manager = SQLSpec()
9+
db = db_manager.add_config(SqliteConfig())
10+
11+
with db_manager.provide_session(db) as session:
12+
_ = session.execute(
13+
"""
14+
CREATE TABLE users (
15+
id INTEGER PRIMARY KEY,
16+
name TEXT NOT NULL
17+
)
18+
"""
19+
)
20+
_ = session.execute("INSERT INTO users VALUES (?, ?)", 1, "alice")
21+
22+
with db_manager.provide_session(db) as session:
23+
user = session.select_one("SELECT * FROM users WHERE id = ?", 1)
24+
25+
assert user == {"id": 1, "name": "alice"}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from sqlspec import SQLSpec, sql
2+
from sqlspec.adapters.sqlite import SqliteConfig
3+
4+
__all__ = ("test_index_2",)
5+
6+
7+
def test_index_2() -> None:
8+
db_manager = SQLSpec()
9+
db = db_manager.add_config(SqliteConfig())
10+
query = sql.select("id", "name", "email").from_("users").where("active = ?")
11+
12+
with db_manager.provide_session(db) as session:
13+
_ = session.execute(
14+
"""
15+
CREATE TABLE users (
16+
id INTEGER PRIMARY KEY,
17+
name TEXT NOT NULL,
18+
email TEXT,
19+
active BOOLEAN NOT NULL DEFAULT 1
20+
)
21+
"""
22+
)
23+
_ = session.execute(
24+
"""
25+
INSERT INTO users VALUES
26+
(1, 'alice', 'alice@example.com', 1),
27+
(2, 'bob', 'bob@example.com', 0),
28+
(3, 'carol', 'carol@example.com', 1)
29+
"""
30+
)
31+
users = session.select(query, True) # noqa: FBT003
32+
33+
names = [user["name"] for user in users]
34+
assert names == ["alice", "carol"]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from pathlib import Path
2+
3+
from sqlspec import SQLSpec
4+
from sqlspec.adapters.sqlite import SqliteConfig
5+
from sqlspec.loader import SQLFileLoader
6+
7+
__all__ = ("test_index_3",)
8+
9+
10+
QUERIES_PATH = Path(__file__).parent.parent / "queries" / "users.sql"
11+
12+
13+
def test_index_3() -> None:
14+
db_manager = SQLSpec()
15+
db = db_manager.add_config(SqliteConfig())
16+
loader = SQLFileLoader()
17+
loader.load_sql(QUERIES_PATH)
18+
get_user_by_id = loader.get_sql("get_user_by_id")
19+
20+
with db_manager.provide_session(db) as session:
21+
_ = session.execute(
22+
"""
23+
CREATE TABLE users (
24+
id INTEGER PRIMARY KEY,
25+
username TEXT NOT NULL,
26+
email TEXT,
27+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
28+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
29+
)
30+
"""
31+
)
32+
_ = session.execute(
33+
"""
34+
INSERT INTO users(id, username, email)
35+
VALUES (1, 'alice', 'alice@example.com'),
36+
(2, 'bob', 'bob@example.com')
37+
"""
38+
)
39+
user = session.select_one(get_user_by_id, user_id=2)
40+
41+
assert user["username"] == "bob"
42+
assert user["email"] == "bob@example.com"

docs/usage/index.rst

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,27 @@ Quick Reference
5151

5252
**Basic Query Execution**
5353

54-
.. code-block:: python
55-
56-
from sqlspec import SQLSpec
57-
from sqlspec.adapters.sqlite import SqliteConfig
58-
59-
spec = SQLSpec()
60-
db = spec.add_config(SqliteConfig())
61-
62-
with spec.provide_session(db) as session:
63-
result = session.execute("SELECT * FROM users WHERE id = ?", 1)
64-
user = result.one()
54+
.. literalinclude:: /examples/usage/usage_index_1.py
55+
:language: python
56+
:caption: ``basic query execution``
57+
:lines: 1-23
58+
:dedent: 4
6559

6660
**Using the Query Builder**
6761

68-
.. code-block:: python
69-
70-
from sqlspec import sql
71-
72-
query = sql.select("id", "name", "email").from_("users").where("active = ?")
73-
result = session.execute(query, True)
74-
users = result.all()
62+
.. literalinclude:: /examples/usage/usage_index_2.py
63+
:language: python
64+
:caption: ``using the query builder``
65+
:lines: 1-32
66+
:dedent: 4
7567

7668
**Loading from SQL Files**
7769

78-
.. code-block:: python
79-
80-
from sqlspec.loader import SQLFileLoader
81-
82-
loader = SQLFileLoader()
83-
loader.load_sql("queries/users.sql")
84-
85-
user_query = loader.get_sql("get_user_by_id", user_id=123)
86-
result = session.execute(user_query)
70+
.. literalinclude:: /examples/usage/usage_index_3.py
71+
:language: python
72+
:caption: ``loading from sql files``
73+
:lines: 1-39
74+
:dedent: 4
8775

8876
Next Steps
8977
----------

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ exclude_lines = [
287287
addopts = ["-q", "-ra"]
288288
asyncio_default_fixture_loop_scope = "function"
289289
asyncio_mode = "auto"
290-
python_files = ["test_*.py", "quickstart_*.py"]
290+
python_files = ["test_*.py", "quickstart_*.py", "usage_*.py"]
291291
filterwarnings = [
292292
"ignore::DeprecationWarning:pkg_resources.*",
293293
"ignore:pkg_resources is deprecated as an API:DeprecationWarning",
@@ -331,7 +331,7 @@ markers = [
331331
"pymysql: marks tests using pymysql",
332332
"psqlpy: marks tests using psqlpy",
333333
]
334-
testpaths = ["tests", "docs/examples/quickstart"]
334+
testpaths = ["tests", "docs/examples/quickstart", "docs/examples/usage"]
335335

336336
[tool.mypy]
337337
exclude = ["tmp/", ".tmp/", ".bugs/"]

specs/guides/docs_examples_alignment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
## Testing Command Examples
1919

2020
```bash
21-
uv run pytest docs/examples/quickstart -q
21+
uv run pytest docs/examples/quickstart docs/examples/usage -q
2222
```
2323

2424
- `docs/examples/quickstart/conftest.py` sets `SQLSPEC_QUICKSTART_PG_*` from `pytest-databases` so `quickstart_5.py` stays copy/paste friendly in the docs.
25-
- Async or adapter-specific samples no longer need separate commands—pytest collects `quickstart_4.py` and `quickstart_5.py` automatically via `python_files`.
25+
- Usage samples are function-based (`usage_*.py`) and collected automatically because `python_files` now includes that pattern.
2626
- Prefer smaller batches (per topic/section) to keep feedback loops fast.
2727

2828
## Review Checklist

0 commit comments

Comments
 (0)