forked from graphql-python/gql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dsl.py
153 lines (122 loc) · 3.4 KB
/
test_dsl.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pytest
from gql import Client
from gql.dsl import DSLSchema
from .schema import StarWarsSchema
@pytest.fixture
def ds():
client = Client(schema=StarWarsSchema)
ds = DSLSchema(client)
return ds
def test_invalid_field_on_type_query(ds):
with pytest.raises(KeyError) as exc_info:
ds.Query.extras.select(ds.Character.name)
assert "Field extras does not exist in type Query." in str(exc_info.value)
def test_incompatible_query_field(ds):
with pytest.raises(Exception) as exc_info:
ds.query("hero")
assert "Received incompatible query field" in str(exc_info.value)
def test_hero_name_query(ds):
query = """
hero {
name
}
""".strip()
query_dsl = ds.Query.hero.select(ds.Character.name)
assert query == str(query_dsl)
def test_hero_name_and_friends_query(ds):
query = """
hero {
id
name
friends {
name
}
}
""".strip()
query_dsl = ds.Query.hero.select(
ds.Character.id,
ds.Character.name,
ds.Character.friends.select(ds.Character.name,),
)
assert query == str(query_dsl)
def test_hero_id_and_name(ds):
query = """
hero {
id
name
}
""".strip()
query_dsl = ds.Query.hero.select(ds.Character.id)
query_dsl = query_dsl.select(ds.Character.name)
assert query == str(query_dsl)
def test_nested_query(ds):
query = """
hero {
name
friends {
name
appearsIn
friends {
name
}
}
}
""".strip()
query_dsl = ds.Query.hero.select(
ds.Character.name,
ds.Character.friends.select(
ds.Character.name,
ds.Character.appears_in,
ds.Character.friends.select(ds.Character.name),
),
)
assert query == str(query_dsl)
def test_fetch_luke_query(ds):
query = """
human(id: "1000") {
name
}
""".strip()
query_dsl = ds.Query.human(id="1000").select(ds.Human.name,)
assert query == str(query_dsl)
def test_fetch_luke_aliased(ds):
query = """
luke: human(id: "1000") {
name
}
""".strip()
query_dsl = ds.Query.human.args(id=1000).alias("luke").select(ds.Character.name,)
assert query == str(query_dsl)
def test_hero_name_query_result(ds):
result = ds.query(ds.Query.hero.select(ds.Character.name))
expected = {"hero": {"name": "R2-D2"}}
assert result == expected
def test_arg_serializer_list(ds):
result = ds.query(
ds.Query.characters.args(ids=[1000, 1001, 1003]).select(ds.Character.name,)
)
expected = {
"characters": [
{"name": "Luke Skywalker"},
{"name": "Darth Vader"},
{"name": "Leia Organa"},
]
}
assert result == expected
def test_arg_serializer_enum(ds):
result = ds.query(ds.Query.hero.args(episode=5).select(ds.Character.name))
expected = {"hero": {"name": "Luke Skywalker"}}
assert result == expected
def test_create_review_mutation_result(ds):
result = ds.mutate(
ds.Mutation.createReview.args(
episode=6, review={"stars": 5, "commentary": "This is a great movie!"}
).select(ds.Review.stars, ds.Review.commentary)
)
expected = {"createReview": {"stars": 5, "commentary": "This is a great movie!"}}
assert result == expected
def test_invalid_arg(ds):
with pytest.raises(
KeyError, match="Argument invalid_arg does not exist in Field: Character."
):
ds.query(ds.Query.hero.args(invalid_arg=5).select(ds.Character.name))