-
Notifications
You must be signed in to change notification settings - Fork 21
/
ds_test.go
375 lines (315 loc) · 7.05 KB
/
ds_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package leveldb
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"testing"
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
dstest "github.com/ipfs/go-datastore/test"
)
var testcases = map[string]string{
"/a": "a",
"/a/b": "ab",
"/a/b/c": "abc",
"/a/b/d": "a/b/d",
"/a/c": "ac",
"/a/d": "ad",
"/e": "e",
"/f": "f",
}
// returns datastore, and a function to call on exit.
// (this garbage collects). So:
//
// d, close := newDS(t)
// defer close()
func newDS(t *testing.T) (*Datastore, func()) {
path, err := ioutil.TempDir("/tmp", "testing_leveldb_")
if err != nil {
t.Fatal(err)
}
d, err := NewDatastore(path, nil)
if err != nil {
t.Fatal(err)
}
return d, func() {
os.RemoveAll(path)
d.Close()
}
}
// newDSMem returns an in-memory datastore.
func newDSMem(t *testing.T) *Datastore {
d, err := NewDatastore("", nil)
if err != nil {
t.Fatal(err)
}
return d
}
func addTestCases(t *testing.T, d *Datastore, testcases map[string]string) {
for k, v := range testcases {
dsk := ds.NewKey(k)
if err := d.Put(dsk, []byte(v)); err != nil {
t.Fatal(err)
}
}
for k, v := range testcases {
dsk := ds.NewKey(k)
v2, err := d.Get(dsk)
if err != nil {
t.Fatal(err)
}
if string(v2) != v {
t.Errorf("%s values differ: %s != %s", k, v, v2)
}
}
}
func testQuery(t *testing.T, d *Datastore) {
addTestCases(t, d, testcases)
rs, err := d.Query(dsq.Query{Prefix: "/a/"})
if err != nil {
t.Fatal(err)
}
expectMatches(t, []string{
"/a/b",
"/a/b/c",
"/a/b/d",
"/a/c",
"/a/d",
}, rs)
// test offset and limit
rs, err = d.Query(dsq.Query{Prefix: "/a/", Offset: 2, Limit: 2})
if err != nil {
t.Fatal(err)
}
expectMatches(t, []string{
"/a/b/d",
"/a/c",
}, rs)
// test order
rs, err = d.Query(dsq.Query{Orders: []dsq.Order{dsq.OrderByKey{}}})
if err != nil {
t.Fatal(err)
}
keys := make([]string, 0, len(testcases))
for k := range testcases {
keys = append(keys, k)
}
sort.Strings(keys)
expectOrderedMatches(t, keys, rs)
rs, err = d.Query(dsq.Query{Orders: []dsq.Order{dsq.OrderByKeyDescending{}}})
if err != nil {
t.Fatal(err)
}
// reverse
for i, j := 0, len(keys)-1; i < j; i, j = i+1, j-1 {
keys[i], keys[j] = keys[j], keys[i]
}
expectOrderedMatches(t, keys, rs)
}
func TestQuery(t *testing.T) {
d, close := newDS(t)
defer close()
testQuery(t, d)
}
func TestQueryMem(t *testing.T) {
d := newDSMem(t)
testQuery(t, d)
}
func TestQueryRespectsProcess(t *testing.T) {
d, close := newDS(t)
defer close()
addTestCases(t, d, testcases)
}
func TestQueryRespectsProcessMem(t *testing.T) {
d := newDSMem(t)
addTestCases(t, d, testcases)
}
func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
t.Helper()
actual, err := actualR.Rest()
if err != nil {
t.Error(err)
}
if len(actual) != len(expect) {
t.Error("not enough", expect, actual)
}
for _, k := range expect {
found := false
for _, e := range actual {
if e.Key == k {
found = true
}
}
if !found {
t.Error(k, "not found")
}
}
}
func expectOrderedMatches(t *testing.T, expect []string, actualR dsq.Results) {
t.Helper()
actual, err := actualR.Rest()
if err != nil {
t.Error(err)
}
if len(actual) != len(expect) {
t.Error("not enough", expect, actual)
}
for i := range expect {
if expect[i] != actual[i].Key {
t.Errorf("expected %q, got %q", expect[i], actual[i].Key)
}
}
}
func testBatching(t *testing.T, d *Datastore) {
b, err := d.Batch()
if err != nil {
t.Fatal(err)
}
for k, v := range testcases {
err := b.Put(ds.NewKey(k), []byte(v))
if err != nil {
t.Fatal(err)
}
}
err = b.Commit()
if err != nil {
t.Fatal(err)
}
for k, v := range testcases {
val, err := d.Get(ds.NewKey(k))
if err != nil {
t.Fatal(err)
}
if v != string(val) {
t.Fatal("got wrong data!")
}
}
}
func TestBatching(t *testing.T) {
d, done := newDS(t)
defer done()
testBatching(t, d)
}
func TestBatchingMem(t *testing.T) {
d := newDSMem(t)
testBatching(t, d)
}
func TestDiskUsage(t *testing.T) {
d, done := newDS(t)
addTestCases(t, d, testcases)
du, err := d.DiskUsage()
if err != nil {
t.Fatal(err)
}
if du == 0 {
t.Fatal("expected some disk usage")
}
k := ds.NewKey("more")
err = d.Put(k, []byte("value"))
if err != nil {
t.Fatal(err)
}
du2, err := d.DiskUsage()
if du2 <= du {
t.Fatal("size should have increased")
}
done()
// This should fail
_, err = d.DiskUsage()
if err == nil {
t.Fatal("DiskUsage should fail when we cannot walk path")
}
}
func TestDiskUsageInMem(t *testing.T) {
d := newDSMem(t)
du, _ := d.DiskUsage()
if du != 0 {
t.Fatal("inmem dbs have 0 disk usage")
}
}
func TestTransactionCommit(t *testing.T) {
key := ds.NewKey("/test/key1")
d, done := newDS(t)
defer done()
txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}
defer txn.Discard()
if err := txn.Put(key, []byte("hello")); err != nil {
t.Fatal(err)
}
if val, err := d.Get(key); err != ds.ErrNotFound {
t.Fatalf("expected ErrNotFound, got err: %v, value: %v", err, val)
}
if err := txn.Commit(); err != nil {
t.Fatal(err)
}
if val, err := d.Get(key); err != nil || !bytes.Equal(val, []byte("hello")) {
t.Fatalf("expected entry present after commit, got err: %v, value: %v", err, val)
}
}
func TestTransactionDiscard(t *testing.T) {
key := ds.NewKey("/test/key1")
d, done := newDS(t)
defer done()
txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}
defer txn.Discard()
if err := txn.Put(key, []byte("hello")); err != nil {
t.Fatal(err)
}
if val, err := d.Get(key); err != ds.ErrNotFound {
t.Fatalf("expected ErrNotFound, got err: %v, value: %v", err, val)
}
if txn.Discard(); err != nil {
t.Fatal(err)
}
if val, err := d.Get(key); err != ds.ErrNotFound {
t.Fatalf("expected ErrNotFound, got err: %v, value: %v", err, val)
}
}
func TestTransactionManyOperations(t *testing.T) {
keys := []ds.Key{ds.NewKey("/test/key1"), ds.NewKey("/test/key2"), ds.NewKey("/test/key3"), ds.NewKey("/test/key4"), ds.NewKey("/test/key5")}
d, done := newDS(t)
defer done()
txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}
defer txn.Discard()
// Insert all entries.
for i := 0; i < 5; i++ {
if err := txn.Put(keys[i], []byte(fmt.Sprintf("hello%d", i))); err != nil {
t.Fatal(err)
}
}
// Remove the third entry.
if err := txn.Delete(keys[2]); err != nil {
t.Fatal(err)
}
// Check existences.
if has, err := txn.Has(keys[1]); err != nil || !has {
t.Fatalf("expected key[1] to be present, err: %v, has: %v", err, has)
}
if has, err := txn.Has(keys[2]); err != nil || has {
t.Fatalf("expected key[2] to be absent, err: %v, has: %v", err, has)
}
var res dsq.Results
if res, err = txn.Query(dsq.Query{Prefix: "/test"}); err != nil {
t.Fatalf("query failed, err: %v", err)
}
if entries, err := res.Rest(); err != nil || len(entries) != 4 {
t.Fatalf("query failed or contained unexpected number of entries, err: %v, results: %v", err, entries)
}
txn.Discard()
}
func TestSuite(t *testing.T) {
d := newDSMem(t)
defer d.Close()
dstest.SubtestAll(t, d)
}