-
Notifications
You must be signed in to change notification settings - Fork 6
/
straf_test.go
99 lines (80 loc) · 2.06 KB
/
straf_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
package straf
import (
"reflect"
"testing"
"github.com/graphql-go/graphql"
"github.com/stretchr/testify/assert"
)
type extra struct {
Age int
FavNums []float32
}
type colors struct {
Name string
HexValue string
}
type user struct {
UserID int `description:"UserID"`
ExtraData extra
FavColors []colors
Age int `deprecationReason:"Age is now in ExtraData"`
ID string `unique:"true"`
}
//Tests generation of graphQL Object from struct
func TestGraphQLObjectGen(t *testing.T) {
assert := assert.New(t)
graphQLObject, err := GetGraphQLObject(user{})
assert.NoError(err, "GetGraphQLObject Returned Error")
extraType, err := GetGraphQLObject(extra{})
assert.NoError(err, "GetGraphQLObject Returned Error")
colorType, err := GetGraphQLObject(colors{})
assert.NoError(err, "GetGraphQLObject Returned Error")
testField(
"UserID",
*graphQLObject.Fields()["UserID"],
graphql.Int,
*assert,
)
testField(
"ID",
*graphQLObject.Fields()["ID"],
graphql.ID,
*assert,
)
testField(
"ExtraData",
*graphQLObject.Fields()["ExtraData"],
extraType,
*assert,
)
testField(
"FavColors",
*graphQLObject.Fields()["FavColors"],
graphql.NewList(colorType),
*assert,
)
assert.Equal(graphQLObject.Fields()["Age"].DeprecationReason, "Age is now in ExtraData")
assert.Equal(graphQLObject.Fields()["UserID"].Description, "UserID")
}
func testField(name string,
definition graphql.FieldDefinition,
graphqlType graphql.Output,
assert assert.Assertions) {
assert.Equal(definition.Name, name)
assert.Equal(definition.Type, graphqlType)
}
func TestConvertSimpleType(t *testing.T) {
assert := assert.New(t)
userType := reflect.TypeOf(user{})
scalar, err := convertSimpleType(userType)
assert.Error(err)
assert.Equal("Invalid Type", err.Error())
assert.Equal(&graphql.Scalar{}, scalar)
}
func TestGetTagValue(t *testing.T) {
assert := assert.New(t)
userType := reflect.TypeOf(user{})
field, _ := userType.FieldByName("UserID")
assert.Equal("", getTagValue(field, "random"))
assert.Equal("UserID", getTagValue(field, "description"))
}