|
3 | 3 | from unittest import mock |
4 | 4 |
|
5 | 5 | import pytest |
| 6 | +from sanic import Sanic |
6 | 7 |
|
7 | 8 | from giphynavigator.application import create_app |
8 | 9 | from giphynavigator.giphy import GiphyClient |
9 | 10 |
|
10 | 11 |
|
11 | 12 | @pytest.fixture |
12 | 13 | def app(): |
| 14 | + Sanic.test_mode = True |
13 | 15 | app = create_app() |
14 | 16 | yield app |
15 | | - app.container.unwire() |
| 17 | + app.ctx.container.unwire() |
16 | 18 |
|
17 | 19 |
|
18 | | -async def test_index(app): |
| 20 | +@pytest.fixture |
| 21 | +def test_client(loop, app, sanic_client): |
| 22 | + return loop.run_until_complete(sanic_client(app)) |
| 23 | + |
| 24 | + |
| 25 | +async def test_index(app, test_client): |
19 | 26 | giphy_client_mock = mock.AsyncMock(spec=GiphyClient) |
20 | 27 | giphy_client_mock.search.return_value = { |
21 | | - 'data': [ |
22 | | - {'url': 'https://giphy.com/gif1.gif'}, |
23 | | - {'url': 'https://giphy.com/gif2.gif'}, |
| 28 | + "data": [ |
| 29 | + {"url": "https://giphy.com/gif1.gif"}, |
| 30 | + {"url": "https://giphy.com/gif2.gif"}, |
24 | 31 | ], |
25 | 32 | } |
26 | 33 |
|
27 | | - with app.container.giphy_client.override(giphy_client_mock): |
28 | | - _, response = await app.asgi_client.get( |
29 | | - '/', |
| 34 | + with app.ctx.container.giphy_client.override(giphy_client_mock): |
| 35 | + response = await test_client.get( |
| 36 | + "/", |
30 | 37 | params={ |
31 | | - 'query': 'test', |
32 | | - 'limit': 10, |
| 38 | + "query": "test", |
| 39 | + "limit": 10, |
33 | 40 | }, |
34 | 41 | ) |
35 | 42 |
|
36 | | - assert response.status == 200 |
| 43 | + assert response.status_code == 200 |
37 | 44 | data = response.json() |
38 | 45 | assert data == { |
39 | | - 'query': 'test', |
40 | | - 'limit': 10, |
41 | | - 'gifs': [ |
42 | | - {'url': 'https://giphy.com/gif1.gif'}, |
43 | | - {'url': 'https://giphy.com/gif2.gif'}, |
| 46 | + "query": "test", |
| 47 | + "limit": 10, |
| 48 | + "gifs": [ |
| 49 | + {"url": "https://giphy.com/gif1.gif"}, |
| 50 | + {"url": "https://giphy.com/gif2.gif"}, |
44 | 51 | ], |
45 | 52 | } |
46 | 53 |
|
47 | 54 |
|
48 | | -async def test_index_no_data(app): |
| 55 | +async def test_index_no_data(app, test_client): |
49 | 56 | giphy_client_mock = mock.AsyncMock(spec=GiphyClient) |
50 | 57 | giphy_client_mock.search.return_value = { |
51 | | - 'data': [], |
| 58 | + "data": [], |
52 | 59 | } |
53 | 60 |
|
54 | | - with app.container.giphy_client.override(giphy_client_mock): |
55 | | - _, response = await app.asgi_client.get('/') |
| 61 | + with app.ctx.container.giphy_client.override(giphy_client_mock): |
| 62 | + response = await test_client.get("/") |
56 | 63 |
|
57 | | - assert response.status == 200 |
| 64 | + assert response.status_code == 200 |
58 | 65 | data = response.json() |
59 | | - assert data['gifs'] == [] |
| 66 | + assert data["gifs"] == [] |
60 | 67 |
|
61 | 68 |
|
62 | | -async def test_index_default_params(app): |
| 69 | +async def test_index_default_params(app, test_client): |
63 | 70 | giphy_client_mock = mock.AsyncMock(spec=GiphyClient) |
64 | 71 | giphy_client_mock.search.return_value = { |
65 | | - 'data': [], |
| 72 | + "data": [], |
66 | 73 | } |
67 | 74 |
|
68 | | - with app.container.giphy_client.override(giphy_client_mock): |
69 | | - _, response = await app.asgi_client.get('/') |
| 75 | + with app.ctx.container.giphy_client.override(giphy_client_mock): |
| 76 | + response = await test_client.get("/") |
70 | 77 |
|
71 | | - assert response.status == 200 |
| 78 | + assert response.status_code == 200 |
72 | 79 | data = response.json() |
73 | | - assert data['query'] == app.container.config.default.query() |
74 | | - assert data['limit'] == app.container.config.default.limit() |
| 80 | + assert data["query"] == app.ctx.container.config.default.query() |
| 81 | + assert data["limit"] == app.ctx.container.config.default.limit() |
0 commit comments