1+ from __future__ import unicode_literals
2+
3+ import json
4+
5+ import django
16import mock
7+ from channels import Channel
8+ from channels .test import ChannelTestCase , Client
29from 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
629from 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
2650def 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