mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-21 17:16:46 +03:00
Update sanic example
This commit is contained in:
parent
e670377bb3
commit
31bed0651f
|
@ -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%]
|
||||
|
@ -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%
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -9,11 +9,11 @@ from . import handlers
|
|||
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
|
||||
|
|
|
@ -5,7 +5,7 @@ from aiohttp import ClientSession, ClientTimeout
|
|||
|
||||
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
|
||||
|
@ -13,11 +13,11 @@ class GiphyClient:
|
|||
|
||||
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:
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
)
|
||||
|
|
|
@ -15,4 +15,4 @@ class SearchService:
|
|||
|
||||
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"]]
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
from sanic import Sanic
|
||||
|
||||
from giphynavigator.application import create_app
|
||||
from giphynavigator.giphy import GiphyClient
|
||||
|
@ -10,65 +11,71 @@ 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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user