graphene/tests_asyncio/test_subscribe.py

58 lines
1.2 KiB
Python
Raw Normal View History

2020-07-11 15:37:18 +03:00
from datetime import datetime
from pytest import mark
2020-07-11 15:37:18 +03:00
from graphene import ObjectType, Int, String, Schema, DateTime
2020-07-11 13:51:04 +03:00
from graphene.types.objecttype import ObjectTypeOptions
from graphene.utils.get_unbound_function import get_unbound_function
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