Skip to content

Commit 6108b57

Browse files
committed
Added Schema and SubscriptionManager fixtures to tests
1 parent e0a4052 commit 6108b57

1 file changed

Lines changed: 102 additions & 13 deletions

File tree

tests/test_subscription_manager.py

Lines changed: 102 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
import pytest
22
import redis
3-
from graphql_subscriptions import RedisPubsub
3+
import graphene
4+
from graphql_subscriptions import RedisPubsub, SubscriptionManager
45

56

6-
@pytest.fixture(autouse=True)
7+
@pytest.fixture
78
def mock_redis(monkeypatch):
89
import fakeredis
910
monkeypatch.setattr(redis, 'StrictRedis', fakeredis.FakeStrictRedis)
1011

1112

13+
@pytest.fixture()
14+
def pubsub(mock_redis):
15+
return RedisPubsub()
16+
17+
1218
@pytest.mark.parametrize('test_input, expected', [
1319
('test', 'test'),
1420
({1: 'test'}, {1: 'test'}),
1521
(None, None)
1622
])
17-
def test_pubsub_subscribe_and_publish(test_input, expected):
18-
ps = RedisPubsub()
23+
def test_pubsub_subscribe_and_publish(pubsub, test_input, expected):
1924

2025
def message_handler(message):
2126
assert message == expected
2227

2328
def publish_callback(sub_id):
24-
assert ps.publish('a', test_input)
29+
assert pubsub.publish('a', test_input)
2530

26-
ps.subscribe('a', message_handler, {}).then(publish_callback)
31+
pubsub.subscribe('a', message_handler, {}).then(publish_callback)
2732

2833
while True:
29-
message = ps.pubsub.get_message(ignore_subscribe_messages=True)
34+
message = pubsub.pubsub.get_message(ignore_subscribe_messages=True)
3035
if message:
31-
ps.handle_message(message)
36+
pubsub.handle_message(message)
3237
break
3338

3439

@@ -37,14 +42,98 @@ def publish_callback(sub_id):
3742
({1: 'test'}, {1: 'test'}),
3843
(None, None)
3944
])
40-
def test_pubsub_subscribe_and_unsubscribe(test_input, expected):
41-
ps = RedisPubsub()
45+
def test_pubsub_subscribe_and_unsubscribe(pubsub, test_input, expected):
4246

4347
def message_handler(message):
4448
assert 0
4549

4650
def unsubscribe_publish_callback(sub_id):
47-
ps.unsubscribe(sub_id)
48-
assert ps.publish('a', test_input)
51+
pubsub.unsubscribe(sub_id)
52+
assert pubsub.publish('a', test_input)
53+
54+
pubsub.subscribe('a', message_handler,
55+
{}).then(unsubscribe_publish_callback)
56+
57+
58+
@pytest.fixture
59+
def schema():
60+
class Query(graphene.ObjectType):
61+
test_string = graphene.String()
62+
63+
def resolve_test_string(self, args, context, info):
64+
return 'works'
65+
66+
class Subscription(graphene.ObjectType):
67+
test_subscription = graphene.String()
68+
test_context = graphene.String()
69+
test_filter = graphene.String(filter_boolean=graphene.Boolean())
70+
test_filter_multi = graphene.String(
71+
filter_boolean=graphene.Boolean(),
72+
a=graphene.String(),
73+
b=graphene.Int()
74+
)
75+
test_channel_options = graphene.String()
76+
77+
def resolve_test_subscription(self, args, context, info):
78+
return self
79+
80+
def resolve_test_context(self, args, context, info):
81+
return context
82+
83+
def resolve_test_filter(self, args, context, info):
84+
return 'good_filter' if args.get('filter_boolean') else 'bad_filter'
85+
86+
def resolve_test_filter_multi(self, args, context, info):
87+
return 'good_filter' if args.get('filter_boolean') else 'bad_filter'
88+
89+
def resolve_test_channel_options(self, args, context, info):
90+
return self
91+
92+
return graphene.Schema(query=Query, subscription=Subscription)
93+
94+
95+
@pytest.fixture
96+
def sub_mgr(pubsub, schema):
97+
98+
def filter_single(**kwargs):
99+
args = kwargs.get('args')
100+
return {
101+
'filter_1': {
102+
'filter': lambda root, context: root.get('filter_boolean') == args.get('filter_boolean')
103+
}
104+
}
105+
106+
def filter_multi(**kwargs):
107+
return {
108+
'trigger_1': {
109+
'filter': lambda root, context: True
110+
},
111+
'trigger_2': {
112+
'filter': lambda root, context: True
113+
}
114+
}
115+
116+
def filter_channel_options(**kwargs):
117+
return {
118+
'trigger_1': {
119+
'channel_options': {
120+
'foo': 'bar'
121+
}
122+
}
123+
}
124+
125+
def filter_context(**kwargs):
126+
return {
127+
'context_trigger': lambda root, context: context == 'trigger'
128+
}
49129

50-
ps.subscribe('a', message_handler, {}).then(unsubscribe_publish_callback)
130+
return SubscriptionManager(
131+
schema,
132+
pubsub,
133+
setup_funcs={
134+
'test_filter': filter_single,
135+
'test_filter_multi': filter_multi,
136+
'test_channel_options': filter_channel_options,
137+
'test_context': filter_context
138+
}
139+
)

0 commit comments

Comments
 (0)