-
Notifications
You must be signed in to change notification settings - Fork 51
/
foreach_test.go
83 lines (69 loc) · 1.89 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
// 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 badgerhold_test
import (
"fmt"
"testing"
"time"
"github.com/timshannon/badgerhold/v4"
)
func TestForEach(t *testing.T) {
testWrap(t, func(store *badgerhold.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 TestIssue105ForEachKeys(t *testing.T) {
type Person struct {
ID uint64 `badgerhold:"key"`
Name string
Gender string
Birth time.Time
}
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
data := &Person{Name: "tester1"}
ok(t, store.Insert(badgerhold.NextSequence(), data))
equals(t, uint64(0), data.ID)
data = &Person{Name: "tester2"}
ok(t, store.Insert(badgerhold.NextSequence(), data))
equals(t, uint64(1), data.ID)
data = &Person{Name: "tester3"}
ok(t, store.Insert(badgerhold.NextSequence(), data))
equals(t, uint64(2), data.ID)
var id uint64 = 0
ok(t, store.ForEach(nil, func(record *Person) error {
assert(t, id == record.ID, record.Name+" incorrectly set key")
id++
return nil
}))
})
}