Skip to content

Commit acd5a58

Browse files
committed
update to match the tutorial
1 parent bd39112 commit acd5a58

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

examples/prod_fastapi_demo/migrations/env.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
# This line sets up loggers basically.
2828
fileConfig(config.config_file_name)
2929

30-
config.set_main_option(
31-
"sqlalchemy.url", str(DB_DSN),
32-
)
30+
config.set_main_option("sqlalchemy.url", str(DB_DSN))
3331

3432

3533
def run_migrations_offline():

examples/prod_fastapi_demo/src/gino_fastapi_demo/main.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
2-
import click
32

3+
import click
44
from fastapi import FastAPI
55

66
from .models import db
@@ -14,8 +14,13 @@
1414

1515

1616
def get_app():
17-
app = FastAPI()
17+
app = FastAPI(title="GINO FastAPI Demo")
1818
db.init_app(app)
19+
load_modules(app)
20+
return app
21+
22+
23+
def load_modules(app=None):
1924
for ep in entry_points()["gino_fastapi_demo.modules"]:
2025
logger.info(
2126
"Loading module: %s",
@@ -26,7 +31,7 @@ def get_app():
2631
},
2732
)
2833
mod = ep.load()
29-
init_app = getattr(mod, "init_app", None)
30-
if init_app:
31-
init_app(app)
32-
return app
34+
if app:
35+
init_app = getattr(mod, "init_app", None)
36+
if init_app:
37+
init_app(app)

examples/prod_fastapi_demo/src/gino_fastapi_demo/models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from gino_starlette import Gino
1+
from gino.ext.starlette import Gino
22

33
from .. import config
44

examples/prod_fastapi_demo/src/gino_fastapi_demo/views/users.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ class UserModel(BaseModel):
1212

1313
@router.get("/users/{uid}")
1414
async def get_user(uid: int):
15-
q = User.query.where(User.id == uid)
16-
return (await q.gino.first_or_404()).to_dict()
15+
user = await User.get_or_404(uid)
16+
return user.to_dict()
1717

1818

1919
@router.post("/users")
2020
async def add_user(user: UserModel):
21-
u = await User.create(nickname=user.name)
22-
return u.to_dict()
21+
rv = await User.create(nickname=user.name)
22+
return rv.to_dict()
23+
24+
25+
@router.delete("/users/{uid}")
26+
async def delete_user(uid: int):
27+
user = await User.get_or_404(uid)
28+
await user.delete()
29+
return dict(id=uid)
2330

2431

2532
def init_app(app):

examples/prod_fastapi_demo/tests/test_users.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33

44
def test_crud(client):
5-
assert client.get("/users/1").status_code == 404
5+
# create
66
nickname = str(uuid.uuid4())
77
r = client.post("/users", json=dict(name=nickname))
88
r.raise_for_status()
9-
r = r.json()
10-
assert (
11-
client.get("/users/{}".format(r["id"])).json()["nickname"] == nickname
12-
)
9+
10+
# retrieve
11+
url = "/users/{}".format(r.json()['id'])
12+
assert client.get(url).json()["nickname"] == nickname
13+
14+
# delete
15+
client.delete(url).raise_for_status()
16+
assert client.get(url).status_code == 404

0 commit comments

Comments
 (0)