2020-03-14 19:48:12 +03:00
|
|
|
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-03-14 19:48:12 +03:00
|
|
|
|
|
|
|
|
2020-07-11 15:37:18 +03:00
|
|
|
MYPY = False
|
|
|
|
if MYPY:
|
|
|
|
from typing import Callable # NOQA
|
|
|
|
|
|
|
|
|
2020-03-14 19:48:12 +03:00
|
|
|
class Query(ObjectType):
|
2020-07-11 13:51:04 +03:00
|
|
|
a = String()
|
2020-03-14 19:48:12 +03:00
|
|
|
|
2020-07-11 13:51:04 +03:00
|
|
|
|
|
|
|
class SubscriptionOptions(ObjectTypeOptions):
|
|
|
|
pass
|
2020-03-14 19:48:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
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-03-14 19:48:12 +03:00
|
|
|
|
2020-07-11 15:37:18 +03:00
|
|
|
async def subscribe_count_to_ten(root, info, **kwargs):
|
2020-03-14 19:48:12 +03:00
|
|
|
count = 0
|
|
|
|
while count < 10:
|
|
|
|
count += 1
|
2020-07-11 15:37:18 +03:00
|
|
|
yield count
|
2020-03-14 19:48:12 +03:00
|
|
|
|
|
|
|
|
2020-07-11 13:51:04 +03:00
|
|
|
schema = Schema(query=Query, subscription=MySubscription)
|
2020-03-14 19:48:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
@mark.asyncio
|
|
|
|
async def test_subscription():
|
2020-07-11 15:37:18 +03:00
|
|
|
subscription = """
|
|
|
|
subscription {
|
|
|
|
countToTen
|
|
|
|
}
|
|
|
|
"""
|
2020-03-14 19:48:12 +03:00
|
|
|
result = await schema.subscribe(subscription)
|
|
|
|
count = 0
|
|
|
|
async for item in result:
|
|
|
|
count = item.data["countToTen"]
|
|
|
|
assert count == 10
|