Skip to content

Commit 31bed06

Browse files
committed
Update sanic example
1 parent e670377 commit 31bed06

File tree

7 files changed

+58
-51
lines changed

7 files changed

+58
-51
lines changed

examples/miniapps/sanic/README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ The output should be something like:
9898

9999
.. code-block::
100100
101-
platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
102-
plugins: cov-2.10.0, sanic-1.6.1
101+
platform darwin -- Python 3.9.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
102+
plugins: cov-2.12.1, sanic-1.8.1, anyio-3.3.2
103103
collected 3 items
104104
105105
giphynavigator/tests.py ... [100%]
@@ -114,6 +114,6 @@ The output should be something like:
114114
giphynavigator/giphy.py 14 9 36%
115115
giphynavigator/handlers.py 11 0 100%
116116
giphynavigator/services.py 9 1 89%
117-
giphynavigator/tests.py 34 0 100%
117+
giphynavigator/tests.py 39 0 100%
118118
---------------------------------------------------
119-
TOTAL 90 14 84%
119+
TOTAL 95 14 85%

examples/miniapps/sanic/giphynavigator/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
from .application import create_app
44

55

6-
if __name__ == '__main__':
6+
if __name__ == "__main__":
77
app = create_app()
8-
app.run(host='0.0.0.0', port=8000, debug=True)
8+
app.run(host="0.0.0.0", port=8000, debug=True)

examples/miniapps/sanic/giphynavigator/application.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
def create_app() -> Sanic:
1010
"""Create and return Sanic application."""
1111
container = Container()
12-
container.config.from_yaml('config.yml')
13-
container.config.giphy.api_key.from_env('GIPHY_API_KEY')
12+
container.config.from_yaml("config.yml")
13+
container.config.giphy.api_key.from_env("GIPHY_API_KEY")
1414
container.wire(modules=[handlers])
1515

16-
app = Sanic('Giphy Navigator')
17-
app.container = container
18-
app.add_route(handlers.index, '/')
16+
app = Sanic("giphy-navigator")
17+
app.ctx.container = container
18+
app.add_route(handlers.index, "/")
1919
return app

examples/miniapps/sanic/giphynavigator/giphy.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55

66
class GiphyClient:
77

8-
API_URL = 'https://api.giphy.com/v1'
8+
API_URL = "https://api.giphy.com/v1"
99

1010
def __init__(self, api_key, timeout):
1111
self._api_key = api_key
1212
self._timeout = ClientTimeout(timeout)
1313

1414
async def search(self, query, limit):
1515
"""Make search API call and return result."""
16-
url = f'{self.API_URL}/gifs/search'
16+
url = f"{self.API_URL}/gifs/search"
1717
params = {
18-
'q': query,
19-
'api_key': self._api_key,
20-
'limit': limit,
18+
"q": query,
19+
"api_key": self._api_key,
20+
"limit": limit,
2121
}
2222
async with ClientSession(timeout=self._timeout) as session:
2323
async with session.get(url, params=params) as response:

examples/miniapps/sanic/giphynavigator/handlers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ async def index(
1515
default_query: str = Provide[Container.config.default.query],
1616
default_limit: int = Provide[Container.config.default.limit.as_int()],
1717
) -> HTTPResponse:
18-
query = request.args.get('query', default_query)
19-
limit = int(request.args.get('limit', default_limit))
18+
query = request.args.get("query", default_query)
19+
limit = int(request.args.get("limit", default_limit))
2020

2121
gifs = await search_service.search(query, limit)
2222

2323
return json(
2424
{
25-
'query': query,
26-
'limit': limit,
27-
'gifs': gifs,
25+
"query": query,
26+
"limit": limit,
27+
"gifs": gifs,
2828
},
2929
)

examples/miniapps/sanic/giphynavigator/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ async def search(self, query, limit):
1515

1616
result = await self._giphy_client.search(query, limit)
1717

18-
return [{'url': gif['url']} for gif in result['data']]
18+
return [{"url": gif["url"]} for gif in result["data"]]

examples/miniapps/sanic/giphynavigator/tests.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,79 @@
33
from unittest import mock
44

55
import pytest
6+
from sanic import Sanic
67

78
from giphynavigator.application import create_app
89
from giphynavigator.giphy import GiphyClient
910

1011

1112
@pytest.fixture
1213
def app():
14+
Sanic.test_mode = True
1315
app = create_app()
1416
yield app
15-
app.container.unwire()
17+
app.ctx.container.unwire()
1618

1719

18-
async def test_index(app):
20+
@pytest.fixture
21+
def test_client(loop, app, sanic_client):
22+
return loop.run_until_complete(sanic_client(app))
23+
24+
25+
async def test_index(app, test_client):
1926
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
2027
giphy_client_mock.search.return_value = {
21-
'data': [
22-
{'url': 'https://giphy.com/gif1.gif'},
23-
{'url': 'https://giphy.com/gif2.gif'},
28+
"data": [
29+
{"url": "https://giphy.com/gif1.gif"},
30+
{"url": "https://giphy.com/gif2.gif"},
2431
],
2532
}
2633

27-
with app.container.giphy_client.override(giphy_client_mock):
28-
_, response = await app.asgi_client.get(
29-
'/',
34+
with app.ctx.container.giphy_client.override(giphy_client_mock):
35+
response = await test_client.get(
36+
"/",
3037
params={
31-
'query': 'test',
32-
'limit': 10,
38+
"query": "test",
39+
"limit": 10,
3340
},
3441
)
3542

36-
assert response.status == 200
43+
assert response.status_code == 200
3744
data = response.json()
3845
assert data == {
39-
'query': 'test',
40-
'limit': 10,
41-
'gifs': [
42-
{'url': 'https://giphy.com/gif1.gif'},
43-
{'url': 'https://giphy.com/gif2.gif'},
46+
"query": "test",
47+
"limit": 10,
48+
"gifs": [
49+
{"url": "https://giphy.com/gif1.gif"},
50+
{"url": "https://giphy.com/gif2.gif"},
4451
],
4552
}
4653

4754

48-
async def test_index_no_data(app):
55+
async def test_index_no_data(app, test_client):
4956
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
5057
giphy_client_mock.search.return_value = {
51-
'data': [],
58+
"data": [],
5259
}
5360

54-
with app.container.giphy_client.override(giphy_client_mock):
55-
_, response = await app.asgi_client.get('/')
61+
with app.ctx.container.giphy_client.override(giphy_client_mock):
62+
response = await test_client.get("/")
5663

57-
assert response.status == 200
64+
assert response.status_code == 200
5865
data = response.json()
59-
assert data['gifs'] == []
66+
assert data["gifs"] == []
6067

6168

62-
async def test_index_default_params(app):
69+
async def test_index_default_params(app, test_client):
6370
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
6471
giphy_client_mock.search.return_value = {
65-
'data': [],
72+
"data": [],
6673
}
6774

68-
with app.container.giphy_client.override(giphy_client_mock):
69-
_, response = await app.asgi_client.get('/')
75+
with app.ctx.container.giphy_client.override(giphy_client_mock):
76+
response = await test_client.get("/")
7077

71-
assert response.status == 200
78+
assert response.status_code == 200
7279
data = response.json()
73-
assert data['query'] == app.container.config.default.query()
74-
assert data['limit'] == app.container.config.default.limit()
80+
assert data["query"] == app.ctx.container.config.default.query()
81+
assert data["limit"] == app.ctx.container.config.default.limit()

0 commit comments

Comments
 (0)