2021-11-30 13:27:17 -08:00
|
|
|
package ormkv_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"pgregory.net/rapid"
|
|
|
|
|
2022-01-20 08:22:06 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/orm/encoding/encodeutil"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
|
2021-11-30 13:27:17 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/orm/internal/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeyCodec(t *testing.T) {
|
|
|
|
rapid.Check(t, func(t *rapid.T) {
|
2021-12-02 19:21:57 -08:00
|
|
|
key := testutil.TestKeyCodecGen(0, 5).Draw(t, "key").(testutil.TestKeyCodec)
|
2021-11-30 13:27:17 -08:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
keyValues := key.Draw(t, "values")
|
|
|
|
|
|
|
|
bz1 := assertEncDecKey(t, key, keyValues)
|
|
|
|
|
|
|
|
if key.Codec.IsFullyOrdered() {
|
|
|
|
// check if ordered keys have ordered encodings
|
|
|
|
keyValues2 := key.Draw(t, "values2")
|
|
|
|
bz2 := assertEncDecKey(t, key, keyValues2)
|
|
|
|
// bytes comparison should equal comparison of values
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.Equal(t, key.Codec.CompareKeys(keyValues, keyValues2), bytes.Compare(bz1, bz2))
|
2021-11-30 13:27:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertEncDecKey(t *rapid.T, key testutil.TestKeyCodec, keyValues []protoreflect.Value) []byte {
|
2022-01-20 08:22:06 -08:00
|
|
|
bz, err := key.Codec.EncodeKey(keyValues)
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-20 08:22:06 -08:00
|
|
|
keyValues2, err := key.Codec.DecodeKey(bytes.NewReader(bz))
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.Equal(t, 0, key.Codec.CompareKeys(keyValues, keyValues2))
|
2021-11-30 13:27:17 -08:00
|
|
|
return bz
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompareValues(t *testing.T) {
|
2021-12-02 19:21:57 -08:00
|
|
|
cdc, err := ormkv.NewKeyCodec(nil,
|
2022-01-20 08:22:06 -08:00
|
|
|
(&testpb.ExampleTable{}).ProtoReflect().Type(),
|
2021-12-02 19:21:57 -08:00
|
|
|
[]protoreflect.Name{"u32", "str", "i32"})
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
values1 []protoreflect.Value
|
|
|
|
values2 []protoreflect.Value
|
|
|
|
expect int
|
|
|
|
validRange bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"eq",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", int32(-3)),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", int32(-3)),
|
2021-11-30 13:27:17 -08:00
|
|
|
0,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"eq prefix 0",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(),
|
|
|
|
encodeutil.ValuesOf(),
|
2021-11-30 13:27:17 -08:00
|
|
|
0,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"eq prefix 1",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
2021-11-30 13:27:17 -08:00
|
|
|
0,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"eq prefix 2",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc"),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc"),
|
2021-11-30 13:27:17 -08:00
|
|
|
0,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lt1",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", int32(-3)),
|
|
|
|
encodeutil.ValuesOf(uint32(1), "abc", int32(-3)),
|
2021-11-30 13:27:17 -08:00
|
|
|
-1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lt2",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(1), "abb", int32(-3)),
|
|
|
|
encodeutil.ValuesOf(uint32(1), "abc", int32(-3)),
|
2021-11-30 13:27:17 -08:00
|
|
|
-1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lt3",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(1), "abb", int32(-4)),
|
|
|
|
encodeutil.ValuesOf(uint32(1), "abb", int32(-3)),
|
2021-11-30 13:27:17 -08:00
|
|
|
-1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"less prefix 0",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(),
|
|
|
|
encodeutil.ValuesOf(uint32(1), "abb", int32(-4)),
|
2021-11-30 13:27:17 -08:00
|
|
|
-1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"less prefix 1",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(1)),
|
|
|
|
encodeutil.ValuesOf(uint32(1), "abb", int32(-4)),
|
2021-11-30 13:27:17 -08:00
|
|
|
-1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"less prefix 2",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(1), "abb"),
|
|
|
|
encodeutil.ValuesOf(uint32(1), "abb", int32(-4)),
|
2021-11-30 13:27:17 -08:00
|
|
|
-1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gt1",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(2), "abb", int32(-4)),
|
|
|
|
encodeutil.ValuesOf(uint32(1), "abb", int32(-4)),
|
2021-11-30 13:27:17 -08:00
|
|
|
1,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gt2",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(2), "abc", int32(-4)),
|
|
|
|
encodeutil.ValuesOf(uint32(2), "abb", int32(-4)),
|
2021-11-30 13:27:17 -08:00
|
|
|
1,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gt3",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(2), "abc", int32(1)),
|
|
|
|
encodeutil.ValuesOf(uint32(2), "abc", int32(-3)),
|
2021-11-30 13:27:17 -08:00
|
|
|
1,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gt prefix 0",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(2), "abc", int32(-3)),
|
|
|
|
encodeutil.ValuesOf(),
|
2021-11-30 13:27:17 -08:00
|
|
|
1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gt prefix 1",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(2), "abc", int32(-3)),
|
|
|
|
encodeutil.ValuesOf(uint32(2)),
|
2021-11-30 13:27:17 -08:00
|
|
|
1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gt prefix 2",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(2), "abc", int32(-3)),
|
|
|
|
encodeutil.ValuesOf(uint32(2), "abc"),
|
2021-11-30 13:27:17 -08:00
|
|
|
1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
assert.Equal(
|
|
|
|
t, test.expect,
|
2022-01-20 08:22:06 -08:00
|
|
|
cdc.CompareKeys(test.values1, test.values2),
|
2021-11-30 13:27:17 -08:00
|
|
|
)
|
|
|
|
// CheckValidRangeIterationKeys should give comparable results
|
|
|
|
err := cdc.CheckValidRangeIterationKeys(test.values1, test.values2)
|
|
|
|
if test.validRange {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.ErrorContains(t, err, "")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodePrefixKey(t *testing.T) {
|
2021-12-02 19:21:57 -08:00
|
|
|
cdc, err := ormkv.NewKeyCodec(nil,
|
2022-01-20 08:22:06 -08:00
|
|
|
(&testpb.ExampleTable{}).ProtoReflect().Type(),
|
2021-12-02 19:21:57 -08:00
|
|
|
[]protoreflect.Name{"u32", "str", "bz", "i32"})
|
2021-11-30 13:27:17 -08:00
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
values []protoreflect.Value
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"1",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(5), "abc"),
|
2021-11-30 13:27:17 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-01-20 08:22:06 -08:00
|
|
|
bz, err := cdc.EncodeKey(test.values)
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-20 08:22:06 -08:00
|
|
|
values, err := cdc.DecodeKey(bytes.NewReader(bz))
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.ErrorType(t, err, io.EOF)
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.Equal(t, 0, cdc.CompareKeys(test.values, values))
|
2021-11-30 13:27:17 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidRangeIterationKeys(t *testing.T) {
|
2021-12-02 19:21:57 -08:00
|
|
|
cdc, err := ormkv.NewKeyCodec(nil,
|
2022-01-20 08:22:06 -08:00
|
|
|
(&testpb.ExampleTable{}).ProtoReflect().Type(),
|
2021-12-02 19:21:57 -08:00
|
|
|
[]protoreflect.Name{"u32", "str", "bz", "i32"})
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
values1 []protoreflect.Value
|
|
|
|
values2 []protoreflect.Value
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"1 eq",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
2021-11-30 13:27:17 -08:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1 lt",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
|
|
|
encodeutil.ValuesOf(uint32(1)),
|
2021-11-30 13:27:17 -08:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1 gt",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(1)),
|
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
2021-11-30 13:27:17 -08:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1,2 lt",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc"),
|
2021-11-30 13:27:17 -08:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1,2 gt",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc"),
|
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
2021-11-30 13:27:17 -08:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1,2,3",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0)),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}),
|
2021-11-30 13:27:17 -08:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1,2,3,4 lt",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}, int32(-1)),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}, int32(1)),
|
2021-11-30 13:27:17 -08:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"too long",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}, int32(-1)),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}, int32(1), int32(1)),
|
2021-11-30 13:27:17 -08:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1,2,3,4 eq",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}, int32(1)),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}, int32(1)),
|
2021-11-30 13:27:17 -08:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1,2,3,4 bz err",
|
2022-01-20 08:22:06 -08:00
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2}, int32(-1)),
|
|
|
|
encodeutil.ValuesOf(uint32(0), "abc", []byte{1, 2, 3}, int32(1)),
|
2021-11-30 13:27:17 -08:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
err := cdc.CheckValidRangeIterationKeys(test.values1, test.values2)
|
|
|
|
if test.expectErr {
|
|
|
|
assert.ErrorContains(t, err, "")
|
|
|
|
} else {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSet(t *testing.T) {
|
2021-12-02 19:21:57 -08:00
|
|
|
cdc, err := ormkv.NewKeyCodec(nil,
|
2022-01-20 08:22:06 -08:00
|
|
|
(&testpb.ExampleTable{}).ProtoReflect().Type(),
|
2021-12-02 19:21:57 -08:00
|
|
|
[]protoreflect.Name{"u32", "str", "i32"})
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-01-20 08:22:06 -08:00
|
|
|
var a testpb.ExampleTable
|
|
|
|
values := encodeutil.ValuesOf(uint32(4), "abc", int32(1))
|
|
|
|
cdc.SetKeyValues(a.ProtoReflect(), values)
|
|
|
|
values2 := cdc.GetKeyValues(a.ProtoReflect())
|
|
|
|
assert.Equal(t, 0, cdc.CompareKeys(values, values2))
|
|
|
|
bz, err := cdc.EncodeKey(values)
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-20 08:22:06 -08:00
|
|
|
values3, bz2, err := cdc.EncodeKeyFromMessage(a.ProtoReflect())
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.Equal(t, 0, cdc.CompareKeys(values, values3))
|
2021-11-30 13:27:17 -08:00
|
|
|
assert.Assert(t, bytes.Equal(bz, bz2))
|
|
|
|
}
|