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
|
|
|
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
async def test_index(app):
|
2020-07-29 02:19:05 +03:00
|
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
|
|
giphy_client_mock.search.return_value = {
|
|
|
|
'data': [
|
2020-07-30 05:17:56 +03:00
|
|
|
{'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):
|
2020-10-09 22:16:27 +03:00
|
|
|
_, response = await app.asgi_client.get(
|
2020-07-29 02:19:05 +03:00
|
|
|
'/',
|
|
|
|
params={
|
|
|
|
'query': 'test',
|
|
|
|
'limit': 10,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert response.status == 200
|
2020-10-09 22:16:27 +03:00
|
|
|
data = response.json()
|
2020-07-29 02:19:05 +03:00
|
|
|
assert data == {
|
|
|
|
'query': 'test',
|
|
|
|
'limit': 10,
|
|
|
|
'gifs': [
|
2020-07-30 05:17:56 +03:00
|
|
|
{'url': 'https://giphy.com/gif1.gif'},
|
|
|
|
{'url': 'https://giphy.com/gif2.gif'},
|
2020-07-29 02:19:05 +03:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
async def test_index_no_data(app):
|
2020-07-29 02:19:05 +03:00
|
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
|
|
giphy_client_mock.search.return_value = {
|
|
|
|
'data': [],
|
|
|
|
}
|
|
|
|
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
2020-10-09 22:16:27 +03:00
|
|
|
_, response = await app.asgi_client.get('/')
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
assert response.status == 200
|
2020-10-09 22:16:27 +03:00
|
|
|
data = response.json()
|
2020-07-29 02:19:05 +03:00
|
|
|
assert data['gifs'] == []
|
|
|
|
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
async def test_index_default_params(app):
|
2020-07-29 02:19:05 +03:00
|
|
|
giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
|
|
|
|
giphy_client_mock.search.return_value = {
|
|
|
|
'data': [],
|
|
|
|
}
|
|
|
|
|
|
|
|
with app.container.giphy_client.override(giphy_client_mock):
|
2020-10-09 22:16:27 +03:00
|
|
|
_, response = await app.asgi_client.get('/')
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
assert response.status == 200
|
2020-10-09 22:16:27 +03:00
|
|
|
data = response.json()
|
|
|
|
assert data['query'] == app.container.config.default.query()
|
|
|
|
assert data['limit'] == app.container.config.default.limit()
|