-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathindex_test.go
124 lines (102 loc) · 2.95 KB
/
index_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
// Copyright 2016 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 (
"testing"
bh "github.com/timshannon/bolthold"
bolt "go.etcd.io/bbolt"
)
func TestIndexSlice(t *testing.T) {
testWrap(t, func(store *bh.Store, t *testing.T) {
var testData = []ItemTest{
{
Key: 0,
Name: "John",
Tags: []string{"red", "green", "blue"},
},
{
Key: 1,
Name: "Bill",
Tags: []string{"red", "purple"},
},
{
Key: 2,
Name: "Jane",
Tags: []string{"red", "orange"},
},
{
Key: 3,
Name: "Brian",
Tags: []string{"red", "purple"},
},
}
for _, data := range testData {
ok(t, store.Insert(data.Key, data))
}
b := store.Bolt()
ok(t, b.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("_index:ItemTest:Tags"))
assert(t, bucket != nil, "No index bucket found for Tags index")
indexCount := 0
bucket.ForEach(func(k, v []byte) error {
indexCount++
return nil
})
// each tag chould be indexed individually and there are 5 different tags
equals(t, indexCount, 5)
return nil
}))
})
}
func Test85SliceIndex(t *testing.T) {
type Event struct {
Id uint64
Type string `boltholdIndex:"Type"`
Categories []string `boltholdSliceIndex:"Categories"`
}
testWrap(t, func(store *bh.Store, t *testing.T) {
e1 := &Event{Id: 1, Type: "Type1", Categories: []string{"Cat 1", "Cat 2"}}
e2 := &Event{Id: 2, Type: "Type1", Categories: []string{"Cat 3"}}
ok(t, store.Insert(e1.Id, e1))
ok(t, store.Insert(e2.Id, e2))
var es []*Event
ok(t, store.Find(&es, bh.Where("Categories").Contains("Cat 1").Index("Categories")))
equals(t, len(es), 1)
})
}
func Test87SliceIndex(t *testing.T) {
type Event struct {
Id uint64
Type string `boltholdIndex:"Type"`
Categories []string `boltholdSliceIndex:"Categories"`
}
testWrap(t, func(store *bh.Store, t *testing.T) {
e1 := &Event{Id: 1, Type: "Type1", Categories: []string{"Cat 1", "Cat 2"}}
e2 := &Event{Id: 2, Type: "Type1", Categories: []string{"Cat 3"}}
ok(t, store.Insert(e1.Id, e1))
ok(t, store.Insert(e2.Id, e2))
var es []*Event
ok(t, store.Find(&es, bh.Where("Categories").ContainsAny("Cat 1").Index("Categories")))
equals(t, len(es), 1)
})
}
func TestSliceIndexWithPointers(t *testing.T) {
type Event struct {
Id uint64
Type string `boltholdIndex:"Type"`
Categories []*string `boltholdSliceIndex:"Categories"`
}
testWrap(t, func(store *bh.Store, t *testing.T) {
cat1 := "Cat 1"
cat2 := "Cat 2"
cat3 := "Cat 3"
e1 := &Event{Id: 1, Type: "Type1", Categories: []*string{&cat1, &cat2}}
e2 := &Event{Id: 2, Type: "Type1", Categories: []*string{&cat3}}
ok(t, store.Insert(e1.Id, e1))
ok(t, store.Insert(e2.Id, e2))
var es []*Event
ok(t, store.Find(&es, bh.Where("Categories").ContainsAll("Cat 1").Index("Categories")))
equals(t, len(es), 1)
})
}