-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkosmo_test.go
135 lines (123 loc) · 4.14 KB
/
kosmo_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
package kosmo
import (
"testing"
"github.com/graphql-go/graphql"
. "github.com/smartystreets/goconvey/convey"
)
type TKosmoStruct struct {
Name string
Number int
}
type TAnotherKosmoStruct struct {
Name string
}
func TResolveKosmoStruct() (TKosmoStruct, error) {
return TKosmoStruct{
Name: "Test",
Number: 1,
}, nil
}
func TResolveAnotherKosmoStruct() (TKosmoStruct, error) {
return TKosmoStruct{
Name: "Test",
Number: 1,
}, nil
}
func TestType(t *testing.T) {
Convey("Given a structure", t, func() {
schema := Type(TKosmoStruct{})
Convey("It will reflect the type information of the given structure", func() {
Convey("And will set the resolverType to the reflected graphql.Type", func() {
infos := reflectTypeInformations(TKosmoStruct{})
So(schema.resolverType, ShouldResemble, infos.typ)
})
})
})
}
func TestQueries(t *testing.T) {
Convey("Called on a GraphQLSchema, given a function", t, func() {
schema := Type(TKosmoStruct{})
schema.Queries(TResolveKosmoStruct)
Convey("The root 'Queries' should be added", func() {
Convey("And the given function should be used to resolve the previously given struct", func() {
field := schema.query.Fields.(graphql.Fields)["TResolveKosmoStruct"]
infos := reflectFunctionInformations(TResolveKosmoStruct)
So(field.Args, ShouldResemble, infos.args)
So(field.Type, ShouldResemble, schema.resolverType)
So(field.Description, ShouldEqual, infos.description)
})
})
})
}
func TestMutations(t *testing.T) {
Convey("Called on a GraphQLSchema, given a function", t, func() {
schema := Type(TKosmoStruct{})
schema.Queries(TResolveKosmoStruct)
schema.Mutations(TResolveKosmoStruct)
Convey("The root 'Mutation' should be added", func() {
infos := reflectFunctionInformations(TResolveKosmoStruct)
field := schema.mutations.Fields.(graphql.Fields)["TResolveKosmoStruct"]
So(field.Args, ShouldResemble, infos.args)
So(field.Description, ShouldEqual, infos.description)
So(field.Type, ShouldResemble, schema.resolverType)
})
})
}
func TestSchemas(t *testing.T) {
svc := Service{
GraphQLConfig: GraphQLConfig{
RemoveResolverPrefixes: true,
ResolverPrefixes: []string{"T"},
},
}
schema1 := Type(TKosmoStruct{}).Queries(TResolveKosmoStruct).Mutations(TResolveAnotherKosmoStruct)
schema2 := Type(TAnotherKosmoStruct{}).Queries(TResolveAnotherKosmoStruct).Mutations(TResolveKosmoStruct)
Convey("Called on a Service, given no GraphQLSchema", t, func() {
svcCopy1 := svc.Schemas()
So(svcCopy1.graphQL.Query, ShouldBeNil)
So(svcCopy1.graphQL.Mutation, ShouldBeNil)
})
Convey("Called on a Service, given one or more GraphQLSchema", t, func() {
svcCopy2 := svc.Schemas(schema1, schema2)
So(svcCopy2.graphQL.Query, ShouldNotBeNil)
So(svcCopy2.graphQL.Mutation, ShouldNotBeNil)
})
}
func TestServer(t *testing.T) {
svc := Service{}
schema1 := Type(TKosmoStruct{}).Queries(TResolveKosmoStruct).Mutations(TResolveAnotherKosmoStruct)
schema2 := Type(TAnotherKosmoStruct{}).Queries(TResolveAnotherKosmoStruct).Mutations(TResolveKosmoStruct)
Convey("Called on a Service", t, func() {
Convey("In case the schema is not error free", func() {
Convey("It should panic with the returned error message", func() {
So(func() {
svc.Schemas().Server()
}, ShouldPanic)
})
})
Convey("In case the schema is error free", func() {
Convey("It should return a http.Server", func() {
So(svc.Schemas(schema1, schema2).Server(), ShouldNotBeEmpty)
})
})
})
}
func TestHandler(t *testing.T) {
svc := Service{}
schema1 := Type(TKosmoStruct{}).Queries(TResolveKosmoStruct).Mutations(TResolveAnotherKosmoStruct)
schema2 := Type(TAnotherKosmoStruct{}).Queries(TResolveAnotherKosmoStruct).Mutations(TResolveKosmoStruct)
Convey("Called on a Service", t, func() {
Convey("In case the schema is not error free", func() {
Convey("It should panic with the returned error message", func() {
So(func() {
svc.Schemas().Handler()
}, ShouldPanic)
})
})
Convey("In case the schema is error free", func() {
Convey("It should return a http.Handler", func() {
So(svc.Schemas(schema1, schema2).Handler(), ShouldNotBeEmpty)
})
})
})
}