Skip to content

Commit 38a7f9f

Browse files
committed
Add django_channels tests
1 parent 6cf9332 commit 38a7f9f

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

tests/django_routing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from channels.routing import route
2+
from graphql_ws.django_channels import GraphQLSubscriptionConsumer
3+
4+
channel_routing = [
5+
route("websocket.receive", GraphQLSubscriptionConsumer),
6+
]

tests/test_django_channels.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
from __future__ import unicode_literals
2+
3+
import json
4+
5+
import django
16
import mock
7+
from channels import Channel
8+
from channels.test import ChannelTestCase, Client
29
from django.conf import settings
10+
from django.core.management import call_command
311

4-
settings.configure() # noqa
12+
settings.configure(
13+
CHANNEL_LAYERS={
14+
"default": {
15+
"BACKEND": "asgiref.inmemory.ChannelLayer",
16+
"ROUTING": "tests.django_routing.channel_routing",
17+
},
18+
},
19+
INSTALLED_APPS=[
20+
"django.contrib.sessions",
21+
"django.contrib.contenttypes",
22+
"django.contrib.auth",
23+
],
24+
DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
25+
)
26+
django.setup()
527

28+
from graphql_ws.constants import GQL_CONNECTION_ACK, GQL_CONNECTION_INIT
629
from graphql_ws.django_channels import (
730
DjangoChannelConnectionContext,
831
DjangoChannelSubscriptionServer,
32+
GraphQLSubscriptionConsumer,
933
)
1034

1135

@@ -14,7 +38,7 @@ def test_send(self):
1438
msg = mock.Mock()
1539
connection_context = DjangoChannelConnectionContext(message=msg)
1640
connection_context.send("test")
17-
msg.reply_channel.send.assert_called_with({'text': '"test"'})
41+
msg.reply_channel.send.assert_called_with({"text": '"test"'})
1842

1943
def test_close(self):
2044
msg = mock.Mock()
@@ -25,3 +49,21 @@ def test_close(self):
2549

2650
def test_subscription_server_smoke():
2751
DjangoChannelSubscriptionServer(schema=None)
52+
53+
54+
class TestConsumer(ChannelTestCase):
55+
def test_connect(self):
56+
call_command("migrate")
57+
Channel("websocket.receive").send(
58+
{
59+
"path": "/graphql",
60+
"order": 0,
61+
"reply_channel": "websocket.receive",
62+
"text": json.dumps({"type": GQL_CONNECTION_INIT, "id": 1}),
63+
}
64+
)
65+
message = self.get_next_message("websocket.receive", require=True)
66+
GraphQLSubscriptionConsumer(message)
67+
result = self.get_next_message("websocket.receive", require=True)
68+
result_content = json.loads(result.content["text"])
69+
assert result_content == {"type": GQL_CONNECTION_ACK}

0 commit comments

Comments
 (0)