mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Update quotes in fastapi example
This commit is contained in:
parent
a9173496b4
commit
e670377bb3
|
@ -8,8 +8,8 @@ from . import endpoints
|
||||||
|
|
||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
container = Container()
|
container = Container()
|
||||||
container.config.from_yaml('config.yml')
|
container.config.from_yaml("config.yml")
|
||||||
container.config.giphy.api_key.from_env('GIPHY_API_KEY')
|
container.config.giphy.api_key.from_env("GIPHY_API_KEY")
|
||||||
container.wire(modules=[endpoints])
|
container.wire(modules=[endpoints])
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
|
@ -23,7 +23,7 @@ class Response(BaseModel):
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get('/', response_model=Response)
|
@router.get("/", response_model=Response)
|
||||||
@inject
|
@inject
|
||||||
async def index(
|
async def index(
|
||||||
query: Optional[str] = None,
|
query: Optional[str] = None,
|
||||||
|
@ -38,7 +38,7 @@ async def index(
|
||||||
gifs = await search_service.search(query, limit)
|
gifs = await search_service.search(query, limit)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'query': query,
|
"query": query,
|
||||||
'limit': limit,
|
"limit": limit,
|
||||||
'gifs': gifs,
|
"gifs": gifs,
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ from aiohttp import ClientSession, ClientTimeout
|
||||||
|
|
||||||
class GiphyClient:
|
class GiphyClient:
|
||||||
|
|
||||||
API_URL = 'https://api.giphy.com/v1'
|
API_URL = "https://api.giphy.com/v1"
|
||||||
|
|
||||||
def __init__(self, api_key, timeout):
|
def __init__(self, api_key, timeout):
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
|
@ -13,11 +13,11 @@ class GiphyClient:
|
||||||
|
|
||||||
async def search(self, query, limit):
|
async def search(self, query, limit):
|
||||||
"""Make search API call and return result."""
|
"""Make search API call and return result."""
|
||||||
url = f'{self.API_URL}/gifs/search'
|
url = f"{self.API_URL}/gifs/search"
|
||||||
params = {
|
params = {
|
||||||
'q': query,
|
"q": query,
|
||||||
'api_key': self._api_key,
|
"api_key": self._api_key,
|
||||||
'limit': limit,
|
"limit": limit,
|
||||||
}
|
}
|
||||||
async with ClientSession(timeout=self._timeout) as session:
|
async with ClientSession(timeout=self._timeout) as session:
|
||||||
async with session.get(url, params=params) as response:
|
async with session.get(url, params=params) as response:
|
||||||
|
|
|
@ -15,4 +15,4 @@ class SearchService:
|
||||||
|
|
||||||
result = await self._giphy_client.search(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"]]
|
||||||
|
|
|
@ -11,7 +11,7 @@ from giphynavigator.giphy import GiphyClient
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def client(event_loop):
|
def client(event_loop):
|
||||||
client = AsyncClient(app=app, base_url='http://test')
|
client = AsyncClient(app=app, base_url="http://test")
|
||||||
yield client
|
yield client
|
||||||
event_loop.run_until_complete(client.aclose())
|
event_loop.run_until_complete(client.aclose())
|
||||||
|
|
||||||
|
@ -20,29 +20,29 @@ def client(event_loop):
|
||||||
async def test_index(client):
|
async def test_index(client):
|
||||||
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
||||||
giphy_client_mock.search.return_value = {
|
giphy_client_mock.search.return_value = {
|
||||||
'data': [
|
"data": [
|
||||||
{'url': 'https://giphy.com/gif1.gif'},
|
{"url": "https://giphy.com/gif1.gif"},
|
||||||
{'url': 'https://giphy.com/gif2.gif'},
|
{"url": "https://giphy.com/gif2.gif"},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
with app.container.giphy_client.override(giphy_client_mock):
|
with app.container.giphy_client.override(giphy_client_mock):
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
'/',
|
"/",
|
||||||
params={
|
params={
|
||||||
'query': 'test',
|
"query": "test",
|
||||||
'limit': 10,
|
"limit": 10,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data == {
|
assert data == {
|
||||||
'query': 'test',
|
"query": "test",
|
||||||
'limit': 10,
|
"limit": 10,
|
||||||
'gifs': [
|
"gifs": [
|
||||||
{'url': 'https://giphy.com/gif1.gif'},
|
{"url": "https://giphy.com/gif1.gif"},
|
||||||
{'url': 'https://giphy.com/gif2.gif'},
|
{"url": "https://giphy.com/gif2.gif"},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,28 +51,28 @@ async def test_index(client):
|
||||||
async def test_index_no_data(client):
|
async def test_index_no_data(client):
|
||||||
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
||||||
giphy_client_mock.search.return_value = {
|
giphy_client_mock.search.return_value = {
|
||||||
'data': [],
|
"data": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
with app.container.giphy_client.override(giphy_client_mock):
|
with app.container.giphy_client.override(giphy_client_mock):
|
||||||
response = await client.get('/')
|
response = await client.get("/")
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data['gifs'] == []
|
assert data["gifs"] == []
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_index_default_params(client):
|
async def test_index_default_params(client):
|
||||||
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
||||||
giphy_client_mock.search.return_value = {
|
giphy_client_mock.search.return_value = {
|
||||||
'data': [],
|
"data": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
with app.container.giphy_client.override(giphy_client_mock):
|
with app.container.giphy_client.override(giphy_client_mock):
|
||||||
response = await client.get('/')
|
response = await client.get("/")
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data['query'] == app.container.config.default.query()
|
assert data["query"] == app.container.config.default.query()
|
||||||
assert data['limit'] == app.container.config.default.limit()
|
assert data["limit"] == app.container.config.default.limit()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user