Skip to content

Commit 88420ab

Browse files
authored
Merge pull request #8 from obytes/test_subscription
chore: Add Subscriptions Tests
2 parents 429c888 + 219ea2c commit 88420ab

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/test_api.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import asyncio
12
import json
3+
from threading import Timer
24

35
import httpx
46
import pytest
7+
from ariadne.asgi import GQL_CONNECTION_INIT, GQL_START
8+
from websockets import connect
59

610
my_storage = {}
711

@@ -81,8 +85,45 @@ async def create_blog(host, storage):
8185
)
8286
json_response = json.loads(response.text)
8387
assert ("errors" in json_response) == True
88+
assert json_response["data"]["createblog"]["id"] is not None
8489

8590

8691
@pytest.mark.asyncio
8792
async def test_create_blog(server, host, storage):
8893
await create_blog(host, storage)
94+
95+
96+
@pytest.mark.asyncio
97+
async def test_subscription(server, host, storage):
98+
query = """
99+
subscription reviewblog($token: String!) {
100+
reviewblog(token: $token) {
101+
errors
102+
id
103+
}
104+
}
105+
"""
106+
variables = {"token": f'Bearer {storage["token"]}'}
107+
ws = await connect(f"ws://{host}/", subprotocols=["graphql-ws"])
108+
await ws.send(json.dumps({"type": GQL_CONNECTION_INIT}))
109+
await ws.send(
110+
json.dumps(
111+
{"type": GQL_START, "payload": {"query": query, "variables": variables},}
112+
)
113+
)
114+
received = await ws.recv()
115+
assert received == '{"type": "connection_ack"}'
116+
117+
def delay_create_blog(server, host):
118+
loop = asyncio.new_event_loop()
119+
asyncio.set_event_loop(loop)
120+
loop.run_until_complete(create_blog(server, host))
121+
122+
timer = Timer(1.0, delay_create_blog, (server, host, storage))
123+
timer.start()
124+
125+
received = await ws.recv()
126+
await ws.close()
127+
json_response = json.loads(received)
128+
assert ("errors" in json_response) == False
129+
assert json_response["payload"]["data"]["reviewblog"]["id"] is not None

0 commit comments

Comments
 (0)