cosmos-sdk/orm/model/ormtable/save_test.go
Aaron Craelius 872d9247b8
feat(orm): gRPC codes for save errors (#11386)
## Description

Ref: #11088. First PR of several to add gRPC error codes and make sure that errors are a well-defined part of the API.

Also introduces Cucumber-style BDD acceptance tests into the SDK via https://github.com/regen-network/gocuke.



---

### 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-03-17 18:57:55 +00:00

69 lines
1.7 KiB
Go

package ormtable_test
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/orm/model/ormtable"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"testing"
"github.com/regen-network/gocuke"
"gotest.tools/v3/assert"
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
"github.com/cosmos/cosmos-sdk/orm/testing/ormtest"
)
func TestSave(t *testing.T) {
gocuke.NewRunner(t, &suite{}).Path("../../features/table/saving.feature").Run()
}
type suite struct {
gocuke.TestingT
table ormtable.Table
ctx context.Context
err error
}
func (s *suite) Before() {
var err error
s.table, err = ormtable.Build(ormtable.Options{
MessageType: (&testpb.SimpleExample{}).ProtoReflect().Type(),
})
assert.NilError(s, err)
s.ctx = ormtable.WrapContextDefault(ormtest.NewMemoryBackend())
}
func (s *suite) AnExistingEntity(docString gocuke.DocString) {
existing := s.simpleExampleFromDocString(docString)
assert.NilError(s, s.table.Insert(s.ctx, existing))
}
func (s suite) simpleExampleFromDocString(docString gocuke.DocString) *testpb.SimpleExample {
ex := &testpb.SimpleExample{}
assert.NilError(s, protojson.Unmarshal([]byte(docString.Content), ex))
return ex
}
func (s *suite) IInsert(a gocuke.DocString) {
ex := s.simpleExampleFromDocString(a)
s.err = s.table.Insert(s.ctx, ex)
}
func (s *suite) IUpdate(a gocuke.DocString) {
ex := s.simpleExampleFromDocString(a)
s.err = s.table.Update(s.ctx, ex)
}
func (s *suite) ExpectAError(a string) {
assert.ErrorContains(s, s.err, a)
}
func (s *suite) ExpectGrpcErrorCode(a string) {
var code codes.Code
assert.NilError(s, code.UnmarshalJSON([]byte(fmt.Sprintf("%q", a))))
assert.Equal(s, code, status.Code(s.err))
}