Skip to content

Commit d8abc01

Browse files
committed
Initial tests for aiohttp and django_channels
1 parent e28f05d commit d8abc01

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

setup.cfg

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,22 @@ dev =
4545
flake8>=3.7,<4
4646
tox>=3,<4
4747
pytest>=3.2.5,<4
48+
pytest-cov
49+
pytest-asyncio; python_version>="3.4"
4850
Sphinx>=1.8,<2
4951
coverage>=5.0,<6
5052
test =
5153
pytest>=3.2.5,<4
5254
graphene>=2.0,<3
53-
55+
gevent
56+
graphene>=2.0
57+
graphene_django
58+
mock; python_version<"3"
59+
django==1.11.*; python_version<"3"
60+
channels==1.*; python_version<"3"
61+
django==2.*; python_version>="3"
62+
channels==2.*; python_version>="3"
63+
aiohttp; python_version>="3.5"
5464

5565
[bdist_wheel]
5666
universal = 1

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
if sys.version_info > (3,):
4+
collect_ignore = ["test_django_channels.py"]
5+
else:
6+
collect_ignore = ["test_aiohttp.py"]

tests/test_aiohttp.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from unittest import mock
2+
3+
import pytest
4+
from aiohttp import WSMsgType
5+
6+
from graphql_ws.aiohttp import AiohttpConnectionContext, AiohttpSubscriptionServer
7+
from graphql_ws.base import ConnectionClosedException
8+
9+
10+
class AsyncMock(mock.Mock):
11+
def __call__(self, *args, **kwargs):
12+
13+
async def coro():
14+
return super(AsyncMock, self).__call__(*args, **kwargs)
15+
16+
return coro()
17+
18+
19+
@pytest.fixture()
20+
def mock_ws():
21+
ws = AsyncMock(spec=["receive", "send_str", "closed", "close"])
22+
ws.closed = False
23+
ws.receive.return_value = AsyncMock(spec=["type", "data"])
24+
return ws
25+
26+
27+
@pytest.mark.asyncio
28+
class TestConnectionContext:
29+
async def test_receive_good_data(self, mock_ws):
30+
msg = mock_ws.receive.return_value
31+
msg.type = WSMsgType.TEXT
32+
msg.data = "test"
33+
connection_context = AiohttpConnectionContext(ws=mock_ws)
34+
assert await connection_context.receive() == "test"
35+
36+
async def test_receive_error(self, mock_ws):
37+
msg = mock_ws.receive.return_value
38+
msg.type = WSMsgType.ERROR
39+
connection_context = AiohttpConnectionContext(ws=mock_ws)
40+
with pytest.raises(ConnectionClosedException):
41+
await connection_context.receive()
42+
43+
async def test_receive_closing(self, mock_ws):
44+
mock_ws.receive.return_value.type = WSMsgType.CLOSING
45+
connection_context = AiohttpConnectionContext(ws=mock_ws)
46+
with pytest.raises(ConnectionClosedException):
47+
await connection_context.receive()
48+
49+
async def test_receive_closed(self, mock_ws):
50+
mock_ws.receive.return_value.type = WSMsgType.CLOSED
51+
connection_context = AiohttpConnectionContext(ws=mock_ws)
52+
with pytest.raises(ConnectionClosedException):
53+
await connection_context.receive()
54+
55+
async def test_send(self, mock_ws):
56+
connection_context = AiohttpConnectionContext(ws=mock_ws)
57+
await connection_context.send("test")
58+
mock_ws.send_str.assert_called_with("test")
59+
60+
async def test_send_closed(self, mock_ws):
61+
mock_ws.closed = True
62+
connection_context = AiohttpConnectionContext(ws=mock_ws)
63+
await connection_context.send("test")
64+
mock_ws.send_str.assert_not_called()
65+
66+
async def test_close(self, mock_ws):
67+
connection_context = AiohttpConnectionContext(ws=mock_ws)
68+
await connection_context.close(123)
69+
mock_ws.close.assert_called_with(code=123)
70+
71+
72+
def test_subscription_server_smoke():
73+
AiohttpSubscriptionServer(schema=None)

tests/test_django_channels.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import mock
2+
from django.conf import settings
3+
4+
settings.configure() # noqa
5+
6+
from graphql_ws.django_channels import (
7+
DjangoChannelConnectionContext,
8+
DjangoChannelSubscriptionServer,
9+
)
10+
11+
12+
class TestConnectionContext:
13+
def test_send(self):
14+
msg = mock.Mock()
15+
connection_context = DjangoChannelConnectionContext(message=msg)
16+
connection_context.send("test")
17+
msg.reply_channel.send.assert_called_with("test")
18+
19+
def test_close(self):
20+
msg = mock.Mock()
21+
connection_context = DjangoChannelConnectionContext(message=msg)
22+
connection_context.close(123)
23+
msg.reply_channel.send.assert_called_with({"close": True, "text": 123})
24+
25+
26+
def test_subscription_server_smoke():
27+
DjangoChannelSubscriptionServer(schema=None)

0 commit comments

Comments
 (0)