Skip to content

Commit

Permalink
Merge pull request #157 from simonsobs/dev
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
TaiSakuma authored Nov 21, 2024
2 parents 7e26a7d + 22430ca commit 4842ca0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 2 deletions.
19 changes: 19 additions & 0 deletions nextlinegraphql/plugins/dev/graphql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pathlib import Path

from graphql import parse, print_ast


def read_gql(path: Path | str) -> str:
'''Load a GraphQL query from a file while checking its syntax.'''

text = Path(path).read_text()
parsed = parse(text)
reformatted = print_ast(parsed)
return reformatted


pwd = Path(__file__).resolve().parent


sub = pwd / 'queries'
QUERY_HEADERS = read_gql(sub / 'Headers.gql')
5 changes: 5 additions & 0 deletions nextlinegraphql/plugins/dev/graphql/queries/Headers.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query DevHeaders {
dev {
headers
}
}
3 changes: 3 additions & 0 deletions nextlinegraphql/plugins/dev/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__all__ = ['Query']

from .query import Query

Mutation = None
Subscription = None
4 changes: 2 additions & 2 deletions tests/plugins/ctrl/schema/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def _subscribe_state(schema: Schema, context: Any) -> list[str]:
ret = []
sub = await schema.subscribe(SUBSCRIBE_STATE, context_value=context)
assert hasattr(sub, '__aiter__')
async for result in sub:
async for result in sub: # pragma: no branch
assert (data := result.data)
state = data['ctrlState']
ret.append(state)
Expand Down Expand Up @@ -138,7 +138,7 @@ async def _control_execution(schema: Schema, context: Any) -> None:
agen = agen_with_wait(sub)

prev_ids = set[int]()
async for result in agen:
async for result in agen: # pragma: no branch
assert isinstance(result, ExecutionResult)
assert (data := result.data)
trace_ids: list[int] = data['ctrlTraceIds']
Expand Down
Empty file added tests/plugins/dev/__init__.py
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions tests/plugins/dev/schema/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from strawberry import Schema

from nextlinegraphql.plugins.dev.schema import Mutation, Query, Subscription


@pytest.fixture(scope='session')
def schema() -> Schema:
'''GraphQL schema
The scope is `session` because Hypothesis doesn't allow function-scoped
fixtures. This is fine as the schema is stateless.
'''
return Schema(query=Query, mutation=Mutation, subscription=Subscription)
Empty file.
18 changes: 18 additions & 0 deletions tests/plugins/dev/schema/queries/test_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
from unittest.mock import Mock

from starlette.requests import Headers, Request

from nextlinegraphql.plugins.dev.graphql import QUERY_HEADERS
from tests.plugins.dev.schema.conftest import Schema


async def test_schema(schema: Schema) -> None:
request = Mock(spec=Request)
request.headers = Headers({'foo': 'bar'})
context = {'request': request}
result = await schema.execute(QUERY_HEADERS, context_value=context)
assert (data := result.data)
expected = {'dev': {'headers': json.dumps({'foo': 'bar'})}}
assert data == expected

0 comments on commit 4842ca0

Please sign in to comment.