forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquad_test.go
61 lines (49 loc) · 2.08 KB
/
quad_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
package collections_test
import (
"testing"
"github.com/stretchr/testify/require"
"cosmossdk.io/collections"
"cosmossdk.io/collections/colltest"
coretesting "cosmossdk.io/core/testing"
)
func TestQuad(t *testing.T) {
kc := collections.QuadKeyCodec(collections.Uint64Key, collections.StringKey, collections.BytesKey, collections.BoolKey)
t.Run("conformance", func(t *testing.T) {
colltest.TestKeyCodec(t, kc, collections.Join4(uint64(1), "2", []byte("3"), true))
})
}
func TestQuadRange(t *testing.T) {
ctx := coretesting.Context()
sk := coretesting.KVStoreService(ctx, "test")
schema := collections.NewSchemaBuilder(sk)
// this is a key composed of 4 parts: uint64, string, []byte, bool
kc := collections.QuadKeyCodec(collections.Uint64Key, collections.StringKey, collections.BytesKey, collections.BoolKey)
keySet := collections.NewKeySet(schema, collections.NewPrefix(0), "Quad", kc)
keys := []collections.Quad[uint64, string, []byte, bool]{
collections.Join4(uint64(1), "A", []byte("1"), true),
collections.Join4(uint64(1), "A", []byte("2"), true),
collections.Join4(uint64(1), "B", []byte("3"), false),
collections.Join4(uint64(2), "B", []byte("4"), false),
}
for _, k := range keys {
require.NoError(t, keySet.Set(ctx, k))
}
// we prefix over (1) we expect 3 results
iter, err := keySet.Iterate(ctx, collections.NewPrefixedQuadRange[uint64, string, []byte, bool](uint64(1)))
require.NoError(t, err)
gotKeys, err := iter.Keys()
require.NoError(t, err)
require.Equal(t, keys[:3], gotKeys)
// we super prefix over Join(1, "A") we expect 2 results
iter, err = keySet.Iterate(ctx, collections.NewSuperPrefixedQuadRange[uint64, string, []byte, bool](1, "A"))
require.NoError(t, err)
gotKeys, err = iter.Keys()
require.NoError(t, err)
require.Equal(t, keys[:2], gotKeys)
// we super prefix 3 over Join(1, "A", []byte("1")) we expect 1 result
iter, err = keySet.Iterate(ctx, collections.NewSuperPrefixedQuadRange3[uint64, string, []byte, bool](1, "A", []byte("1")))
require.NoError(t, err)
gotKeys, err = iter.Keys()
require.NoError(t, err)
require.Equal(t, keys[:1], gotKeys)
}