Skip to content

Commit b2093f1

Browse files
committed
Added aiohttp server support
1 parent 743f901 commit b2093f1

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

gino/ext/sanic.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,18 @@ class Gino(_Gino):
6464
"""Support Sanic web server.
6565
6666
By :meth:`init_app` GINO registers a few hooks on Sanic, so that GINO could
67-
use database configuration in Sanic `config` to initialize the bound pool.
67+
use database configuration in Sanic ``config`` to initialize the bound
68+
engine.
6869
6970
A lazy connection context is enabled by default for every request. You can
70-
change this default behavior by setting `DB_USE_CONNECTION_FOR_REQUEST`
71-
config value to `False`. By default, a database connection is borrowed on
71+
change this default behavior by setting ``DB_USE_CONNECTION_FOR_REQUEST``
72+
config value to ``False``. By default, a database connection is borrowed on
7273
the first query, shared in the same execution context, and returned to the
7374
pool on response. If you need to release the connection early in the middle
74-
to do some long-running tasks, you can simply do this:
75+
to do some long-running tasks, you can simply do this::
7576
7677
await request['connection'].release(permanent=False)
7778
78-
Here `request['connection']` is a :class:`LazyConnection` object, see its
79-
doc string for more information.
8079
"""
8180
model_base_classes = _Gino.model_base_classes + (SanicModelMixin,)
8281
query_executor = GinoExecutor

tests/test_sanic.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ async def root(request):
3131

3232
@app.route('/users/<uid:int>')
3333
async def get_user(request, uid):
34-
return json((await User.get_or_404(uid)).to_dict())
34+
method = request.args.get('method')
35+
q = User.query.where(User.id == uid)
36+
if method == '1':
37+
return json((await q.gino.first_or_404()).to_dict())
38+
elif method == '2':
39+
return json(
40+
(await request['connection'].first_or_404(q)).to_dict())
41+
elif method == '3':
42+
return json(
43+
(await db.bind.first_or_404(q)).to_dict())
44+
elif method == '4':
45+
return json(
46+
(await db.first_or_404(q)).to_dict())
47+
else:
48+
return json((await User.get_or_404(uid)).to_dict())
3549

3650
@app.route('/users', methods=['POST'])
3751
async def add_user(request):
@@ -63,6 +77,10 @@ def test(app):
6377
request, response = app.test_client.get('/users/1')
6478
assert response.status == 404
6579

80+
for method in '1234':
81+
request, response = app.test_client.get(f'/users/1?method={method}')
82+
assert response.status == 404
83+
6684
request, response = app.test_client.post('/users',
6785
data=dict(name='fantix'))
6886
assert response.status == 200

0 commit comments

Comments
 (0)