-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtest_openapi.py
More file actions
66 lines (49 loc) · 1.98 KB
/
test_openapi.py
File metadata and controls
66 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pytest
from graphql import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
def resolve_abc(_root, _info):
return "abc"
def test_include_router_prefix():
from starlette.testclient import TestClient
from fastapi import FastAPI
from graphql_server.fastapi import GraphQLRouter
app = FastAPI()
QueryType = GraphQLObjectType(
name="Query",
fields={"abc": GraphQLField(GraphQLString, resolve=resolve_abc)},
)
schema = GraphQLSchema(query=QueryType)
graphql_app = GraphQLRouter[None, None](schema)
app.include_router(graphql_app, prefix="/graphql")
test_client = TestClient(app)
response = test_client.post("/graphql", json={"query": "{ abc }"})
assert response.status_code == 200
assert response.json() == {"data": {"abc": "abc"}}
def test_graphql_router_path():
from starlette.testclient import TestClient
from fastapi import FastAPI
from graphql_server.fastapi import GraphQLRouter
app = FastAPI()
QueryType = GraphQLObjectType(
name="Query",
fields={"abc": GraphQLField(GraphQLString, resolve=resolve_abc)},
)
schema = GraphQLSchema(query=QueryType)
graphql_app = GraphQLRouter[None, None](schema, path="/graphql")
app.include_router(graphql_app)
test_client = TestClient(app)
response = test_client.post("/graphql", json={"query": "{ abc }"})
assert response.status_code == 200
assert response.json() == {"data": {"abc": "abc"}}
def test_missing_path_and_prefix():
from fastapi import FastAPI
from graphql_server.fastapi import GraphQLRouter
app = FastAPI()
QueryType = GraphQLObjectType(
name="Query",
fields={"abc": GraphQLField(GraphQLString, resolve=resolve_abc)},
)
schema = GraphQLSchema(query=QueryType)
graphql_app = GraphQLRouter[None, None](schema)
with pytest.raises(Exception) as exc:
app.include_router(graphql_app)
assert "Prefix and path cannot be both empty" in str(exc)