python-dependency-injector/examples/miniapps/fastapi/giphynavigator/tests.py

78 lines
2.0 KiB
Python
Raw Normal View History

2020-11-13 01:48:07 +03:00
"""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
2020-11-13 01:48:07 +03:00
@pytest.mark.asyncio
async def test_index(client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = {
2021-09-30 23:55:50 +03:00
"data": [
{"url": "https://giphy.com/gif1.gif"},
{"url": "https://giphy.com/gif2.gif"},
2020-11-13 01:48:07 +03:00
],
}
with app.container.giphy_client.override(giphy_client_mock):
response = await client.get(
2021-09-30 23:55:50 +03:00
"/",
2020-11-13 01:48:07 +03:00
params={
2021-09-30 23:55:50 +03:00
"query": "test",
"limit": 10,
2020-11-13 01:48:07 +03:00
},
)
assert response.status_code == 200
data = response.json()
assert data == {
2021-09-30 23:55:50 +03:00
"query": "test",
"limit": 10,
"gifs": [
{"url": "https://giphy.com/gif1.gif"},
{"url": "https://giphy.com/gif2.gif"},
2020-11-13 01:48:07 +03:00
],
}
@pytest.mark.asyncio
async def test_index_no_data(client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = {
2021-09-30 23:55:50 +03:00
"data": [],
2020-11-13 01:48:07 +03:00
}
with app.container.giphy_client.override(giphy_client_mock):
2021-09-30 23:55:50 +03:00
response = await client.get("/")
2020-11-13 01:48:07 +03:00
assert response.status_code == 200
data = response.json()
2021-09-30 23:55:50 +03:00
assert data["gifs"] == []
2020-11-13 01:48:07 +03:00
@pytest.mark.asyncio
async def test_index_default_params(client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = {
2021-09-30 23:55:50 +03:00
"data": [],
2020-11-13 01:48:07 +03:00
}
with app.container.giphy_client.override(giphy_client_mock):
2021-09-30 23:55:50 +03:00
response = await client.get("/")
2020-11-13 01:48:07 +03:00
assert response.status_code == 200
data = response.json()
2021-09-30 23:55:50 +03:00
assert data["query"] == app.container.config.default.query()
assert data["limit"] == app.container.config.default.limit()