2020-07-29 02:19:05 +03:00
|
|
|
"""Tests module."""
|
|
|
|
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from giphynavigator.application import create_app
|
|
|
|
from giphynavigator.giphy import GiphyClient
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def app():
|
2020-10-09 22:16:27 +03:00
|
|
|
app = create_app()
|
|
|
|
yield app
|
|
|
|
app.container.unwire()
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def client(app, aiohttp_client, loop):
|
|
|
|
return loop.run_until_complete(aiohttp_client(app))
|
|
|
|
|
|
|
|
|
|
|
|
async def test_index(client, app):
|
|
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
|
|
giphy_client_mock.search.return_value = {
|
2021-09-30 22:37:21 +03:00
|
|
|
"data": [
|
|
|
|
{"url": "https://giphy.com/gif1.gif"},
|
|
|
|
{"url": "https://giphy.com/gif2.gif"},
|
2020-07-29 02:19:05 +03:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
|
|
|
response = await client.get(
|
2021-09-30 22:37:21 +03:00
|
|
|
"/",
|
2020-07-29 02:19:05 +03:00
|
|
|
params={
|
2021-09-30 22:37:21 +03:00
|
|
|
"query": "test",
|
|
|
|
"limit": 10,
|
2020-07-29 02:19:05 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert response.status == 200
|
|
|
|
data = await response.json()
|
|
|
|
assert data == {
|
2021-09-30 22:37:21 +03:00
|
|
|
"query": "test",
|
|
|
|
"limit": 10,
|
|
|
|
"gifs": [
|
|
|
|
{"url": "https://giphy.com/gif1.gif"},
|
|
|
|
{"url": "https://giphy.com/gif2.gif"},
|
2020-07-29 02:19:05 +03:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_index_no_data(client, app):
|
|
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
|
|
giphy_client_mock.search.return_value = {
|
2021-09-30 22:37:21 +03:00
|
|
|
"data": [],
|
2020-07-29 02:19:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
2021-09-30 22:37:21 +03:00
|
|
|
response = await client.get("/")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
assert response.status == 200
|
|
|
|
data = await response.json()
|
2021-09-30 22:37:21 +03:00
|
|
|
assert data["gifs"] == []
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
async def test_index_default_params(client, app):
|
|
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
|
|
giphy_client_mock.search.return_value = {
|
2021-09-30 22:37:21 +03:00
|
|
|
"data": [],
|
2020-07-29 02:19:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
2021-09-30 22:37:21 +03:00
|
|
|
response = await client.get("/")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
assert response.status == 200
|
|
|
|
data = await response.json()
|
2021-09-30 22:37:21 +03:00
|
|
|
assert data["query"] == app.container.config.default.query()
|
|
|
|
assert data["limit"] == app.container.config.default.limit()
|