-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
scalar_test.go
82 lines (69 loc) · 2.32 KB
/
scalar_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
package scalars
import (
"testing"
"time"
"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/introspection"
"github.com/stretchr/testify/require"
)
type RawUser struct {
ID string
Name string
Created int64
Address struct{ Location string }
PrimitiveResolver string
CustomResolver string
Tier string
}
func TestScalars(t *testing.T) {
c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{}})))
t.Run("marshaling", func(t *testing.T) {
var resp struct {
User RawUser
Search []RawUser
}
c.MustPost(`{
user(id:"=1=") {
...UserData
}
search(input:{location:"6,66", createdAfter:666}) {
...UserData
}
}
fragment UserData on User { id name created tier address { location } }`, &resp)
require.Equal(t, "1,2", resp.User.Address.Location)
// There can be a delay between creation and test assertion, so we
// give some leeway to eliminate false positives.
require.WithinDuration(t, time.Now(), time.Unix(resp.User.Created, 0), 5*time.Second)
require.Equal(t, "6,66", resp.Search[0].Address.Location)
require.Equal(t, int64(666), resp.Search[0].Created)
require.Equal(t, "A", resp.Search[0].Tier)
})
t.Run("default search location", func(t *testing.T) {
var resp struct{ Search []RawUser }
err := c.Post(`{ search { address { location } } }`, &resp)
require.NoError(t, err)
require.Equal(t, "37,144", resp.Search[0].Address.Location)
})
t.Run("custom error messages", func(t *testing.T) {
var resp struct{ Search []RawUser }
err := c.Post(`{ search(input:{createdAfter:"2014"}) { id } }`, &resp)
require.EqualError(
t,
err,
`[{"message":"time should be a unix timestamp","path":["search","input","createdAfter"]}]`,
)
})
t.Run("scalar resolver methods", func(t *testing.T) {
var resp struct{ User RawUser }
c.MustPost(`{ user(id: "=1=") { primitiveResolver, customResolver } }`, &resp)
require.Equal(t, "test", resp.User.PrimitiveResolver)
require.Equal(t, "5,1", resp.User.CustomResolver)
})
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)
})
}