-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathforeach_test.go
147 lines (126 loc) · 3.53 KB
/
foreach_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
// Copyright 2019 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package bolthold_test
import (
"fmt"
"testing"
"github.com/timshannon/bolthold"
bolt "go.etcd.io/bbolt"
)
func TestForEach(t *testing.T) {
testWrap(t, func(store *bolthold.Store, t *testing.T) {
insertTestData(t, store)
for _, tst := range testResults {
t.Run(tst.name, func(t *testing.T) {
count := 0
err := store.ForEach(tst.query, func(record *ItemTest) error {
count++
found := false
for i := range tst.result {
if record.equal(&testData[tst.result[i]]) {
found = true
break
}
}
if !found {
if testing.Verbose() {
return fmt.Errorf("%v was not found in the result set! Full results: %v",
record, tst.result)
}
return fmt.Errorf("%v was not found in the result set!", record)
}
return nil
})
if count != len(tst.result) {
t.Fatalf("ForEach count is %d wanted %d.", count, len(tst.result))
}
if err != nil {
t.Fatalf("Error during ForEach iteration: %s", err)
}
})
}
})
}
func TestForEachInBucket(t *testing.T) {
testWrapWithBucket(t, func(store *bolthold.Store, bucket *bolt.Bucket, t *testing.T) {
insertBucketTestData(t, store, bucket)
for _, tst := range testResults {
t.Run(tst.name, func(t *testing.T) {
count := 0
err := store.ForEachInBucket(bucket, tst.query, func(record *ItemTest) error {
count++
found := false
for i := range tst.result {
if record.equal(&testData[tst.result[i]]) {
found = true
break
}
}
if !found {
if testing.Verbose() {
return fmt.Errorf("%v was not found in the result set! Full results: %v",
record, tst.result)
}
return fmt.Errorf("%v was not found in the result set!", record)
}
return nil
})
if count != len(tst.result) {
t.Fatalf("ForEach count is %d wanted %d.", count, len(tst.result))
}
if err != nil {
t.Fatalf("Error during ForEach iteration: %s", err)
}
})
}
})
}
func TestForEachKeyStructTag(t *testing.T) {
testWrap(t, func(store *bolthold.Store, t *testing.T) {
type KeyTest struct {
Key int `boltholdKey:"Key"`
Value string
}
key := 3
err := store.Insert(key, &KeyTest{
Value: "test value",
})
if err != nil {
t.Fatalf("Error inserting KeyTest struct for Key struct tag testing. Error: %s", err)
}
err = store.ForEach(bolthold.Where(bolthold.Key).Eq(key), func(result *KeyTest) error {
if result.Key != key {
t.Fatalf("Key struct tag was not set correctly. Expected %d, got %d", key, result.Key)
}
return nil
})
if err != nil {
t.Fatalf("Error running ForEach in TestKeyStructTag. ERROR: %s", err)
}
})
}
func TestForEachKeyStructTagIntoPtr(t *testing.T) {
testWrap(t, func(store *bolthold.Store, t *testing.T) {
type KeyTest struct {
Key *int `boltholdKey:"Key"`
Value string
}
key := 3
err := store.Insert(&key, &KeyTest{
Value: "test value",
})
if err != nil {
t.Fatalf("Error inserting KeyTest struct for Key struct tag testing. Error: %s", err)
}
err = store.ForEach(bolthold.Where(bolthold.Key).Eq(key), func(result *KeyTest) error {
if result.Key == nil || *result.Key != key {
t.Fatalf("Key struct tag was not set correctly. Expected %d, got %d", key, result.Key)
}
return nil
})
if err != nil {
t.Fatalf("Error running ForEach in TestKeyStructTag. ERROR: %s", err)
}
})
}