-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql_test.go
287 lines (260 loc) · 9.98 KB
/
graphql_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
280
281
282
283
284
285
286
287
package kosmo
import (
"errors"
"reflect"
"testing"
"github.com/graphql-go/graphql"
. "github.com/smartystreets/goconvey/convey"
)
type TForNestingStruct struct {
Field1 string
}
type TForNestingSlice []TForNestingStruct
type TNativeFieldToGraphQLStruct struct {
Field1 TForNestingStruct
Field2 TForNestingSlice
Field3 string `description:"Test"`
}
type TNativeFieldToGraphQLStructWithIgnoredFields struct {
Field1 TForNestingStruct `kosmo:"ignore"`
Field2 TForNestingSlice `kosmo:"ignore"`
Field3 string `description:"Test"`
}
type TResolverArguments struct {
Field1 string
Field2 int
}
type TResolverArgumentsWithIngoredFields struct {
Field1 string `kosmo:"ignore"`
Field2 int
}
type TResolverArgumentsWithRequiredFields struct {
Field1 string `kosmo:"require"`
Field2 int
}
func TFunctionToResolverWithNoArgs() (TForNestingStruct, error) {
return TForNestingStruct{
Field1: "Test",
}, nil
}
func TFunctionToResolverWithArgs(args TResolverArguments) (TForNestingStruct, error) {
return TForNestingStruct{
Field1: args.Field1,
}, errors.New("Test")
}
func TFunctionToResolverWithArgsAndIgnoredFields(args TResolverArgumentsWithIngoredFields) (TForNestingStruct, error) {
return TForNestingStruct{
Field1: args.Field1,
}, errors.New("Test")
}
func TFunctionToResolverWithArgsAndRequiredFields(args TResolverArgumentsWithRequiredFields) (TForNestingStruct, error) {
return TForNestingStruct{
Field1: args.Field1,
}, errors.New("Test")
}
func TestReflectFunctionInformations(t *testing.T) {
Convey("Given a function", t, func() {
infos := reflectFunctionInformations(TFunctionToResolverWithArgs)
Convey("It should assemble all informations needed for building a graphql resolver", func() {
So(infos.metaInformations.name, ShouldEqual, "TFunctionToResolverWithArgs")
So(infos.args, ShouldNotBeEmpty)
So(infos.resolver, ShouldNotBeEmpty)
})
})
}
func TestReflectTypeInformations(t *testing.T) {
Convey("Given a struct", t, func() {
infos := reflectTypeInformations(TNativeFieldToGraphQLStruct{})
Convey("It should assemble all informations needed for building a graphql schema", func() {
So(infos.metaInformations.description, ShouldEqual, "")
So(infos.typ, ShouldNotBeEmpty)
})
})
Convey("Given a slice", t, func() {
infos := reflectTypeInformations(TForNestingSlice{})
Convey("It should assemble all informations needed for building a graphql schema", func() {
So(infos.metaInformations.description, ShouldEqual, "")
So(infos.typ, ShouldNotBeEmpty)
})
})
}
func TestFunctionToConfigArguments(t *testing.T) {
Convey("Given a reflect.Value of a function", t, func() {
Convey("In case the function has no args", func() {
fieldConfigArgument := functionToConfigArguments(reflect.ValueOf(TFunctionToResolverWithNoArgs))
Convey("It should return a empty graphql.FieldConfigArgument", func() {
So(fieldConfigArgument, ShouldBeEmpty)
})
})
Convey("In case the function has args", func() {
fieldConfigArgument := functionToConfigArguments(reflect.ValueOf(TFunctionToResolverWithArgs))
Convey("It should return a graphql.FieldConfigArgument with all the fields from its arguments type", func() {
So(fieldConfigArgument, ShouldNotBeEmpty)
So(fieldConfigArgument["Field1"], ShouldNotBeEmpty)
So(fieldConfigArgument["Field2"], ShouldNotBeEmpty)
})
})
Convey("In case some args in the function are marked as ignored", func() {
fieldConfigArgument := functionToConfigArguments(reflect.ValueOf(TFunctionToResolverWithArgsAndIgnoredFields))
Convey("The ignored fields should not be returned arguments", func() {
So(fieldConfigArgument, ShouldNotBeEmpty)
So(fieldConfigArgument["Field1"], ShouldBeNil)
So(fieldConfigArgument["Field2"], ShouldNotBeEmpty)
})
})
Convey("In case some args in the function are marked as required", func() {
fieldConfigArgument := functionToConfigArguments(reflect.ValueOf(TFunctionToResolverWithArgsAndRequiredFields))
Convey("The required function should be a non null type", func() {
So(fieldConfigArgument, ShouldNotBeEmpty)
So(fieldConfigArgument["Field1"], ShouldNotBeEmpty)
So(fieldConfigArgument["Field2"], ShouldNotBeEmpty)
})
})
})
}
func TestFunctionToResolver(t *testing.T) {
Convey("Given a reflect.Value of a function", t, func() {
Convey("In case the function has no args", func() {
fn := functionToResolver(reflect.ValueOf(TFunctionToResolverWithNoArgs))
Convey("It should call the passed in function and return it's results", func() {
val, err := fn(graphql.ResolveParams{})
So(val, ShouldResemble, TForNestingStruct{
Field1: "Test",
})
So(err, ShouldBeNil)
})
})
Convey("In case the function has args", func() {
Convey("The returned function should map the graphql.ResolveParams to the passed in function and return its results", func() {
args := make(map[string]interface{})
args["Field1"] = "Test1"
args["Field2"] = 2
fn := functionToResolver(reflect.ValueOf(TFunctionToResolverWithArgs))
val, err := fn(graphql.ResolveParams{
Args: args,
})
So(val, ShouldResemble, TForNestingStruct{
Field1: "Test1",
})
So(err, ShouldNotBeNil)
})
})
})
}
func TestStructToGraph(t *testing.T) {
Convey("Given a struct", t, func() {
obj := structToGraph(TForNestingStruct{})
Convey("It should produce a object of its type", func() {
So(obj.Name(), ShouldEqual, "TForNestingStruct")
})
})
}
func TestSliceToGraph(t *testing.T) {
Convey("Given a slice", t, func() {
list := sliceToGraph(TForNestingSlice{})
Convey("It should produce a list of its underling type", func() {
So(list.OfType.Name(), ShouldEqual, "TForNestingStruct")
})
})
}
func TestStructToGraphConfig(t *testing.T) {
Convey("Given a struct", t, func() {
objConf := structToGraphConfig(TForNestingStruct{})
Convey("The name of the returned config should be the name of its type", func() {
So(objConf.Name, ShouldEqual, "TForNestingStruct")
})
})
}
func TestSliceToGraphConfig(t *testing.T) {
Convey("Given a slice", t, func() {
objConf := sliceToGraphConfig(TForNestingSlice{})
Convey("The name of the returned config should be the name from the underling type", func() {
So(objConf.Name, ShouldEqual, "TForNestingStruct")
})
})
}
func TestBuildObjectConfigFromType(t *testing.T) {
Convey("Given a reflect.Type of a struct", t, func() {
objConf := buildObjectConfigFromType(reflect.TypeOf(TNativeFieldToGraphQLStruct{}))
Convey("The name of the returned config should be the name from the type of the passed in struct", func() {
So(objConf.Name, ShouldEqual, "TNativeFieldToGraphQLStruct")
})
Convey("The length of the returned fields should be the number of fields in given struct", func() {
So(len(objConf.Fields.(graphql.Fields)), ShouldEqual, 3)
})
})
}
func TestNativeFieldsToGraphQLFields(t *testing.T) {
Convey("Given reflected fields of a struct", t, func() {
Convey("All reflected fields should be mapped to graphql.Fields", func() {
_, fields := reflectStructInformations(reflect.TypeOf(TNativeFieldToGraphQLStruct{}))
gqlFields := nativeFieldsToGraphQLFields(fields)
Convey("The length of the returned fields should equal", func() {
So(len(gqlFields), ShouldEqual, len(fields))
})
Convey("Names of the returned fields should be the name of the underling type", func() {
So(gqlFields["Field1"].Type.Name(), ShouldEqual, "TForNestingStruct")
So(gqlFields["Field2"].Type.Name(), ShouldEqual, "TForNestingStruct")
So(gqlFields["Field3"].Type.Name(), ShouldEqual, "String")
})
Convey("In case a field has a 'description' tag, the description of the field should be the value of its tag", func() {
So(gqlFields["Field3"].Description, ShouldEqual, "Test")
})
Convey("In case a field has no 'description' tag, the description of the field should be a empty string", func() {
So(gqlFields["Field1"].Description, ShouldEqual, "")
})
})
Convey("In case a field is ignored", func() {
_, fields := reflectStructInformations(reflect.TypeOf(TNativeFieldToGraphQLStructWithIgnoredFields{}))
gqlFields := nativeFieldsToGraphQLFields(fields)
Convey("It should not be in the returned slice of fields", func() {
So(len(gqlFields), ShouldEqual, 1)
So(gqlFields["Field3"].Type.Name(), ShouldEqual, "String")
})
})
})
}
func TestNativeFieldToGraphQL(t *testing.T) {
Convey("Given a reflected field of a struct", t, func() {
_, fields := reflectStructInformations(reflect.TypeOf(TNativeFieldToGraphQLStruct{}))
Convey("In case the input is a struct", func() {
gqlField := nativeFieldToGraphQL(fields[0])
Convey("The field type name should be the type of the struct", func() {
So(gqlField.Type.Name(), ShouldEqual, "TForNestingStruct")
})
})
Convey("In case the input is a slice", func() {
gqlField := nativeFieldToGraphQL(fields[1])
Convey("The field type name should be the name of the underling slice type", func() {
So(gqlField.Type.Name(), ShouldEqual, "TForNestingStruct")
})
})
Convey("In case the input is a primitive", func() {
gqlField := nativeFieldToGraphQL(fields[2])
Convey("The field type name should be the name of the mapped graphql.Type", func() {
So(gqlField.Type.Name(), ShouldEqual, "String")
})
})
})
}
func TestNativeTypeToGraphQL(t *testing.T) {
Convey("Given a name of a primitive type", t, func() {
var typeUInt uint
var typeInt int
var typeString string
var typeFloat32 float32
var typeFloat64 float64
Convey("The corresponding graphQL type should be returned", func() {
So(nativeTypeToGraphQL(getType(typeInt)), ShouldEqual, graphql.Int)
So(nativeTypeToGraphQL(getType(typeUInt)), ShouldEqual, graphql.Int)
So(nativeTypeToGraphQL(getType(typeString)), ShouldEqual, graphql.String)
So(nativeTypeToGraphQL(getType(typeFloat32)), ShouldEqual, graphql.Float)
So(nativeTypeToGraphQL(getType(typeFloat64)), ShouldEqual, graphql.Float)
})
Convey("The function should panic if the given type is not available", func() {
So(func() {
nativeTypeToGraphQL("garbage")
}, ShouldPanic)
})
})
}