1
+ from __future__ import unicode_literals
2
+
3
+ import json
4
+
5
+ import django
1
6
import mock
7
+ from channels import Channel
8
+ from channels .test import ChannelTestCase , Client
2
9
from django .conf import settings
10
+ from django .core .management import call_command
3
11
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 ()
5
27
28
+ from graphql_ws .constants import GQL_CONNECTION_ACK , GQL_CONNECTION_INIT
6
29
from graphql_ws .django_channels import (
7
30
DjangoChannelConnectionContext ,
8
31
DjangoChannelSubscriptionServer ,
32
+ GraphQLSubscriptionConsumer ,
9
33
)
10
34
11
35
@@ -14,7 +38,7 @@ def test_send(self):
14
38
msg = mock .Mock ()
15
39
connection_context = DjangoChannelConnectionContext (message = msg )
16
40
connection_context .send ("test" )
17
- msg .reply_channel .send .assert_called_with ({' text' : '"test"' })
41
+ msg .reply_channel .send .assert_called_with ({" text" : '"test"' })
18
42
19
43
def test_close (self ):
20
44
msg = mock .Mock ()
@@ -25,3 +49,21 @@ def test_close(self):
25
49
26
50
def test_subscription_server_smoke ():
27
51
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