/
todo_test.go
279 lines (237 loc) · 7.01 KB
/
todo_test.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package todo
import (
"testing"
"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/introspection"
"github.com/stretchr/testify/require"
)
func TestTodo(t *testing.T) {
c := client.New(handler.NewDefaultServer(NewExecutableSchema(New())))
var resp struct {
CreateTodo struct{ ID string }
}
c.MustPost(`mutation { createTodo(todo:{text:"Fery important"}) { id } }`, &resp)
require.Equal(t, "5", resp.CreateTodo.ID)
t.Run("update the todo text", func(t *testing.T) {
var resp struct {
UpdateTodo struct{ Text string }
}
c.MustPost(
`mutation($id: ID!, $text: String!) { updateTodo(id: $id, changes:{text:$text}) { text } }`,
&resp,
client.Var("id", 5),
client.Var("text", "Very important"),
)
require.Equal(t, "Very important", resp.UpdateTodo.Text)
})
t.Run("get __typename", func(t *testing.T) {
var resp struct {
Todo struct {
Typename string `json:"__typename"`
}
}
c.MustPost(`{ todo(id: 5) { __typename } }`, &resp)
require.Equal(t, "Todo", resp.Todo.Typename)
})
t.Run("update the todo status", func(t *testing.T) {
var resp struct {
UpdateTodo struct{ Text string }
}
c.MustPost(`mutation { updateTodo(id: 5, changes:{done:true}) { text } }`, &resp)
require.Equal(t, "Very important", resp.UpdateTodo.Text)
})
t.Run("update the todo status by user id in mutation", func(t *testing.T) {
var resp struct {
UpdateTodo struct {
Text string
Done bool
}
}
c.MustPost(`mutation @user(id:2){ updateTodo(id: 3, changes:{done:true}) { text, done } }`, &resp)
require.Equal(t, "Somebody else's todo", resp.UpdateTodo.Text)
})
t.Run("update the todo status by user id in field", func(t *testing.T) {
var resp struct {
UpdateTodo struct {
Text string
Done bool
}
}
c.MustPost(`mutation { updateTodo(id: 3, changes:{done:true})@user(id:2) { text, done } }`, &resp)
require.Equal(t, "Somebody else's todo", resp.UpdateTodo.Text)
})
t.Run("failed update the todo status by user id in field", func(t *testing.T) {
var resp struct {
UpdateTodo *struct {
Text string
Done bool
}
}
err := c.Post(`mutation { updateTodo(id: 3, changes:{done:true}) { text, done } }`, &resp)
require.EqualError(t, err, "[{\"message\":\"you dont own that\",\"path\":[\"updateTodo\",\"done\"]}]")
require.Nil(t, resp.UpdateTodo)
})
t.Run("select with alias", func(t *testing.T) {
var resp struct {
A struct{ Text string }
B struct{ ID string }
}
c.MustPost(`{ a: todo(id:1) { text } b: todo(id:2) { id } }`, &resp)
require.Equal(t, "A todo not to forget", resp.A.Text)
require.Equal(t, "2", resp.B.ID)
})
t.Run("find a missing todo", func(t *testing.T) {
var resp struct {
Todo *struct{ Text string }
}
err := c.Post(`{ todo(id:99) { text } }`, &resp)
require.Error(t, err)
require.Nil(t, resp.Todo)
})
t.Run("get done status of unowned todo", func(t *testing.T) {
var resp struct {
Todo *struct {
Text string
Done bool
}
}
err := c.Post(`{ todo(id:3) { text, done } }`, &resp)
require.EqualError(t, err, `[{"message":"you dont own that","path":["todo","done"]}]`)
require.Nil(t, resp.Todo)
})
t.Run("test panic", func(t *testing.T) {
var resp struct {
Todo *struct{ Text string }
}
err := c.Post(`{ todo(id:666) { text } }`, &resp)
require.EqualError(t, err, `[{"message":"internal system error","path":["todo"]}]`)
})
t.Run("select all", func(t *testing.T) {
var resp struct {
Todo struct {
ID string
Text string
Done bool
}
LastTodo struct {
ID string
Text string
Done bool
}
Todos []struct {
ID string
Text string
}
}
c.MustPost(`{
todo(id:1) { id done text }
lastTodo { id text done }
todos { id text }
}`, &resp)
require.Equal(t, "1", resp.Todo.ID)
require.Equal(t, "5", resp.LastTodo.ID)
require.Len(t, resp.Todos, 5)
require.Equal(t, "Very important", resp.LastTodo.Text)
require.Equal(t, "5", resp.LastTodo.ID)
})
t.Run("introspection", func(t *testing.T) {
// Make sure we can run the graphiql introspection query without errors
var resp interface{}
c.MustPost(introspection.Query, &resp)
})
t.Run("null optional field", func(t *testing.T) {
var resp struct {
CreateTodo struct{ Text string }
}
c.MustPost(`mutation { createTodo(todo:{text:"Completed todo", done: null}) { text } }`, &resp)
require.Equal(t, "Completed todo", resp.CreateTodo.Text)
})
}
func TestSkipAndIncludeDirectives(t *testing.T) {
c := client.New(handler.NewDefaultServer(NewExecutableSchema(New())))
t.Run("skip on field", func(t *testing.T) {
var resp map[string]interface{}
c.MustPost(`{ todo(id: 1) @skip(if:true) { __typename } }`, &resp)
_, ok := resp["todo"]
require.False(t, ok)
})
t.Run("skip on variable", func(t *testing.T) {
q := `query Test($cond: Boolean!) { todo(id: 1) @skip(if: $cond) { __typename } }`
var resp map[string]interface{}
c.MustPost(q, &resp, client.Var("cond", true))
_, ok := resp["todo"]
require.False(t, ok)
c.MustPost(q, &resp, client.Var("cond", false))
_, ok = resp["todo"]
require.True(t, ok)
})
t.Run("skip on inline fragment", func(t *testing.T) {
var resp struct {
Todo struct {
Typename string `json:"__typename"`
}
}
c.MustPost(`{ todo(id: 1) {
...@skip(if:true) {
__typename
}
}
}`, &resp)
require.Empty(t, resp.Todo.Typename)
})
t.Run("skip on fragment", func(t *testing.T) {
var resp struct {
Todo struct {
Typename string `json:"__typename"`
}
}
c.MustPost(`
{
todo(id: 1) {
...todoFragment @skip(if:true)
}
}
fragment todoFragment on Todo {
__typename
}
`, &resp)
require.Empty(t, resp.Todo.Typename)
})
t.Run("include on field", func(t *testing.T) {
q := `query Test($cond: Boolean!) { todo(id: 1) @include(if: $cond) { __typename } }`
var resp map[string]interface{}
c.MustPost(q, &resp, client.Var("cond", true))
_, ok := resp["todo"]
require.True(t, ok)
c.MustPost(q, &resp, client.Var("cond", false))
_, ok = resp["todo"]
require.False(t, ok)
})
t.Run("both skip and include defined", func(t *testing.T) {
type TestCase struct {
Skip bool
Include bool
Expected bool
}
table := []TestCase{
{Skip: true, Include: true, Expected: false},
{Skip: true, Include: false, Expected: false},
{Skip: false, Include: true, Expected: true},
{Skip: false, Include: false, Expected: false},
}
q := `query Test($skip: Boolean!, $include: Boolean!) { todo(id: 1) @skip(if: $skip) @include(if: $include) { __typename } }`
for _, tc := range table {
var resp map[string]interface{}
c.MustPost(q, &resp, client.Var("skip", tc.Skip), client.Var("include", tc.Include))
_, ok := resp["todo"]
require.Equal(t, tc.Expected, ok)
}
})
t.Run("skip with default query argument", func(t *testing.T) {
var resp map[string]interface{}
c.MustPost(`query Test($skip: Boolean = true) { todo(id: 1) @skip(if: $skip) { __typename } }`, &resp)
_, ok := resp["todo"]
require.False(t, ok)
})
}