-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
add_test.go
332 lines (283 loc) · 7.72 KB
/
add_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package ds_test
import (
"os"
"path"
"testing"
"github.com/ecnepsnai/ds"
"go.etcd.io/bbolt"
)
// Test that you can add a row to the table
func TestAdd(t *testing.T) {
t.Parallel()
type exampleType struct {
Primary string `ds:"primary"`
Index string `ds:"index"`
Unique string `ds:"unique"`
}
table, err := ds.Register(exampleType{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: randomString(12),
Index: randomString(12),
Unique: randomString(12),
})
})
if err != nil {
t.Errorf("Error adding value to table: %s", err.Error())
}
}
// Test that you can add a row with an indexed field to the table
func TestAddIndex(t *testing.T) {
t.Parallel()
type exampleType struct {
Primary string `ds:"primary"`
Index string `ds:"index"`
Unique string `ds:"unique"`
}
table, err := ds.Register(exampleType{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
index := randomString(12)
i := 0
for i < 10 {
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: randomString(12),
Index: index,
Unique: randomString(12),
})
})
if err != nil {
t.Errorf("Error adding value to table: %s", err.Error())
}
i++
}
}
// Test that you can't add an object of a different type to a table
func TestAddTypeMismatch(t *testing.T) {
t.Parallel()
type exampleType struct {
Primary string `ds:"primary"`
Index string `ds:"index"`
Unique string `ds:"unique"`
}
table, err := ds.Register(exampleType{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
type otherType struct {
Foo string `ds:"primary"`
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(otherType{
Foo: randomString(12),
})
})
if err == nil {
t.Error("No error seen while attempting to insert incorrect object into table")
}
}
// Test that you can't add a pointer to a table
func TestAddPointer(t *testing.T) {
t.Parallel()
type exampleType struct {
Primary string `ds:"primary"`
Index string `ds:"index"`
Unique string `ds:"unique"`
}
table, err := ds.Register(exampleType{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
type otherType struct {
Foo string `ds:"primary"`
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(&otherType{
Foo: randomString(12),
})
})
if err == nil {
t.Error("No error seen while attempting to insert pointer into table")
}
}
// Test that you can't add an object with a duplicate primary key value
func TestAddDuplicatePrimaryKey(t *testing.T) {
t.Parallel()
type exampleType struct {
Primary string `ds:"primary"`
Index string `ds:"index"`
Unique string `ds:"unique"`
}
primaryKey := randomString(12)
table, err := ds.Register(exampleType{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: primaryKey,
Index: randomString(12),
Unique: randomString(12),
})
})
if err != nil {
t.Errorf("Error adding value to table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: primaryKey,
Index: randomString(12),
Unique: randomString(12),
})
})
if err == nil {
t.Errorf("No error seen while attempting to insert object with duplicate primary key")
}
}
// Test that you can't add an object with a duplicate unique fields value
func TestAddDuplicateUnique(t *testing.T) {
t.Parallel()
type exampleType struct {
Primary string `ds:"primary"`
Index string `ds:"index"`
Unique string `ds:"unique"`
}
unique := randomString(12)
table, err := ds.Register(exampleType{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: randomString(12),
Index: randomString(12),
Unique: unique,
})
})
if err != nil {
t.Errorf("Error adding value to table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: randomString(12),
Index: randomString(12),
Unique: unique,
})
})
if err == nil {
t.Errorf("No error seen while attempting to insert object with duplicate primary key")
}
}
// Test that you can add an object into a table that has an unmatched unique field
func TestAddUnmatchedUnique(t *testing.T) {
t.Parallel()
type exampleType struct {
Primary string `ds:"primary"`
Unique string `ds:"unique"`
}
unique := randomString(12)
tablePath := path.Join(t.TempDir(), randomString(12))
table, err := ds.Register(exampleType{}, tablePath, nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: randomString(12),
Unique: unique,
})
})
if err != nil {
t.Errorf("Error adding value to table: %s", err.Error())
}
table.Close()
db, err := bbolt.Open(tablePath, os.ModePerm, nil)
if err != nil {
t.Fatalf("Error opening bolt table: %s", err.Error())
}
db.Update(func(tx *bbolt.Tx) error {
tx.DeleteBucket([]byte("data"))
tx.CreateBucketIfNotExists([]byte("data"))
return nil
})
db.Close()
table, err = ds.Register(exampleType{}, tablePath, nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(exampleType{
Primary: randomString(12),
Unique: unique,
})
})
if err != nil {
t.Errorf("Error adding value to table: %s", err.Error())
}
}
func TestAddDifferentStruct(t *testing.T) {
t.Parallel()
table, err := ds.Register(struct {
Primary string `ds:"primary"`
}{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
// Change name of primary
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(struct {
Foo int `ds:"primary"`
}{Foo: 1})
})
if err == nil {
t.Errorf("No error seen when adding different struct to table")
}
// Omit primary tag
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(struct {
Primary string
}{Primary: randomString(12)})
})
if err == nil {
t.Errorf("No error seen when adding different struct to table")
}
// Change type of primary
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(struct {
Primary int `ds:"primary"`
}{Primary: 1})
})
if err == nil {
t.Errorf("No error seen when adding different struct to table")
}
}
func TestAddBadStruct(t *testing.T) {
t.Parallel()
table, err := ds.Register(struct {
Primary string `ds:"primary"`
Index string `ds:"index"`
Unique string `ds:"unique"`
}{}, path.Join(t.TempDir(), randomString(12)), nil)
if err != nil {
t.Errorf("Error registering table: %s", err.Error())
}
err = table.StartWrite(func(tx ds.IReadWriteTransaction) error {
return tx.Add(struct {
Primary string
Index string
Unique string
}{
Primary: randomString(12),
Index: randomString(12),
Unique: randomString(12),
})
})
if err == nil {
t.Errorf("No error seen when adding bad struct to table")
}
}