Skip to content

Commit

Permalink
Update sanic example
Browse files Browse the repository at this point in the history
  • Loading branch information
rmk135 committed Sep 30, 2021
1 parent e670377 commit 31bed06
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 51 deletions.
8 changes: 4 additions & 4 deletions examples/miniapps/sanic/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ The output should be something like:

.. code-block::
platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
plugins: cov-2.10.0, sanic-1.6.1
platform darwin -- Python 3.9.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
plugins: cov-2.12.1, sanic-1.8.1, anyio-3.3.2
collected 3 items
giphynavigator/tests.py ... [100%]
Expand All @@ -114,6 +114,6 @@ The output should be something like:
giphynavigator/giphy.py 14 9 36%
giphynavigator/handlers.py 11 0 100%
giphynavigator/services.py 9 1 89%
giphynavigator/tests.py 34 0 100%
giphynavigator/tests.py 39 0 100%
---------------------------------------------------
TOTAL 90 14 84%
TOTAL 95 14 85%
4 changes: 2 additions & 2 deletions examples/miniapps/sanic/giphynavigator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from .application import create_app


if __name__ == '__main__':
if __name__ == "__main__":
app = create_app()
app.run(host='0.0.0.0', port=8000, debug=True)
app.run(host="0.0.0.0", port=8000, debug=True)
10 changes: 5 additions & 5 deletions examples/miniapps/sanic/giphynavigator/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
def create_app() -> Sanic:
"""Create and return Sanic application."""
container = Container()
container.config.from_yaml('config.yml')
container.config.giphy.api_key.from_env('GIPHY_API_KEY')
container.config.from_yaml("config.yml")
container.config.giphy.api_key.from_env("GIPHY_API_KEY")
container.wire(modules=[handlers])

app = Sanic('Giphy Navigator')
app.container = container
app.add_route(handlers.index, '/')
app = Sanic("giphy-navigator")
app.ctx.container = container
app.add_route(handlers.index, "/")
return app
10 changes: 5 additions & 5 deletions examples/miniapps/sanic/giphynavigator/giphy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

class GiphyClient:

API_URL = 'https://api.giphy.com/v1'
API_URL = "https://api.giphy.com/v1"

def __init__(self, api_key, timeout):
self._api_key = api_key
self._timeout = ClientTimeout(timeout)

async def search(self, query, limit):
"""Make search API call and return result."""
url = f'{self.API_URL}/gifs/search'
url = f"{self.API_URL}/gifs/search"
params = {
'q': query,
'api_key': self._api_key,
'limit': limit,
"q": query,
"api_key": self._api_key,
"limit": limit,
}
async with ClientSession(timeout=self._timeout) as session:
async with session.get(url, params=params) as response:
Expand Down
10 changes: 5 additions & 5 deletions examples/miniapps/sanic/giphynavigator/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ async def index(
default_query: str = Provide[Container.config.default.query],
default_limit: int = Provide[Container.config.default.limit.as_int()],
) -> HTTPResponse:
query = request.args.get('query', default_query)
limit = int(request.args.get('limit', default_limit))
query = request.args.get("query", default_query)
limit = int(request.args.get("limit", default_limit))

gifs = await search_service.search(query, limit)

return json(
{
'query': query,
'limit': limit,
'gifs': gifs,
"query": query,
"limit": limit,
"gifs": gifs,
},
)
2 changes: 1 addition & 1 deletion examples/miniapps/sanic/giphynavigator/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ async def search(self, query, limit):

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

return [{'url': gif['url']} for gif in result['data']]
return [{"url": gif["url"]} for gif in result["data"]]
65 changes: 36 additions & 29 deletions examples/miniapps/sanic/giphynavigator/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,79 @@
from unittest import mock

import pytest
from sanic import Sanic

from giphynavigator.application import create_app
from giphynavigator.giphy import GiphyClient


@pytest.fixture
def app():
Sanic.test_mode = True
app = create_app()
yield app
app.container.unwire()
app.ctx.container.unwire()


async def test_index(app):
@pytest.fixture
def test_client(loop, app, sanic_client):
return loop.run_until_complete(sanic_client(app))


async def test_index(app, test_client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = {
'data': [
{'url': 'https://giphy.com/gif1.gif'},
{'url': 'https://giphy.com/gif2.gif'},
"data": [
{"url": "https://giphy.com/gif1.gif"},
{"url": "https://giphy.com/gif2.gif"},
],
}

with app.container.giphy_client.override(giphy_client_mock):
_, response = await app.asgi_client.get(
'/',
with app.ctx.container.giphy_client.override(giphy_client_mock):
response = await test_client.get(
"/",
params={
'query': 'test',
'limit': 10,
"query": "test",
"limit": 10,
},
)

assert response.status == 200
assert response.status_code == 200
data = response.json()
assert data == {
'query': 'test',
'limit': 10,
'gifs': [
{'url': 'https://giphy.com/gif1.gif'},
{'url': 'https://giphy.com/gif2.gif'},
"query": "test",
"limit": 10,
"gifs": [
{"url": "https://giphy.com/gif1.gif"},
{"url": "https://giphy.com/gif2.gif"},
],
}


async def test_index_no_data(app):
async def test_index_no_data(app, test_client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = {
'data': [],
"data": [],
}

with app.container.giphy_client.override(giphy_client_mock):
_, response = await app.asgi_client.get('/')
with app.ctx.container.giphy_client.override(giphy_client_mock):
response = await test_client.get("/")

assert response.status == 200
assert response.status_code == 200
data = response.json()
assert data['gifs'] == []
assert data["gifs"] == []


async def test_index_default_params(app):
async def test_index_default_params(app, test_client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = {
'data': [],
"data": [],
}

with app.container.giphy_client.override(giphy_client_mock):
_, response = await app.asgi_client.get('/')
with app.ctx.container.giphy_client.override(giphy_client_mock):
response = await test_client.get("/")

assert response.status == 200
assert response.status_code == 200
data = response.json()
assert data['query'] == app.container.config.default.query()
assert data['limit'] == app.container.config.default.limit()
assert data["query"] == app.ctx.container.config.default.query()
assert data["limit"] == app.ctx.container.config.default.limit()

0 comments on commit 31bed06

Please sign in to comment.