forked from cometbft/cometbft-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixdb_test.go
117 lines (102 loc) · 3.11 KB
/
prefixdb_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
package db
import (
"testing"
"github.com/stretchr/testify/require"
)
func mockDBWithStuff(t *testing.T) DB {
db := NewMemDB()
// Under "key" prefix
require.NoError(t, db.Set(bz("key"), bz("value")))
require.NoError(t, db.Set(bz("key1"), bz("value1")))
require.NoError(t, db.Set(bz("key2"), bz("value2")))
require.NoError(t, db.Set(bz("key3"), bz("value3")))
require.NoError(t, db.Set(bz("something"), bz("else")))
require.NoError(t, db.Set(bz("k"), bz("val")))
require.NoError(t, db.Set(bz("ke"), bz("valu")))
require.NoError(t, db.Set(bz("kee"), bz("valuu")))
return db
}
func TestPrefixDBSimple(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
checkValue(t, pdb, bz("key"), nil)
checkValue(t, pdb, bz("key1"), nil)
checkValue(t, pdb, bz("1"), bz("value1"))
checkValue(t, pdb, bz("key2"), nil)
checkValue(t, pdb, bz("2"), bz("value2"))
checkValue(t, pdb, bz("key3"), nil)
checkValue(t, pdb, bz("3"), bz("value3"))
checkValue(t, pdb, bz("something"), nil)
checkValue(t, pdb, bz("k"), nil)
checkValue(t, pdb, bz("ke"), nil)
checkValue(t, pdb, bz("kee"), nil)
}
func TestPrefixDBIterator1(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.Iterator(nil, nil)
require.NoError(t, err)
checkDomain(t, itr, nil, nil)
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, true)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator1(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(nil, nil)
require.NoError(t, err)
checkDomain(t, itr, nil, nil)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, true)
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator5(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(bz("1"), nil)
require.NoError(t, err)
checkDomain(t, itr, bz("1"), nil)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, true)
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator6(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(bz("2"), nil)
require.NoError(t, err)
checkDomain(t, itr, bz("2"), nil)
checkItem(t, itr, bz("3"), bz("value3"))
checkNext(t, itr, true)
checkItem(t, itr, bz("2"), bz("value2"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}
func TestPrefixDBReverseIterator7(t *testing.T) {
db := mockDBWithStuff(t)
pdb := NewPrefixDB(db, bz("key"))
itr, err := pdb.ReverseIterator(nil, bz("2"))
require.NoError(t, err)
checkDomain(t, itr, nil, bz("2"))
checkItem(t, itr, bz("1"), bz("value1"))
checkNext(t, itr, false)
checkInvalid(t, itr)
itr.Close()
}