-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_graphql_api.py
67 lines (49 loc) · 2.23 KB
/
test_graphql_api.py
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
67
import pytest
from starlette.testclient import TestClient
def test_get_querystring(client: TestClient):
response = client.get("/?query={ hello }")
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
@pytest.mark.parametrize("path", ("/", "/?foo=bar", "/?q={ hello }"))
def test_get_no_query(client: TestClient, path: str):
response = client.get(path)
assert response.status_code == 400
assert response.text == "No GraphQL query found in the request"
def test_post_querystring(client: TestClient):
response = client.post("/?query={ hello }")
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_post_json(client: TestClient):
response = client.post("/", json={"query": "{ hello }"})
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_post_invalid_json(client: TestClient):
response = client.post(
"/", data="{test", headers={"content-type": "application/json"}
)
assert response.status_code == 400
assert response.json() == {"error": "Invalid JSON."}
def test_post_graphql(client: TestClient):
response = client.post(
"/", data="{ hello }", headers={"content-type": "application/graphql"}
)
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_post_invalid_media_type(client: TestClient):
response = client.post("/", data="{ hello }", headers={"content-type": "dummy"})
assert response.status_code == 415
assert response.text == "Unsupported Media Type"
def test_put(client: TestClient):
response = client.put("/", json={"query": "{ hello }"})
assert response.status_code == 405
assert response.text == "Method Not Allowed"
def test_error_handling(client: TestClient):
response = client.post("/", json={"query": "{ dummy }"})
assert response.status_code == 400
json = response.json()
assert json["data"] is None
assert len(json["errors"]) == 1
error = json["errors"][0]
assert error["locations"] == [{"column": 3, "line": 1}]
assert "dummy" in error["message"]
assert error["path"] == ["dummy"]