graphene/tests_asyncio/test_subscribe.py

56 lines
1.1 KiB
Python
Raw Normal View History

from pytest import mark
2020-07-11 15:41:47 +03:00
from graphene import ObjectType, Int, String, Schema
2020-07-11 13:51:04 +03:00
from graphene.types.objecttype import ObjectTypeOptions
2020-07-11 15:37:18 +03:00
MYPY = False
if MYPY:
from typing import Callable # NOQA
class Query(ObjectType):
2020-07-11 13:51:04 +03:00
a = String()
2020-07-11 13:51:04 +03:00
class SubscriptionOptions(ObjectTypeOptions):
pass
class Subscription(ObjectType):
2020-07-11 13:51:04 +03:00
@classmethod
def __init_subclass_with_meta__(
2020-07-11 15:37:18 +03:00
cls, _meta=None, **options,
2020-07-11 13:51:04 +03:00
):
if not _meta:
_meta = SubscriptionOptions(cls)
super().__init_subclass_with_meta__(_meta=_meta, **options)
class MySubscription(Subscription):
2020-07-11 15:37:18 +03:00
count_to_ten = Int(yes=Int())
2020-07-11 15:37:18 +03:00
async def subscribe_count_to_ten(root, info, **kwargs):
count = 0
while count < 10:
count += 1
2020-07-11 15:37:18 +03:00
yield count
2020-07-11 13:51:04 +03:00
schema = Schema(query=Query, subscription=MySubscription)
@mark.asyncio
async def test_subscription():
2020-07-11 15:37:18 +03:00
subscription = """
subscription {
countToTen
}
"""
result = await schema.subscribe(subscription)
count = 0
async for item in result:
count = item.data["countToTen"]
assert count == 10