-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
enums_test.go
53 lines (45 loc) · 1.58 KB
/
enums_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
package followschema
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql/handler"
)
func TestEnumsResolver(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.EnumInInput = func(ctx context.Context, input *InputWithEnumValue) (EnumTest, error) {
return input.Enum, nil
}
c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
t.Run("input with valid enum value", func(t *testing.T) {
var resp struct {
EnumInInput EnumTest
}
c.MustPost(`query {
enumInInput(input: {enum: OK})
}
`, &resp)
require.Equal(t, EnumTestOk, resp.EnumInInput)
})
t.Run("input with invalid enum value", func(t *testing.T) {
var resp struct {
EnumInInput EnumTest
}
err := c.Post(`query {
enumInInput(input: {enum: INVALID})
}
`, &resp)
require.EqualError(t, err, `http 422: {"errors":[{"message":"Value \"INVALID\" does not exist in \"EnumTest!\" enum.","locations":[{"line":2,"column":30}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
})
t.Run("input with invalid enum value via vars", func(t *testing.T) {
var resp struct {
EnumInInput EnumTest
}
err := c.Post(`query ($input: InputWithEnumValue) {
enumInInput(input: $input)
}
`, &resp, client.Var("input", map[string]interface{}{"enum": "INVALID"}))
require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid EnumTest","path":["variable","input","enum"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
})
}