-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprefixscan_test.go
119 lines (99 loc) · 2.45 KB
/
prefixscan_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
package buckets_test
import (
"bytes"
"testing"
)
// Ensure we can scan prefixes.
func TestPrefixScanner(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
paths, err := bx.New([]byte("paths"))
// items to put in `paths` bucket
pathItems := []struct {
Key, Value []byte
}{
{[]byte("f/"), []byte("")},
{[]byte("fo/"), []byte("")},
{[]byte("foo/"), []byte("foo")},
{[]byte("foo/bar/"), []byte("bar")},
{[]byte("foo/bar/baz/"), []byte("baz")},
{[]byte("food/"), []byte("")},
{[]byte("good/"), []byte("")},
{[]byte("goo/"), []byte("")},
}
if err = paths.Insert(pathItems); err != nil {
t.Error(err.Error())
}
foo := paths.NewPrefixScanner([]byte("foo/"))
// expected items in `foo`
wantItems := []struct {
Key, Value []byte
}{
{[]byte("foo/"), []byte("foo")},
{[]byte("foo/bar/"), []byte("bar")},
{[]byte("foo/bar/baz/"), []byte("baz")},
}
// expected count of items in range
want := len(wantItems)
got, err := foo.Count()
if err != nil {
t.Error(err.Error())
}
if got != want {
t.Errorf("got %v, want %v", got, want)
}
// get keys for paths with `foo` prefix
keys, err := foo.Keys()
if err != nil {
t.Error(err.Error())
}
for i, want := range wantItems {
if got := keys[i]; !bytes.Equal(got, want.Key) {
t.Errorf("got %s, want %s", got, want.Key)
}
}
// get values for paths with `foo` prefix
values, err := foo.Values()
if err != nil {
t.Error(err.Error())
}
for i, want := range wantItems {
if got := values[i]; !bytes.Equal(got, want.Value) {
t.Errorf("got %s, want %s", got, want.Value)
}
}
// get k/v pairs for keys with `foo` prefix
items, err := foo.Items()
for i, want := range wantItems {
got := items[i]
if !bytes.Equal(got.Key, want.Key) {
t.Errorf("got %s, want %s", got.Key, want.Key)
}
if !bytes.Equal(got.Value, want.Value) {
t.Errorf("got %s, want %s", got.Value, want.Value)
}
}
// expected mapping
wantMapping := map[string][]byte{
"foo/": []byte("foo"),
"foo/bar/": []byte("bar"),
"foo/bar/baz/": []byte("baz"),
}
// get mapping of k/v pairs for keys with `foo` prefix
gotMapping, err := foo.ItemMapping()
if err != nil {
t.Error(err.Error())
}
for key, want := range wantMapping {
got, ok := gotMapping[key]
if ok == false {
t.Errorf("missing wanted key: %s", key)
}
if !bytes.Equal(got, want) {
t.Errorf("got %s, want %s", got, want)
}
}
if err = bx.Delete([]byte("paths")); err != nil {
t.Error(err.Error())
}
}