Skip to content

Commit 9827aa8

Browse files
committed
test: cover channels consumer
1 parent 1ffc6b9 commit 9827aa8

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/tests/channels/test_consumer.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
5+
import pytest
6+
7+
from graphql_server.channels.handlers.base import ChannelsConsumer
8+
9+
10+
class DummyChannelLayer:
11+
def __init__(self) -> None:
12+
self.added: list[tuple[str, str]] = []
13+
self.discarded: list[tuple[str, str]] = []
14+
15+
async def group_add(self, group: str, channel: str) -> None:
16+
self.added.append((group, channel))
17+
18+
async def group_discard(self, group: str, channel: str) -> None:
19+
self.discarded.append((group, channel))
20+
21+
22+
@pytest.mark.asyncio
23+
async def test_channel_listen_receives_messages_and_cleans_up() -> None:
24+
consumer = ChannelsConsumer()
25+
layer = DummyChannelLayer()
26+
consumer.channel_layer = layer
27+
consumer.channel_name = "chan"
28+
29+
gen = consumer.channel_listen("test.message", groups=["g"], timeout=0.1)
30+
31+
async def send() -> None:
32+
await asyncio.sleep(0)
33+
queue = next(iter(consumer.listen_queues["test.message"]))
34+
queue.put_nowait({"type": "test.message", "payload": 1})
35+
36+
asyncio.create_task(send())
37+
38+
with pytest.deprecated_call(match="Use listen_to_channel instead"):
39+
message = await gen.__anext__()
40+
assert message == {"type": "test.message", "payload": 1}
41+
42+
await gen.aclose()
43+
44+
assert layer.added == [("g", "chan")]
45+
assert layer.discarded == [("g", "chan")]
46+
47+
48+
@pytest.mark.asyncio
49+
async def test_channel_listen_times_out() -> None:
50+
consumer = ChannelsConsumer()
51+
layer = DummyChannelLayer()
52+
consumer.channel_layer = layer
53+
consumer.channel_name = "chan"
54+
55+
gen = consumer.channel_listen("test.message", groups=["g"], timeout=0.01)
56+
57+
with pytest.deprecated_call(match="Use listen_to_channel instead"):
58+
with pytest.raises(StopAsyncIteration):
59+
await gen.__anext__()
60+
61+
assert layer.added == [("g", "chan")]
62+
assert layer.discarded == [("g", "chan")]

src/tests/utils/test_debug.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import builtins
2+
3+
import pytest
4+
5+
from graphql_server.utils.debug import GraphQLJSONEncoder, pretty_print_graphql_operation
6+
7+
8+
def test_graphql_json_encoder_default():
9+
class Foo:
10+
pass
11+
12+
foo = Foo()
13+
encoder = GraphQLJSONEncoder()
14+
assert encoder.default(foo) == repr(foo)
15+
16+
17+
def test_pretty_print_requires_pygments(monkeypatch):
18+
original_import = builtins.__import__
19+
20+
def fake_import(name, *args, **kwargs):
21+
if name.startswith("pygments"):
22+
raise ImportError("No module named pygments")
23+
return original_import(name, *args, **kwargs)
24+
25+
monkeypatch.setattr(builtins, "__import__", fake_import)
26+
with pytest.raises(ImportError):
27+
pretty_print_graphql_operation("Query", "query { field }", None)
28+
29+
30+
def test_pretty_print_graphql_operation(capsys):
31+
obj = object()
32+
variables = {"var": obj}
33+
pretty_print_graphql_operation("MyQuery", "query { field }", variables)
34+
captured = capsys.readouterr().out
35+
assert "MyQuery" in captured
36+
assert "field" in captured
37+
assert "var" in captured
38+
assert repr(obj) in captured
39+
40+
41+
def test_pretty_print_introspection_query(capsys):
42+
pretty_print_graphql_operation(
43+
"IntrospectionQuery", "query { __schema { queryType { name } } }", None
44+
)
45+
captured = capsys.readouterr().out
46+
assert captured == ""

0 commit comments

Comments
 (0)