forked from udacity/graphb
-
Notifications
You must be signed in to change notification settings - Fork 3
/
query_test.go
66 lines (53 loc) · 1.66 KB
/
query_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
package graphb
import (
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestQuery_checkName(t *testing.T) {
q := Query{Name: "1"}
err := q.checkName()
assert.IsType(t, InvalidNameErr{}, errors.Cause(err))
assert.Equal(t, "'1' is an invalid operation name in GraphQL. A valid name matches /[_A-Za-z][_0-9A-Za-z]*/, see: http://facebook.github.io/graphql/October2016/#sec-Names", err.Error())
}
func TestQuery_check(t *testing.T) {
q := Query{Name: "1", Type: TypeQuery}
err := q.check()
assert.IsType(t, InvalidNameErr{}, errors.Cause(err))
assert.Equal(t, "'1' is an invalid operation name in GraphQL. A valid name matches /[_A-Za-z][_0-9A-Za-z]*/, see: http://facebook.github.io/graphql/October2016/#sec-Names", err.Error())
}
func TestQuery_GetField(t *testing.T) {
q := MakeQuery(TypeQuery).SetFields(MakeField("f1"))
f := q.GetField("f1")
assert.Equal(t, "f1", f.Name)
f = q.GetField("f2")
assert.Nil(t, f)
}
func TestQuery_JSON(t *testing.T) {
t.Parallel()
t.Run("Arguments can be nested structures", func(t *testing.T) {
t.Parallel()
q := NewQuery(TypeMutation).
SetFields(
NewField("createQuestion").
SetArguments(
ArgumentCustomType(
"input",
ArgumentString("title", "what"),
ArgumentString("content", "what"),
ArgumentStringSlice("tagIds"),
),
).
SetFields(
NewField("question", OfFields("id")),
),
)
c := q.stringChan()
var strs []string
for str := range c {
strs = append(strs, str)
}
assert.Equal(t, `mutation{createQuestion(input:{title:"what",content:"what",tagIds:[]}){question{id}}}`, strings.Join(strs, ""))
})
}