test(jobs): test simple counting

This commit is contained in:
Houkime 2024-05-15 20:43:17 +00:00
parent 967e59271f
commit 3910e416db
2 changed files with 81 additions and 26 deletions

View file

@ -28,6 +28,8 @@ from selfprivacy_api.graphql.queries.services import Services
from selfprivacy_api.graphql.queries.storage import Storage from selfprivacy_api.graphql.queries.storage import Storage
from selfprivacy_api.graphql.queries.system import System from selfprivacy_api.graphql.queries.system import System
from selfprivacy_api.graphql.subscriptions.jobs import JobSubscriptions
from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations
from selfprivacy_api.graphql.queries.users import Users from selfprivacy_api.graphql.queries.users import Users
from selfprivacy_api.jobs.test import test_job from selfprivacy_api.jobs.test import test_job
@ -129,16 +131,19 @@ class Mutation(
code=200, code=200,
) )
pass
@strawberry.type @strawberry.type
class Subscription: class Subscription:
"""Root schema for subscriptions""" """Root schema for subscriptions"""
@strawberry.subscription(permission_classes=[IsAuthenticated]) @strawberry.field(permission_classes=[IsAuthenticated])
async def count(self, target: int = 100) -> AsyncGenerator[int, None]: def jobs(self) -> JobSubscriptions:
for i in range(target): """Jobs subscriptions"""
return JobSubscriptions()
@strawberry.subscription
async def count(self) -> AsyncGenerator[int, None]:
for i in range(10):
yield i yield i
await asyncio.sleep(0.5) await asyncio.sleep(0.5)

View file

@ -2,22 +2,22 @@ from tests.common import generate_jobs_subscription
from selfprivacy_api.graphql.queries.jobs import Job as _Job from selfprivacy_api.graphql.queries.jobs import Job as _Job
from selfprivacy_api.jobs import Jobs from selfprivacy_api.jobs import Jobs
# JOBS_SUBSCRIPTION = """ JOBS_SUBSCRIPTION = """
# jobUpdates { jobUpdates {
# uid uid
# typeId typeId
# name name
# description description
# status status
# statusText statusText
# progress progress
# createdAt createdAt
# updatedAt updatedAt
# finishedAt finishedAt
# error error
# result result
# } }
# """ """
def test_websocket_connection_bare(authorized_client): def test_websocket_connection_bare(authorized_client):
@ -50,12 +50,62 @@ def test_websocket_graphql_ping(authorized_client):
assert pong == {"type": "pong"} assert pong == {"type": "pong"}
def init_graphql(websocket):
websocket.send_json({"type": "connection_init", "payload": {}})
ack = websocket.receive_json()
assert ack == {"type": "connection_ack"}
def test_websocket_subscription_minimal(authorized_client):
client = authorized_client
with client.websocket_connect(
"/graphql", subprotocols=["graphql-transport-ws"]
) as websocket:
init_graphql(websocket)
websocket.send_json(
{
"id": "3aaa2445",
"type": "subscribe",
"payload": {
"query": "subscription TestSubscription {count}",
},
}
)
response = websocket.receive_json()
assert response == {
"id": "3aaa2445",
"payload": {"data": {"count": 0}},
"type": "next",
}
response = websocket.receive_json()
assert response == {
"id": "3aaa2445",
"payload": {"data": {"count": 1}},
"type": "next",
}
response = websocket.receive_json()
assert response == {
"id": "3aaa2445",
"payload": {"data": {"count": 2}},
"type": "next",
}
# def test_websocket_subscription(authorized_client): # def test_websocket_subscription(authorized_client):
# client = authorized_client # client = authorized_client
# with client.websocket_connect( # with client.websocket_connect(
# "/graphql", subprotocols=["graphql-transport-ws", "graphql-ws"] # "/graphql", subprotocols=["graphql-transport-ws"]
# ) as websocket: # ) as websocket:
# websocket.send(generate_jobs_subscription([JOBS_SUBSCRIPTION])) # init_graphql(websocket)
# websocket.send_json(
# {
# "id": "3aaa2445",
# "type": "subscribe",
# "payload": {
# "query": generate_jobs_subscription([JOBS_SUBSCRIPTION]),
# },
# }
# )
# Jobs.add("bogus", "bogus.bogus", "yyyaaaaayy") # Jobs.add("bogus", "bogus.bogus", "yyyaaaaayy")
# joblist = websocket.receive_json() # response = websocket.receive_json()
# raise NotImplementedError(joblist) # raise NotImplementedError(response)