-
Notifications
You must be signed in to change notification settings - Fork 7
/
rangescan_test.go
129 lines (109 loc) · 2.66 KB
/
rangescan_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
package buckets_test
import (
"bytes"
"testing"
)
// Ensures we can scan ranges.
func TestRangeScanner(t *testing.T) {
bx := NewTestDB()
defer bx.Close()
years, err := bx.New([]byte("years"))
if err != nil {
t.Error(err.Error())
}
// items to put in `years` bucket
yearItems := []struct {
Key, Value []byte
}{
{[]byte("1970"), []byte("70")},
{[]byte("1975"), []byte("75")},
{[]byte("1980"), []byte("80")},
{[]byte("1985"), []byte("85")},
{[]byte("1990"), []byte("90")}, // min = 1990
{[]byte("1995"), []byte("95")}, // min < 1995 < max
{[]byte("2000"), []byte("00")}, // max = 2000
{[]byte("2005"), []byte("05")},
{[]byte("2010"), []byte("10")},
}
// insert items into `years` bucket
if err = years.Insert(yearItems); err != nil {
t.Error(err.Error())
}
// time range to scan over
min := []byte("1990")
max := []byte("2000")
nineties := years.NewRangeScanner(min, max)
// expected count of items in range
wantCount := 3
// expected items
wantItems := []struct {
Key []byte
Value []byte
}{
{[]byte("1990"), []byte("90")},
{[]byte("1995"), []byte("95")},
{[]byte("2000"), []byte("00")},
}
count, err := nineties.Count()
if err != nil {
t.Error(err.Error())
}
if count != wantCount {
t.Errorf("got %v, want %v", count, wantCount)
}
keys, err := nineties.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)
}
}
values, err := nineties.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 within range (1995 <= year <= 2000)
items, err := nineties.Items()
if err != nil {
t.Error(err.Error())
}
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{
"1990": []byte("90"),
"1995": []byte("95"),
"2000": []byte("00"),
}
// get mapping of k/v pairs for keys within range (1995 <= year <= 2000)
gotMapping, err := nineties.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("years")); err != nil {
t.Error(err.Error())
}
}