-
Notifications
You must be signed in to change notification settings - Fork 8
/
db_test.go
183 lines (148 loc) · 3.17 KB
/
db_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2014 Gyepi Sam. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package redux
import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"testing"
)
func initRoot() (root string, fn func(), err error) {
root, err = ioutil.TempDir("", "redo-db-root-")
fn = func() {
os.RemoveAll(root)
}
if err != nil {
return
}
err = InitDir(root)
return
}
func makeDBFunc(root string) func(func(DB) error) error {
return func(f func(DB) error) error {
return WithDB(root, f)
}
}
func randBytes(b []byte) (err error) {
_, err = io.ReadFull(rand.Reader, b)
return
}
func TestDBOpen(t *testing.T) {
root, fn, err := initRoot()
if err != nil {
t.Fatal(err)
}
defer fn()
f := func(db DB) error {
return nil
}
if err := WithDB(root, f); err != nil {
t.Fatal(err)
}
}
func TestDBNullKey(t *testing.T) {
root, fn, err := initRoot()
if err != nil {
t.Fatal(err)
}
defer fn()
err = WithDB(root, func(db DB) error {
return db.Put("", []byte{0})
})
if err != NullKeyErr {
t.Fatal(err)
}
}
func TestDBAction(t *testing.T) {
root, fn, err := initRoot()
if err != nil {
t.Fatal(err)
}
defer fn()
type KeyValue struct {
key string
value []byte
}
sizes := []int{0, 1, 2, 3, 5, 7, 10, 100, 1001, 10002, 100003}
data := make([]KeyValue, len(sizes))
var prefixKeys []string
var prefixValues [][]byte
prefix := "random/byte/0"
for i, size := range sizes {
data[i].key = fmt.Sprintf("random/byte/%02d", size)
data[i].value = make([]byte, size)
if err := randBytes(data[i].value); err != nil {
t.Fatal(err)
}
if size < 10 {
prefixKeys = append(prefixKeys, data[i].key)
prefixValues = append(prefixValues, data[i].value)
}
}
for _, rec := range data {
//Put
err := WithDB(root, func(db DB) error {
return db.Put(rec.key, rec.value)
})
if err != nil {
t.Fatalf("Error: %s in db.Put(%q,...)", err, rec.key)
}
// Get
var value []byte
err = WithDB(root, func(db DB) error {
var found bool
var err error
value, found, err = db.Get(rec.key)
if err == nil && !found {
err = fmt.Errorf("cannot find key: %s", rec.key)
}
return err
})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(rec.value, value) {
t.Fatalf("Get: input and output bytes differ: %v != %v", rec.value, value)
}
t.Logf("%s/%s\n", root, rec.key)
}
//GetKeys
var keys []string
err = WithDB(root, func(db DB) error {
var err error
keys, err = db.GetKeys(prefix)
return err
})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(prefixKeys, keys) {
t.Fatalf("GetKeys: input and output prefixed keys differ: %v != %v", prefixKeys, keys)
}
//GetValues
var values [][]byte
err = WithDB(root, func(db DB) error {
var err error
values, err = db.GetValues(prefix)
return err
})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(prefixValues, values) {
t.Fatalf("GetValues: input and output prefixed values differ: %v != %v", prefixValues, values)
}
//Delete
for _, rec := range data {
err = WithDB(root, func(db DB) error {
return db.Delete(rec.key)
})
if err != nil {
t.Fatal(err)
}
}
}