cosmos-sdk/orm/model/ormtable/auto_increment_test.go
Tyler f6150bd4af
refactor(ORM)!: InsertReturningID -> InsertReturning<PrimaryKeyName> (#11659)
## Description

- changes the generated function signature for InsertReturningID to InsertReturning[AutoIncrement Field Name] 

Closes: #11655 



---

### 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-05-13 21:47:55 +00:00

98 lines
3.4 KiB
Go

package ormtable_test
import (
"bytes"
"context"
"os"
"strings"
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
"github.com/cosmos/cosmos-sdk/orm/internal/testkv"
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
"github.com/cosmos/cosmos-sdk/orm/model/ormtable"
)
func TestAutoIncrementScenario(t *testing.T) {
table, err := ormtable.Build(ormtable.Options{
MessageType: (&testpb.ExampleAutoIncrementTable{}).ProtoReflect().Type(),
})
assert.NilError(t, err)
autoTable, ok := table.(ormtable.AutoIncrementTable)
assert.Assert(t, ok)
// first run tests with a split index-commitment store
runAutoIncrementScenario(t, autoTable, ormtable.WrapContextDefault(testkv.NewSplitMemBackend()))
// now run with shared store and debugging
debugBuf := &strings.Builder{}
store := testkv.NewDebugBackend(
testkv.NewSharedMemBackend(),
&testkv.EntryCodecDebugger{
EntryCodec: table,
Print: func(s string) { debugBuf.WriteString(s + "\n") },
},
)
runAutoIncrementScenario(t, autoTable, ormtable.WrapContextDefault(store))
golden.Assert(t, debugBuf.String(), "test_auto_inc.golden")
checkEncodeDecodeEntries(t, table, store.IndexStoreReader())
}
func runAutoIncrementScenario(t *testing.T, table ormtable.AutoIncrementTable, ctx context.Context) {
store, err := testpb.NewExampleAutoIncrementTableTable(table)
assert.NilError(t, err)
err = store.Save(ctx, &testpb.ExampleAutoIncrementTable{Id: 5})
assert.ErrorContains(t, err, "not found")
ex1 := &testpb.ExampleAutoIncrementTable{X: "foo", Y: 5}
assert.NilError(t, store.Save(ctx, ex1))
assert.Equal(t, uint64(1), ex1.Id)
ex2 := &testpb.ExampleAutoIncrementTable{X: "bar", Y: 10}
newId, err := table.InsertReturningPKey(ctx, ex2)
assert.NilError(t, err)
assert.Equal(t, uint64(2), ex2.Id)
assert.Equal(t, newId, ex2.Id)
buf := &bytes.Buffer{}
assert.NilError(t, table.ExportJSON(ctx, buf))
assert.NilError(t, table.ValidateJSON(bytes.NewReader(buf.Bytes())))
store2 := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
assert.NilError(t, table.ImportJSON(store2, bytes.NewReader(buf.Bytes())))
assertTablesEqual(t, table, ctx, store2)
// test edge case where we have deleted all entities but we're still exporting the sequence number
assert.NilError(t, table.Delete(ctx, ex1))
assert.NilError(t, table.Delete(ctx, ex2))
buf = &bytes.Buffer{}
assert.NilError(t, table.ExportJSON(ctx, buf))
assert.NilError(t, table.ValidateJSON(bytes.NewReader(buf.Bytes())))
golden.Assert(t, buf.String(), "trivial_auto_inc_export.golden")
store3 := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
assert.NilError(t, table.ImportJSON(store3, bytes.NewReader(buf.Bytes())))
ex1.Id = 0
assert.NilError(t, table.Insert(store3, ex1))
assert.Equal(t, uint64(3), ex1.Id) // should equal 3 because the sequence number 2 should have been imported from JSON
}
func TestBadJSON(t *testing.T) {
table, err := ormtable.Build(ormtable.Options{
MessageType: (&testpb.ExampleAutoIncrementTable{}).ProtoReflect().Type(),
})
assert.NilError(t, err)
store := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
f, err := os.Open("testdata/bad_auto_inc.json")
assert.NilError(t, err)
assert.ErrorContains(t, table.ImportJSON(store, f), "invalid auto increment primary key")
f, err = os.Open("testdata/bad_auto_inc2.json")
assert.NilError(t, err)
assert.ErrorContains(t, table.ImportJSON(store, f), "invalid auto increment primary key")
}