forked from lab259/go-graphql-struct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
array_test.go
112 lines (96 loc) · 3.64 KB
/
array_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
package gqlstruct_test
import (
"github.com/lab259/go-graphql-struct"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"reflect"
)
var _ = Describe("ArrayOf", func() {
Describe("String", func() {
It("should generate graphql.Type with a string array field", func() {
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(""))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[String]"))
})
It("should generate graphql.Type with a string pointer array field", func() {
s := ""
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(&s))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[String]"))
})
})
Describe("Int", func() {
It("should generate graphql.Type with a int array field", func() {
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(123))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[Int]"))
})
It("should generate graphql.Type with a int pointer array field", func() {
s := 123
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(&s))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[Int]"))
})
})
Describe("Float", func() {
It("should generate graphql.Type with a float array field", func() {
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(123.))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[Float]"))
})
It("should generate graphql.Type with a float pointer array field", func() {
s := 123.
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(&s))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[Float]"))
})
})
Describe("Custom Type", func() {
It("should generate graphql.Type with a int array field", func() {
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(CustomFieldType{}))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[CustomFieldType]"))
})
It("should generate graphql.Type with a int pointer array field", func() {
obj, err := gqlstruct.NewEncoder().ArrayOf(reflect.TypeOf(&CustomFieldType{}))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[CustomFieldType]"))
})
It("should generate graphql.Type with a int pointer array field from the cache", func() {
enc := gqlstruct.NewEncoder()
obj, err := enc.ArrayOf(reflect.TypeOf(&CustomFieldType{}))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[CustomFieldType]"))
obj, err = enc.ArrayOf(reflect.TypeOf(&CustomFieldType{}))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[CustomFieldType]"))
})
It("should panic when find a type not supported", func() {
type CustomFieldTypeWithNoGraphQLConverted interface{}
type StructExampleNotFound struct {
Field1 []CustomFieldTypeWithNoGraphQLConverted `graphql:"field1"`
}
enc := gqlstruct.NewEncoder()
_, err := enc.ArrayOf(reflect.TypeOf(&StructExampleNotFound{}))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("not recognized"))
})
It("should generate graphql.Type with a int pointer array field using the global", func() {
enc := gqlstruct.NewEncoder()
obj, err := enc.ArrayOf(reflect.TypeOf(&CustomFieldType{}))
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.String()).To(Equal("[CustomFieldType]"))
})
})
})