mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
fe01ad41d9
* Update application-single-container example * Update application-multiple-containers example * Update decoupled-packages example * Update movie lister example * Update CLI tutorial * Update sanic example * Update sanic example with wiring_config * Update fastapi example * Update fastapi-simple example * Update fastapi-sqlalchemy example * Update flask-blueprints example * Update flask example and tutorial * Update aiohttp example and tutorial * Update asyncio-daemon example and tutorial
78 lines
2.0 KiB
Python
78 lines
2.0 KiB
Python
"""Tests module."""
|
|
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
|
|
from giphynavigator.application import app
|
|
from giphynavigator.giphy import GiphyClient
|
|
|
|
|
|
@pytest.fixture
|
|
async def client():
|
|
async with AsyncClient(app=app, base_url="http://test") as client:
|
|
yield client
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_index(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"},
|
|
],
|
|
}
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
|
response = await client.get(
|
|
"/",
|
|
params={
|
|
"query": "test",
|
|
"limit": 10,
|
|
},
|
|
)
|
|
|
|
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"},
|
|
],
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_index_no_data(client):
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
giphy_client_mock.search.return_value = {
|
|
"data": [],
|
|
}
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
|
response = await client.get("/")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["gifs"] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_index_default_params(client):
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
giphy_client_mock.search.return_value = {
|
|
"data": [],
|
|
}
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
|
response = await client.get("/")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["query"] == app.container.config.default.query()
|
|
assert data["limit"] == app.container.config.default.limit()
|