Skip to content

Commit ac554c1

Browse files
authored
[Tests] Fixture to generate the test API client (#263)
Added the new `ccn_api_client` fixture to put the API test client code in one place. We now set the event loop in debug mode to return the stack trace on 500 errors.
1 parent 9d41b98 commit ac554c1

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

tests/api/test_messages.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import pytest_asyncio
88

99
from aleph.model.messages import Message
10-
from aleph.web import create_app
1110

1211
MESSAGES_URI = "/api/v0/messages.json"
1312

@@ -43,11 +42,8 @@ async def fixture_messages(test_db):
4342

4443

4544
@pytest.mark.asyncio
46-
async def test_get_messages(fixture_messages, aiohttp_client):
47-
app = create_app()
48-
client = await aiohttp_client(app)
49-
50-
response = await client.get(MESSAGES_URI)
45+
async def test_get_messages(fixture_messages, ccn_api_client):
46+
response = await ccn_api_client.get(MESSAGES_URI)
5147
assert response.status == 200, await response.text()
5248

5349
data = await response.json()
@@ -61,12 +57,9 @@ async def test_get_messages(fixture_messages, aiohttp_client):
6157

6258

6359
@pytest.mark.asyncio
64-
async def test_get_messages_filter_by_channel(fixture_messages, aiohttp_client):
65-
app = create_app()
66-
client = await aiohttp_client(app)
67-
60+
async def test_get_messages_filter_by_channel(fixture_messages, ccn_api_client):
6861
async def fetch_messages_by_channel(channel: str) -> Dict:
69-
response = await client.get(MESSAGES_URI, params={"channels": channel})
62+
response = await ccn_api_client.get(MESSAGES_URI, params={"channels": channel})
7063
assert response.status == 200, await response.text()
7164
return await response.json()
7265

tests/api/test_version.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import pytest
22

33
from aleph import __version__
4-
from aleph.web import create_app
54

65

76
@pytest.mark.asyncio
8-
async def test_get_version(aiohttp_client):
9-
app = create_app()
10-
client = await aiohttp_client(app)
11-
12-
response = await client.get("/api/v0/version")
7+
async def test_get_version(ccn_api_client):
8+
response = await ccn_api_client.get("/api/v0/version")
139
assert response.status == 200, await response.text()
1410

1511
data = await response.json()

tests/conftest.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import pytest
2-
from aleph.model import init_db
3-
from aleph.config import get_defaults
4-
from configmanager import Config
1+
import asyncio
2+
import os
3+
54
import pymongo
65
import pytest_asyncio
6+
from configmanager import Config
77

8+
from aleph.config import get_defaults
9+
from aleph.model import init_db
10+
from aleph.web import create_app
811

912
TEST_DB = "ccn_automated_tests"
1013

@@ -28,3 +31,15 @@ async def test_db():
2831

2932
from aleph.model import db
3033
yield db
34+
35+
36+
@pytest_asyncio.fixture
37+
async def ccn_api_client(aiohttp_client):
38+
# Make aiohttp return the stack trace on 500 errors
39+
event_loop = asyncio.get_event_loop()
40+
event_loop.set_debug(True)
41+
42+
app = create_app()
43+
client = await aiohttp_client(app)
44+
45+
return client

0 commit comments

Comments
 (0)