|
1 | | -import os |
2 | | -from typing import Any |
| 1 | +"""Async PostgreSQL quickstart example.""" |
3 | 2 |
|
4 | | -from pydantic import BaseModel |
| 3 | +import pytest |
5 | 4 |
|
6 | | -from sqlspec import SQLSpec |
7 | | -from sqlspec.adapters.asyncpg import AsyncpgConfig |
| 5 | +__all__ = ("test_quickstart_5",) |
8 | 6 |
|
9 | | -__all__ = ("User", "test_quickstart_5") |
10 | 7 |
|
11 | | - |
12 | | -class User(BaseModel): |
13 | | - id: int |
14 | | - name: str |
15 | | - email: str |
16 | | - |
17 | | - |
18 | | -def _pool_config() -> "dict[str, Any]": |
19 | | - return { |
20 | | - "host": os.getenv("SQLSPEC_QUICKSTART_PG_HOST", "localhost"), |
21 | | - "port": int(os.getenv("SQLSPEC_QUICKSTART_PG_PORT", "5432")), |
22 | | - "user": os.getenv("SQLSPEC_QUICKSTART_PG_USER", "postgres"), |
23 | | - "password": os.getenv("SQLSPEC_QUICKSTART_PG_PASSWORD", "postgres"), |
24 | | - "database": os.getenv("SQLSPEC_QUICKSTART_PG_DATABASE", "mydb"), |
25 | | - } |
26 | | - |
27 | | - |
28 | | -async def _seed_users(session: Any) -> None: |
29 | | - await session.execute( |
30 | | - """ |
31 | | - CREATE TABLE IF NOT EXISTS users ( |
32 | | - id INTEGER PRIMARY KEY, |
33 | | - name TEXT NOT NULL, |
34 | | - email TEXT NOT NULL |
| 8 | +@pytest.mark.asyncio |
| 9 | +async def test_quickstart_5() -> None: |
| 10 | + """Demonstrate async PostgreSQL usage with SQLSpec.""" |
| 11 | + # start-example |
| 12 | + import os |
| 13 | + from typing import Any |
| 14 | + |
| 15 | + from pydantic import BaseModel |
| 16 | + |
| 17 | + from sqlspec import SQLSpec |
| 18 | + from sqlspec.adapters.asyncpg import AsyncpgConfig |
| 19 | + |
| 20 | + class User(BaseModel): |
| 21 | + id: int |
| 22 | + name: str |
| 23 | + email: str |
| 24 | + |
| 25 | + def pool_config() -> "dict[str, Any]": |
| 26 | + return { |
| 27 | + "host": os.getenv("SQLSPEC_QUICKSTART_PG_HOST", "localhost"), |
| 28 | + "port": int(os.getenv("SQLSPEC_QUICKSTART_PG_PORT", "5432")), |
| 29 | + "user": os.getenv("SQLSPEC_QUICKSTART_PG_USER", "postgres"), |
| 30 | + "password": os.getenv("SQLSPEC_QUICKSTART_PG_PASSWORD", "postgres"), |
| 31 | + "database": os.getenv("SQLSPEC_QUICKSTART_PG_DATABASE", "mydb"), |
| 32 | + } |
| 33 | + |
| 34 | + async def seed_users(session: Any) -> None: |
| 35 | + await session.execute( |
| 36 | + """ |
| 37 | + CREATE TABLE IF NOT EXISTS users ( |
| 38 | + id INTEGER PRIMARY KEY, |
| 39 | + name TEXT NOT NULL, |
| 40 | + email TEXT NOT NULL |
| 41 | + ) |
| 42 | + """ |
| 43 | + ) |
| 44 | + await session.execute("TRUNCATE TABLE users") |
| 45 | + await session.execute( |
| 46 | + "INSERT INTO users (id, name, email) VALUES ($1, $2, $3)", 1, "Alice", "alice@example.com" |
35 | 47 | ) |
36 | | - """ |
37 | | - ) |
38 | | - await session.execute("TRUNCATE TABLE users") |
39 | | - await session.execute("INSERT INTO users (id, name, email) VALUES ($1, $2, $3)", 1, "Alice", "alice@example.com") |
40 | | - |
41 | 48 |
|
42 | | -async def test_quickstart_5() -> None: |
43 | 49 | db_manager = SQLSpec() |
44 | | - db = db_manager.add_config(AsyncpgConfig(pool_config=_pool_config())) |
| 50 | + db = db_manager.add_config(AsyncpgConfig(pool_config=pool_config())) |
45 | 51 |
|
46 | 52 | async with db_manager.provide_session(db) as session: |
47 | | - await _seed_users(session) |
48 | | - |
49 | | - # PostgreSQL uses $1, $2 for parameters instead of ? |
| 53 | + await seed_users(session) |
50 | 54 | user = await session.select_one("SELECT * FROM users WHERE id = $1", 1, schema_type=User) |
51 | 55 | print(f"User: {user.name}") |
| 56 | + # end-example |
52 | 57 |
|
53 | 58 | assert user == User(id=1, name="Alice", email="alice@example.com") |
0 commit comments