-
Notifications
You must be signed in to change notification settings - Fork 139
/
extract_test.go
136 lines (118 loc) · 3.67 KB
/
extract_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
package storm
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func allByType(m *structConfig, indexType string) []*fieldConfig {
var idx []*fieldConfig
for k := range m.Fields {
if m.Fields[k].Index == indexType {
idx = append(idx, m.Fields[k])
}
}
return idx
}
func TestExtractNoTags(t *testing.T) {
s := ClassicNoTags{}
r := reflect.ValueOf(&s)
_, err := extract(&r)
require.Error(t, err)
require.Equal(t, ErrNoID, err)
}
func TestExtractBadTags(t *testing.T) {
s := ClassicBadTags{}
r := reflect.ValueOf(&s)
infos, err := extract(&r)
require.Error(t, err)
require.Equal(t, ErrUnknownTag, err)
require.Nil(t, infos)
}
func TestExtractUniqueTags(t *testing.T) {
s := ClassicUnique{ID: "id"}
r := reflect.ValueOf(&s)
infos, err := extract(&r)
require.NoError(t, err)
require.NotNil(t, infos)
require.NotNil(t, infos.ID)
require.False(t, infos.ID.IsZero)
require.Equal(t, "ClassicUnique", infos.Name)
require.Len(t, allByType(infos, "index"), 0)
require.Len(t, allByType(infos, "unique"), 5)
}
func TestExtractIndexTags(t *testing.T) {
s := ClassicIndex{ID: "id"}
r := reflect.ValueOf(&s)
infos, err := extract(&r)
require.NoError(t, err)
require.NotNil(t, infos)
require.NotNil(t, infos.ID)
require.False(t, infos.ID.IsZero)
require.Equal(t, "ClassicIndex", infos.Name)
require.Len(t, allByType(infos, "index"), 5)
require.Len(t, allByType(infos, "unique"), 1)
}
func TestExtractInlineWithIndex(t *testing.T) {
s := ClassicInline{ToEmbed: &ToEmbed{ID: "50"}}
r := reflect.ValueOf(&s)
infos, err := extract(&r)
require.NoError(t, err)
require.NotNil(t, infos)
require.NotNil(t, infos.ID)
require.Equal(t, "ClassicInline", infos.Name)
require.Len(t, allByType(infos, "index"), 3)
require.Len(t, allByType(infos, "unique"), 3)
}
func TestExtractMultipleTags(t *testing.T) {
type User struct {
ID uint64 `storm:"id,increment"`
Age uint16 `storm:"index,increment"`
unexportedField int32 `storm:"index,increment"`
X uint32 `storm:"unique,increment=100"`
Y int8 `storm:"index,increment=-100"`
}
s := User{}
r := reflect.ValueOf(&s)
infos, err := extract(&r)
require.NoError(t, err)
require.NotNil(t, infos)
require.NotNil(t, infos.ID)
require.Equal(t, "User", infos.Name)
require.Len(t, allByType(infos, "index"), 2)
require.Len(t, allByType(infos, "unique"), 2)
require.True(t, infos.Fields["Age"].Increment)
require.Equal(t, int64(1), infos.Fields["Age"].IncrementStart)
require.Equal(t, "index", infos.Fields["Age"].Index)
require.False(t, infos.Fields["Age"].IsID)
require.True(t, infos.Fields["Age"].IsInteger)
require.True(t, infos.Fields["Age"].IsZero)
require.NotNil(t, infos.Fields["Age"].Value)
require.True(t, infos.Fields["X"].Increment)
require.Equal(t, int64(100), infos.Fields["X"].IncrementStart)
require.Equal(t, "unique", infos.Fields["X"].Index)
require.False(t, infos.Fields["X"].IsID)
require.True(t, infos.Fields["X"].IsInteger)
require.True(t, infos.Fields["X"].IsZero)
require.NotNil(t, infos.Fields["X"].Value)
require.True(t, infos.Fields["Y"].Increment)
require.Equal(t, int64(-100), infos.Fields["Y"].IncrementStart)
require.Equal(t, "index", infos.Fields["Y"].Index)
require.False(t, infos.Fields["Y"].IsID)
require.True(t, infos.Fields["Y"].IsInteger)
require.True(t, infos.Fields["Y"].IsZero)
require.NotNil(t, infos.Fields["Y"].Value)
type NoInt struct {
ID uint64 `storm:"id,increment=hello"`
}
var n NoInt
r = reflect.ValueOf(&n)
_, err = extract(&r)
require.Error(t, err)
type BadSuffix struct {
ID uint64 `storm:"id,incrementag=100"`
}
var b BadSuffix
r = reflect.ValueOf(&b)
_, err = extract(&r)
require.Error(t, err)
}