cosmos-sdk/orm/encoding/ormkv/unique_key_test.go
Aaron Craelius 1486a669b7
fix(orm): add additional checks on invalid tables (#11387)
## Description

Adds checks for two types of errors in table definitions
* defining a key on an `optional` field (haven't found a way to test unfortunately because a test case will break the code generator)
* defining a trivial unique key, essentially a unique key which contains all the fields in the primary key and is redundant



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-04-05 19:48:58 +00:00

94 lines
3.0 KiB
Go

package ormkv_test
import (
"bytes"
"fmt"
"testing"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"gotest.tools/v3/assert"
"pgregory.net/rapid"
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
"github.com/cosmos/cosmos-sdk/orm/internal/testutil"
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
)
func TestUniqueKeyCodec(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
keyCodec := testutil.TestKeyCodecGen(1, 5).Draw(t, "keyCodec").(testutil.TestKeyCodec)
pkCodec := testutil.TestKeyCodecGen(1, 5).Draw(t, "primaryKeyCodec").(testutil.TestKeyCodec)
// check if we have a trivial unique index where all of the fields
// in the primary key are in the unique key, we should expect an
// error in this case
isInPk := map[protoreflect.Name]bool{}
for _, spec := range pkCodec.KeySpecs {
isInPk[spec.FieldName] = true
}
numPkFields := 0
for _, spec := range keyCodec.KeySpecs {
if isInPk[spec.FieldName] {
numPkFields++
}
}
isTrivialUniqueKey := numPkFields == len(pkCodec.KeySpecs)
messageType := (&testpb.ExampleTable{}).ProtoReflect().Type()
uniqueKeyCdc, err := ormkv.NewUniqueKeyCodec(
keyCodec.Codec.Prefix(),
messageType,
keyCodec.Codec.GetFieldNames(),
pkCodec.Codec.GetFieldNames(),
)
if isTrivialUniqueKey {
assert.ErrorContains(t, err, "no new uniqueness constraint")
return
} else {
assert.NilError(t, err)
}
for i := 0; i < 100; i++ {
a := testutil.GenA.Draw(t, fmt.Sprintf("a%d", i)).(*testpb.ExampleTable)
key := keyCodec.Codec.GetKeyValues(a.ProtoReflect())
pk := pkCodec.Codec.GetKeyValues(a.ProtoReflect())
uniq1 := &ormkv.IndexKeyEntry{
TableName: messageType.Descriptor().FullName(),
Fields: keyCodec.Codec.GetFieldNames(),
IsUnique: true,
IndexValues: key,
PrimaryKey: pk,
}
k, v, err := uniqueKeyCdc.EncodeEntry(uniq1)
assert.NilError(t, err)
k2, v2, err := uniqueKeyCdc.EncodeKVFromMessage(a.ProtoReflect())
assert.NilError(t, err)
assert.Assert(t, bytes.Equal(k, k2))
assert.Assert(t, bytes.Equal(v, v2))
entry2, err := uniqueKeyCdc.DecodeEntry(k, v)
assert.NilError(t, err)
uniq2 := entry2.(*ormkv.IndexKeyEntry)
assert.Equal(t, 0, keyCodec.Codec.CompareKeys(uniq1.IndexValues, uniq2.IndexValues))
assert.Equal(t, 0, pkCodec.Codec.CompareKeys(uniq1.PrimaryKey, uniq2.PrimaryKey))
assert.Equal(t, true, uniq2.IsUnique)
assert.Equal(t, messageType.Descriptor().FullName(), uniq2.TableName)
assert.DeepEqual(t, uniq1.Fields, uniq2.Fields)
idxFields, pk2, err := uniqueKeyCdc.DecodeIndexKey(k, v)
assert.NilError(t, err)
assert.Equal(t, 0, keyCodec.Codec.CompareKeys(key, idxFields))
assert.Equal(t, 0, pkCodec.Codec.CompareKeys(pk, pk2))
}
})
}
func TestTrivialUnique(t *testing.T) {
_, err := ormkv.NewUniqueKeyCodec(nil, (&testpb.ExampleTable{}).ProtoReflect().Type(),
[]protoreflect.Name{"u32", "str"}, []protoreflect.Name{"str", "u32"})
assert.ErrorIs(t, err, ormerrors.InvalidTableDefinition)
}