forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
82 lines (66 loc) · 2.09 KB
/
map_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
package collections
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestMap(t *testing.T) {
sk, ctx := deps()
schemaBuilder := NewSchemaBuilder(sk)
m := NewMap(schemaBuilder, NewPrefix("hi"), "m", Uint64Key, Uint64Value)
_, err := schemaBuilder.Build()
require.NoError(t, err)
// test not has
has, err := m.Has(ctx, 1)
require.NoError(t, err)
require.False(t, has)
// test get error
_, err = m.Get(ctx, 1)
require.ErrorIs(t, err, ErrNotFound)
// test set/get
err = m.Set(ctx, 1, 100)
require.NoError(t, err)
v, err := m.Get(ctx, 1)
require.NoError(t, err)
require.Equal(t, uint64(100), v)
// test remove
err = m.Remove(ctx, 1)
require.NoError(t, err)
has, err = m.Has(ctx, 1)
require.NoError(t, err)
require.False(t, has)
}
func TestMap_IterateRaw(t *testing.T) {
sk, ctx := deps()
// safety check to ensure prefix boundaries are not crossed
require.NoError(t, sk.OpenKVStore(ctx).Set([]byte{0x0, 0x0}, []byte("before prefix")))
require.NoError(t, sk.OpenKVStore(ctx).Set([]byte{0x2, 0x0}, []byte("after prefix")))
sb := NewSchemaBuilder(sk)
m := NewMap(sb, NewPrefix(1), "m", Uint64Key, Uint64Value)
require.NoError(t, m.Set(ctx, 0, 0))
require.NoError(t, m.Set(ctx, 1, 1))
require.NoError(t, m.Set(ctx, 2, 2))
// test non nil end in ascending order
twoBigEndian, err := encodeKeyWithPrefix(nil, Uint64Key, 2)
require.NoError(t, err)
iter, err := m.IterateRaw(ctx, nil, twoBigEndian, OrderAscending)
require.NoError(t, err)
defer iter.Close()
keys, err := iter.Keys()
require.NoError(t, err)
require.Equal(t, []uint64{0, 1}, keys)
// test nil end in reverse
iter, err = m.IterateRaw(ctx, nil, nil, OrderDescending)
require.NoError(t, err)
defer iter.Close()
keys, err = iter.Keys()
require.NoError(t, err)
require.Equal(t, []uint64{2, 1, 0}, keys)
}
func Test_encodeKey(t *testing.T) {
prefix := "prefix"
number := []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
expectedKey := append([]byte(prefix), number...)
gotKey, err := encodeKeyWithPrefix(NewPrefix(prefix).Bytes(), Uint64Key, 0)
require.NoError(t, err)
require.Equal(t, expectedKey, gotKey)
}