-
Notifications
You must be signed in to change notification settings - Fork 25
/
struct_tags_test.go
243 lines (213 loc) · 7.6 KB
/
struct_tags_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
// File struct_tags_test.go tests the different options
// that may be provided in model type declarations using
// struct tags.
package zoom
import (
"testing"
"github.com/garyburd/redigo/redis"
)
// Test that the redis ignore struct tag causes a field to be ignored.
func TestRedisIgnoreOption(t *testing.T) {
testingSetUp()
defer testingTearDown()
type ignoredFieldModel struct {
Attr string `redis:"-"`
RandomID
}
ignoredFieldModels, err := testPool.NewCollection(&ignoredFieldModel{})
if err != nil {
t.Errorf("Unexpected error in Register: %s", err)
}
// check the spec
spec, found := testPool.modelNameToSpec["ignoredFieldModel"]
if !found {
t.Error("Could not find spec for model name ignoredFieldModel")
}
if fs, found := spec.fieldsByName["Attr"]; found {
t.Errorf("Expected to not find the Attr field in the spec, but found: %v", fs)
}
// save a new model
model := &ignoredFieldModel{
Attr: "this should be ignored",
}
if err := ignoredFieldModels.Save(model); err != nil {
t.Errorf("Unexpected error in Save: %s", err.Error())
}
// Check the database to make sure the field is not there
conn := testPool.NewConn()
defer func() {
_ = conn.Close()
}()
key := ignoredFieldModels.ModelKey(model.ModelID())
gotAttr, err := redis.String(conn.Do("HGET", key, "Attr"))
if err != nil && err != redis.ErrNil {
t.Errorf("Unexpected error in HGET command: %s", err.Error())
}
if gotAttr != "" {
t.Errorf("Expected empty attr but got: %s", gotAttr)
}
}
// Test that the redis name struct tag causes a field's name in redis to be changed
func TestRedisNameOption(t *testing.T) {
testingSetUp()
defer testingTearDown()
type customFieldModel struct {
Attr string `redis:"a"`
RandomID
}
customFieldModels, err := testPool.NewCollection(&customFieldModel{})
if err != nil {
t.Errorf("Unexpected error in Register: %s", err.Error())
}
// check the spec
spec, found := testPool.modelNameToSpec["customFieldModel"]
if !found {
t.Error("Could not find spec for model name customFieldModel")
}
if fs, found := spec.fieldsByName["Attr"]; !found {
t.Error("Expected to find Attr field in the spec, but got nil")
} else if fs.redisName != "a" {
t.Errorf("Expected fs.redisName to be `a` but got %s", fs.redisName)
}
// save a new model and check redis
model := &customFieldModel{
Attr: "test",
}
if err := customFieldModels.Save(model); err != nil {
t.Errorf("Unexpected error in Save: %s", err.Error())
}
modelKey := customFieldModels.ModelKey(model.ModelID())
expectFieldEquals(t, modelKey, "a", customFieldModels.spec.fallback, "test")
}
func TestInvalidOptionThrowsError(t *testing.T) {
testingSetUp()
testingTearDown()
type invalid struct {
Attr string `zoom:"index,poop"`
RandomID
}
if _, err := testPool.NewCollection(&invalid{}); err == nil {
t.Error("Expected error when registering struct with invalid tag")
}
}
// Test that the indexes are actually created in redis for a model with all
// the different indexed primitive fields
func TestSaveIndexedPrimativesModel(t *testing.T) {
testingSetUp()
defer testingTearDown()
// Create and save a new model with random primitive fields
model := createIndexedPrimativesModel()
if err := indexedPrimativesModels.Save(model); err != nil {
t.Fatalf("Unexpected error in Save: %s", err.Error())
}
// Iterate through each field using reflection and validate that the index was set properly
numFields := indexedPrimativesModels.spec.typ.Elem().NumField()
for i := 0; i < numFields; i++ {
field := indexedPrimativesModels.spec.typ.Elem().Field(i)
if field.Anonymous {
continue // Skip embedded structs
}
expectIndexExists(t, indexedPrimativesModels, model, field.Name)
}
}
// Test that the indexes are actually created in redis for a model with all
// the different indexed pointer to primitive fields
func TestSaveIndexedPointersModel(t *testing.T) {
testingSetUp()
defer testingTearDown()
// Create and save a new model with random pointer to primitive fields
model := createIndexedPointersModel()
if err := indexedPointersModels.Save(model); err != nil {
t.Fatalf("Unexpected error in Save: %s", err.Error())
}
// Iterate through each field using reflection and validate that the index was set properly
numFields := indexedPointersModels.spec.typ.Elem().NumField()
for i := 0; i < numFields; i++ {
field := indexedPointersModels.spec.typ.Elem().Field(i)
if field.Anonymous {
continue // Skip embedded structs
}
expectIndexExists(t, indexedPointersModels, model, field.Name)
}
}
// Test that the indexes are removed from redis after a model with primitive indexes is deleted
func TestDeleteIndexedPrimativesModel(t *testing.T) {
testingSetUp()
defer testingTearDown()
// Create and save a new model with random primitive fields
model := createIndexedPrimativesModel()
if err := indexedPrimativesModels.Save(model); err != nil {
t.Fatalf("Unexpected error in Save: %s", err.Error())
}
if _, err := indexedPrimativesModels.Delete(model.ModelID()); err != nil {
t.Fatalf("Unexpected error in Delete: %s", err.Error())
}
// Iterate through each field using reflection and validate that the index was set properly
numFields := indexedPrimativesModels.spec.typ.Elem().NumField()
for i := 0; i < numFields; i++ {
field := indexedPrimativesModels.spec.typ.Elem().Field(i)
if field.Anonymous {
continue // Skip embedded structs
}
expectIndexDoesNotExist(t, indexedPrimativesModels, model, field.Name)
}
}
// Test that the indexes are removed from redis after a model with indexed pointer to primitive
// fields is deleted.
func TestDeleteIndexedPointersModel(t *testing.T) {
testingSetUp()
defer testingTearDown()
// Create and save a new model with random pointer to primitive fields.
model := createIndexedPointersModel()
if err := indexedPointersModels.Save(model); err != nil {
t.Fatalf("Unexpected error in Save: %s", err.Error())
}
if _, err := indexedPointersModels.Delete(model.ModelID()); err != nil {
t.Fatalf("Unexpected error in Delete: %s", err.Error())
}
// Iterate through each field using reflection and validate that the index was set properly.
numFields := indexedPointersModels.spec.typ.Elem().NumField()
for i := 0; i < numFields; i++ {
field := indexedPointersModels.spec.typ.Elem().Field(i)
if field.Anonymous {
continue // Skip embedded structs
}
expectIndexDoesNotExist(t, indexedPointersModels, model, field.Name)
}
}
// Test that the indexes are actually created in redis for a model with all
// the different indexed primitive fields.
func TestIndexAndCustomName(t *testing.T) {
testingSetUp()
defer testingTearDown()
type customIndexModel struct {
Int int `zoom:"index" redis:"integer"`
String string `zoom:"index" redis:"str"`
Bool bool `zoom:"index" redis:"boolean"`
RandomID
}
customIndexModels, err := testPool.NewCollection(&customIndexModel{})
if err != nil {
t.Fatalf("Unexpected error in Register: %s", err.Error())
}
model := &customIndexModel{
Int: randomInt(),
String: randomString(),
Bool: randomBool(),
}
if err := customIndexModels.Save(model); err != nil {
t.Fatalf("Unexpected error in Save: %s", err.Error())
}
// Iterate through each field using reflection and validate that the index was set properly.
numFields := customIndexModels.spec.typ.Elem().NumField()
for i := 0; i < numFields; i++ {
field := customIndexModels.spec.typ.Elem().Field(i)
if field.Anonymous {
continue // Skip embedded structs
}
expectIndexExists(t, customIndexModels, model, field.Name)
}
}