cosmos-sdk/x/group/internal/orm/table_test.go

234 lines
5.3 KiB
Go
Raw Normal View History

feat: Add Table-Store (aka ORM) package - Table and Indexable (#9751) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description ref: https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/discussions/9156 This is the first step towards the migration of the [Table-Store (ORM) package](https://github.com/regen-network/regen-ledger/tree/c99dbedd1ff6b5b08ae7da63eec8dd19bff8273e/orm) currently in regen-ledger into the SDK. This won't be exposed as a public API to start with but rather be internal to `x/group`. This first PR brings these core concepts: - `table` is the high level object for storage mapper functionality where entities are stored by an unique identifier called `RowID` - `Indexable` types can be used to setup new tables. There will be follow-up PRs for adding the following features: - table types with auto-incrementing `uint64` primary keys and natural primary keys - secondary indexes - iterator and pagination - import/export genesis --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] 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) _not applicable_ - [x] 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` _not applicable_ - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] 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)
2021-10-21 03:19:52 -07:00
package orm
import (
"fmt"
"testing"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewTable(t *testing.T) {
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
testCases := []struct {
name string
model codec.ProtoMarshaler
expectErr bool
expectedErr string
}{
{
name: "nil model",
model: nil,
expectErr: true,
expectedErr: "Model must not be nil",
},
{
name: "all not nil",
model: &testdata.TableModel{},
expectErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
table, err := newTable([2]byte{0x1}, tc.model, cdc)
if tc.expectErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErr)
} else {
require.NoError(t, err)
require.NotNil(t, table)
}
})
}
}
func TestCreate(t *testing.T) {
specs := map[string]struct {
rowID RowID
src codec.ProtoMarshaler
expErr *errors.Error
}{
"empty rowID": {
rowID: []byte{},
src: &testdata.TableModel{
Id: 1,
Name: "some name",
},
expErr: errors.ErrORMEmptyKey,
},
"happy path": {
rowID: EncodeSequence(1),
src: &testdata.TableModel{
Id: 1,
Name: "some name",
},
},
"wrong type": {
rowID: EncodeSequence(1),
src: &testdata.Cat{
Moniker: "cat moniker",
Lives: 10,
},
expErr: errors.ErrInvalidType,
},
"model validation fails": {
rowID: EncodeSequence(1),
src: &testdata.TableModel{
Id: 1,
Name: "",
},
expErr: testdata.ErrTest,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
ctx := NewMockContext()
store := ctx.KVStore(sdk.NewKVStoreKey("test"))
var anyPrefix = [2]byte{0x10}
myTable, err := newTable(anyPrefix, &testdata.TableModel{}, cdc)
require.NoError(t, err)
err = myTable.Create(store, spec.rowID, spec.src)
require.True(t, spec.expErr.Is(err), err)
shouldExists := spec.expErr == nil
assert.Equal(t, shouldExists, myTable.Has(store, spec.rowID), fmt.Sprintf("expected %v", shouldExists))
// then
var loaded testdata.TableModel
err = myTable.GetOne(store, spec.rowID, &loaded)
if spec.expErr != nil {
require.True(t, errors.ErrNotFound.Is(err))
return
}
require.NoError(t, err)
assert.Equal(t, spec.src, &loaded)
})
}
}
func TestUpdate(t *testing.T) {
specs := map[string]struct {
src codec.ProtoMarshaler
expErr *errors.Error
}{
"happy path": {
src: &testdata.TableModel{
Id: 1,
Name: "some name",
},
},
"wrong type": {
src: &testdata.Cat{
Moniker: "cat moniker",
Lives: 10,
},
expErr: errors.ErrInvalidType,
},
"model validation fails": {
src: &testdata.TableModel{
Id: 1,
Name: "",
},
expErr: testdata.ErrTest,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
ctx := NewMockContext()
store := ctx.KVStore(sdk.NewKVStoreKey("test"))
var anyPrefix = [2]byte{0x10}
myTable, err := newTable(anyPrefix, &testdata.TableModel{}, cdc)
require.NoError(t, err)
initValue := testdata.TableModel{
Id: 1,
Name: "old name",
}
err = myTable.Create(store, EncodeSequence(1), &initValue)
require.NoError(t, err)
// when
err = myTable.Update(store, EncodeSequence(1), spec.src)
require.True(t, spec.expErr.Is(err), "got ", err)
// then
var loaded testdata.TableModel
require.NoError(t, myTable.GetOne(store, EncodeSequence(1), &loaded))
if spec.expErr == nil {
assert.Equal(t, spec.src, &loaded)
} else {
assert.Equal(t, initValue, loaded)
}
})
}
}
func TestDelete(t *testing.T) {
specs := map[string]struct {
rowId []byte
expErr *errors.Error
}{
"happy path": {
rowId: EncodeSequence(1),
},
"not found": {
rowId: []byte("not-found"),
expErr: errors.ErrNotFound,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
ctx := NewMockContext()
store := ctx.KVStore(sdk.NewKVStoreKey("test"))
var anyPrefix = [2]byte{0x10}
myTable, err := newTable(anyPrefix, &testdata.TableModel{}, cdc)
require.NoError(t, err)
initValue := testdata.TableModel{
Id: 1,
Name: "some name",
}
err = myTable.Create(store, EncodeSequence(1), &initValue)
require.NoError(t, err)
// when
err = myTable.Delete(store, spec.rowId)
require.True(t, spec.expErr.Is(err), "got ", err)
// then
var loaded testdata.TableModel
if spec.expErr == errors.ErrNotFound {
require.NoError(t, myTable.GetOne(store, EncodeSequence(1), &loaded))
assert.Equal(t, initValue, loaded)
} else {
err := myTable.GetOne(store, EncodeSequence(1), &loaded)
require.Error(t, err)
require.Equal(t, err, errors.ErrNotFound)
}
})
}
}