forked from iFaceless/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_test.go
108 lines (87 loc) · 3.65 KB
/
schema_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
package portal
import (
"context"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type SchoolSchema struct {
Name string
Addr string
}
type PersonSchema struct {
ID string `json:"id"`
Age int `json:"age"`
}
type UserSchema2 struct {
PersonSchema
Name string `portal:"meth:GetName"`
School *SchoolSchema `portal:"nested"`
Async int `json:"async" portal:"async"`
}
func (u *UserSchema2) GetName(user interface{}) interface{} {
return "test"
}
func TestSchema(t *testing.T) {
schema := newSchema(&UserSchema2{})
assert.Equal(t, "UserSchema2", schema.name())
assert.NotNil(t, schema.innerStruct())
user := struct {
ID int
School *SchoolSchema
}{10, &SchoolSchema{Name: "test school"}}
idField := newField(schema, schema.schemaStruct.Field("ID"))
val, err := schema.fieldValueFromSrc(context.TODO(), idField, user, false)
assert.Nil(t, err)
assert.Equal(t, 10, val)
nameField := newField(schema, schema.schemaStruct.Field("Name"))
val, err = schema.fieldValueFromSrc(context.TODO(), nameField, user, false)
assert.Nil(t, err)
assert.Equal(t, "test", val)
schoolField := newField(schema, schema.schemaStruct.Field("School"))
val, err = schema.fieldValueFromSrc(context.TODO(), schoolField, user, false)
assert.Nil(t, err)
assert.Equal(t, &SchoolSchema{Name: "test school", Addr: ""}, val)
}
func Test_hasAsyncFields(t *testing.T) {
assert.False(t, hasAsyncFields(reflect.TypeOf(&PersonSchema{}), nil, nil))
assert.False(t, hasAsyncFields(reflect.TypeOf(PersonSchema{}), nil, nil))
assert.True(t, hasAsyncFields(reflect.TypeOf(UserSchema2{}), nil, nil))
assert.True(t, hasAsyncFields(reflect.TypeOf(&UserSchema2{}), nil, nil))
assert.True(t, hasAsyncFields(reflect.TypeOf(&UserSchema2{}), []string{"async"}, nil))
assert.True(t, hasAsyncFields(reflect.TypeOf(&UserSchema2{}), []string{"Async"}, nil))
assert.False(t, hasAsyncFields(reflect.TypeOf(&UserSchema2{}), []string{"Name"}, nil))
assert.False(t, hasAsyncFields(reflect.TypeOf(&UserSchema2{}), []string{}, []string{"Async"}))
assert.False(t, hasAsyncFields(reflect.TypeOf(&UserSchema2{}), []string{}, []string{"async"}))
}
func TestSchema_GetFields(t *testing.T) {
schema := newSchema(&UserSchema2{}).withFieldAliasMapTagName("json")
assert.ElementsMatch(t, []string{"Age", "ID", "Name", "School", "Async"}, filedNames(schema.availableFields()))
assert.ElementsMatch(t, []string{"Age", "ID", "Name", "School"}, filedNames(schema.syncFields(false)))
assert.ElementsMatch(t, []string{"Async"}, filedNames(schema.asyncFields(false)))
schema.setOnlyFields("ID")
assert.ElementsMatch(t, []string{"ID"}, filedNames(schema.availableFields()))
schema.setOnlyFields("id")
assert.ElementsMatch(t, []string{"ID"}, filedNames(schema.availableFields()))
schema.setOnlyFields("ID", "NotFound")
assert.ElementsMatch(t, []string{"ID"}, filedNames(schema.availableFields()))
schema = newSchema(&UserSchema2{})
schema.setExcludeFields("ID", "Name", "School")
assert.ElementsMatch(t, []string{"Age", "Async"}, filedNames(schema.availableFields()))
schema.setExcludeFields("ID", "Name", "School", "NotFound")
assert.ElementsMatch(t, []string{"Age", "Async"}, filedNames(schema.availableFields()))
}
func filedNames(fields []*schemaField) (names []string) {
for _, f := range fields {
names = append(names, f.Name())
}
return
}
func TestSchema_NameWithParent(t *testing.T) {
s1 := newSchema(&SchoolSchema{})
s2 := newSchema(&PersonSchema{}, s1)
s3 := newSchema(&UserSchema2{}, s2)
assert.Equal(t, s1.nameWithParents(), "SchoolSchema")
assert.Equal(t, s2.nameWithParents(), "SchoolSchema.PersonSchema")
assert.Equal(t, s3.nameWithParents(), "SchoolSchema.PersonSchema.UserSchema2")
}