-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_context.py
156 lines (124 loc) · 5.61 KB
/
test_context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright (C) DATADVANCE, 2010-2023
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Test `info.context` and `DictAsObject`."""
from typing import List
import graphene
import pytest
import channels_graphql_ws.dict_as_object
def test_dict__as_object():
"""Make sure `DictAsObject` behaves as a correct dict wrapper."""
print("Construct a context as a wrapper of dict scope.")
scope: dict = {}
context = channels_graphql_ws.dict_as_object.DictAsObject(scope)
print("Add records and check they propagate in both directions.")
context.marker1 = 1
assert "marker1" in context
assert scope["marker1"] == context["marker1"] == context.marker1 == 1
scope["marker2"] = 2
assert "marker2" in context
assert scope["marker2"] == context["marker2"] == context.marker2 == 2
print("Check string context representation equals to dict one.")
assert str(context) == str(scope)
print("Make sure `_asdict` returns underlying scope.")
assert id(context._asdict()) == id(scope)
print("Remove records and check they propagate in both directions.")
del scope["marker1"]
assert "marker1" not in context
assert "marker1" not in scope
with pytest.raises(KeyError):
_ = context["marker1"]
with pytest.raises(AttributeError):
_ = context.marker1
del context["marker2"]
assert "marker2" not in context
assert "marker2" not in scope
with pytest.raises(KeyError):
_ = context["marker2"]
with pytest.raises(AttributeError):
_ = context.marker2
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_context_lifetime(gql, subprotocol):
"""Check `info.context` does hold data between requests."""
# Store ids of `info.context` to check them later.
run_log: List[bool] = []
print("Setup GraphQL backend and initialize GraphQL client.")
class Query(graphene.ObjectType):
"""Root GraphQL query."""
ok = graphene.Boolean()
def resolve_ok(self, info):
"""Store `info.context` id."""
run_log.append("fortytwo" in info.context)
if "fortytwo" in info.context:
assert info.context.fortytwo == 42, "Context has delivered wrong data!"
info.context.fortytwo = 42
return True
for _ in range(2):
print("Make connection,perform query, and close connection.")
client = gql(
query=Query,
consumer_attrs={"strict_ordering": True},
subprotocol=subprotocol,
)
await client.connect_and_init()
for _ in range(2):
op_id = await client.start(query="{ ok }")
await client.receive_next(op_id)
await client.receive_complete(op_id)
await client.finalize()
# Expected run log: [False, False, False, False].
assert not any(run_log), "Context preserved some values between requests!"
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_context_channels_scope_lifetime(gql, subprotocol):
"""Check `info.context.channels_scope` holds data in connection."""
# Store ids of `info.context.channels_scope` to check them later.
run_log: List[bool] = []
print("Setup GraphQL backend and initialize GraphQL client.")
class Query(graphene.ObjectType):
"""Root GraphQL query."""
ok = graphene.Boolean()
def resolve_ok(self, info):
"""Store `info.context.channels_scope` id."""
run_log.append("fortytwo" in info.context.channels_scope)
if "fortytwo" in info.context.channels_scope:
assert (
info.context.channels_scope["fortytwo"] == 42
), "Context has delivered wrong data!"
info.context.channels_scope["fortytwo"] = 42
return True
for _ in range(2):
print("Make connection,perform query, and close connection.")
client = gql(
query=Query,
consumer_attrs={"strict_ordering": True},
subprotocol=subprotocol,
)
await client.connect_and_init()
for _ in range(2):
op_id = await client.start(query="{ ok }")
await client.receive_next(op_id)
await client.receive_complete(op_id)
await client.finalize()
# Expected run log: [False, True, False, True].
assert run_log[2] is False, "Data stays between connections!"
assert run_log[0:2] == [False, True], "Data lost within a single connection!"
assert run_log[2:4] == [False, True], "Data lost within a single connection!"