Skip to content

Commit 8c3af57

Browse files
committed
tests(emit_str_enum): add end to end test for emit str enum
1 parent 1f7fd85 commit 8c3af57

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.27.0
4+
import dataclasses
5+
import enum
6+
from typing import Optional
7+
8+
9+
class BookStatus(enum.StrEnum):
10+
AVAILABLE = "available"
11+
CHECKED_OUT = "checked_out"
12+
OVERDUE = "overdue"
13+
14+
15+
@dataclasses.dataclass()
16+
class Book:
17+
id: int
18+
title: str
19+
status: Optional[BookStatus]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.27.0
4+
# source: query.sql
5+
from typing import AsyncIterator, Iterator, Optional
6+
7+
import sqlalchemy
8+
import sqlalchemy.ext.asyncio
9+
10+
from db import models
11+
12+
13+
CREATE_BOOK = """-- name: create_book \\:one
14+
INSERT INTO books (
15+
title, status
16+
) VALUES (
17+
:p1, :p2
18+
) RETURNING id, title, status
19+
"""
20+
21+
22+
DELETE_BOOK = """-- name: delete_book \\:exec
23+
DELETE FROM books
24+
WHERE id = :p1
25+
"""
26+
27+
28+
GET_BOOK = """-- name: get_book \\:one
29+
SELECT id, title, status FROM books
30+
WHERE id = :p1 LIMIT 1
31+
"""
32+
33+
34+
LIST_BOOKS = """-- name: list_books \\:many
35+
SELECT id, title, status FROM books
36+
ORDER BY title
37+
"""
38+
39+
40+
class Querier:
41+
def __init__(self, conn: sqlalchemy.engine.Connection):
42+
self._conn = conn
43+
44+
def create_book(self, *, title: str, status: Optional[models.BookStatus]) -> Optional[models.Book]:
45+
row = self._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1": title, "p2": status}).first()
46+
if row is None:
47+
return None
48+
return models.Book(
49+
id=row[0],
50+
title=row[1],
51+
status=row[2],
52+
)
53+
54+
def delete_book(self, *, id: int) -> None:
55+
self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": id})
56+
57+
def get_book(self, *, id: int) -> Optional[models.Book]:
58+
row = self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": id}).first()
59+
if row is None:
60+
return None
61+
return models.Book(
62+
id=row[0],
63+
title=row[1],
64+
status=row[2],
65+
)
66+
67+
def list_books(self) -> Iterator[models.Book]:
68+
result = self._conn.execute(sqlalchemy.text(LIST_BOOKS))
69+
for row in result:
70+
yield models.Book(
71+
id=row[0],
72+
title=row[1],
73+
status=row[2],
74+
)
75+
76+
77+
class AsyncQuerier:
78+
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
79+
self._conn = conn
80+
81+
async def create_book(self, *, title: str, status: Optional[models.BookStatus]) -> Optional[models.Book]:
82+
row = (await self._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1": title, "p2": status})).first()
83+
if row is None:
84+
return None
85+
return models.Book(
86+
id=row[0],
87+
title=row[1],
88+
status=row[2],
89+
)
90+
91+
async def delete_book(self, *, id: int) -> None:
92+
await self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": id})
93+
94+
async def get_book(self, *, id: int) -> Optional[models.Book]:
95+
row = (await self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": id})).first()
96+
if row is None:
97+
return None
98+
return models.Book(
99+
id=row[0],
100+
title=row[1],
101+
status=row[2],
102+
)
103+
104+
async def list_books(self) -> AsyncIterator[models.Book]:
105+
result = await self._conn.stream(sqlalchemy.text(LIST_BOOKS))
106+
async for row in result:
107+
yield models.Book(
108+
id=row[0],
109+
title=row[1],
110+
status=row[2],
111+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- name: GetBook :one
2+
SELECT * FROM books
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListBooks :many
6+
SELECT * FROM books
7+
ORDER BY title;
8+
9+
-- name: CreateBook :one
10+
INSERT INTO books (
11+
title, status
12+
) VALUES (
13+
$1, $2
14+
) RETURNING *;
15+
16+
-- name: DeleteBook :exec
17+
DELETE FROM books
18+
WHERE id = $1;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TYPE book_status AS ENUM ('available', 'checked_out', 'overdue');
2+
3+
4+
CREATE TABLE books (
5+
id BIGSERIAL PRIMARY KEY,
6+
title text NOT NULL,
7+
status book_status DEFAULT 'available'
8+
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "2"
2+
plugins:
3+
- name: py
4+
wasm:
5+
url: file://../../../../bin/sqlc-gen-python.wasm
6+
sha256: "d6846ffad948181e611e883cedd2d2be66e091edc1273a0abc6c9da18399e0ca"
7+
sql:
8+
- schema: schema.sql
9+
queries: query.sql
10+
engine: postgresql
11+
codegen:
12+
- plugin: py
13+
out: db
14+
options:
15+
package: db
16+
emit_sync_querier: true
17+
emit_async_querier: true
18+
emit_str_enum: true
19+

0 commit comments

Comments
 (0)