-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencoder_test.go
More file actions
96 lines (80 loc) · 2.69 KB
/
Copy pathencoder_test.go
File metadata and controls
96 lines (80 loc) · 2.69 KB
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
package gqlstruct_test
import (
"github.com/graphql-go/graphql"
"github.com/lab259/go-graphql-struct"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type CustomFieldType struct {
Value string
}
func (t *CustomFieldType) GraphqlType() graphql.Type {
return graphql.Float
}
type CustomFieldTypeWithResolver struct {
Value string
}
func (t *CustomFieldTypeWithResolver) GraphqlResolve(p graphql.ResolveParams) (interface{}, error) {
panic("only to catch")
}
func (t *CustomFieldTypeWithResolver) GraphqlType() graphql.Type {
return graphql.Float
}
var _ = Describe("Struct", func() {
It("should ignore not tagged fields", func() {
type StructExample struct {
Field1 string `graphql:"field1"`
Field2 int
Field3 bool `graphql:"field3"`
}
obj, err := gqlstruct.NewEncoder().Struct(&StructExample{})
Expect(err).ToNot(HaveOccurred())
Expect(obj).ToNot(BeNil())
Expect(obj.Name()).To(Equal("StructExample"))
fields := obj.Fields()
Expect(fields).ToNot(BeNil())
Expect(fields).To(HaveLen(2))
Expect(fields).To(HaveKey("field1"))
Expect(fields).To(HaveKey("field3"))
Expect(fields["field1"].Name).To(Equal("field1"))
Expect(fields["field1"].Type.String()).To(Equal("String"))
Expect(fields["field3"].Name).To(Equal("field3"))
Expect(fields["field3"].Type.String()).To(Equal("Boolean"))
})
It("should create an object with options", func() {
type StructExample struct {
Field1 string `graphql:"field1"`
Field2 string `graphql:"field2"`
}
obj, err := gqlstruct.NewEncoder().Field(&StructExample{}, gqlstruct.WithDescription("Description 1"))
Expect(err).ToNot(HaveOccurred())
Expect(obj.Description).To(Equal("Description 1"))
})
It("should fail when a option fails", func() {
type StructExample struct {
Field1 string `graphql:"field1"`
Field2 string `graphql:"field2"`
}
_, err := gqlstruct.NewEncoder().Field(&StructExample{}, &erroredOption{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("forced error"))
})
It("should fail with a not recognized type", func() {
type StructExample struct {
Field1 []interface{} `graphql:"field1"`
}
_, err := gqlstruct.NewEncoder().Field(&StructExample{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("not recognized"))
Expect(err.Error()).To(ContainSubstring("interface {}"))
})
It("should fail with a not recognized type with a pointer", func() {
type StructExample struct {
Field1 []*interface{} `graphql:"field1"`
}
_, err := gqlstruct.NewEncoder().Field(&StructExample{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("not recognized"))
Expect(err.Error()).To(ContainSubstring("interface {}"))
})
})