forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(orm): add mock hooks (cosmos#11135)
* feat(orm): add mock hooks * add hooks * add tests * update docs * Update orm/testing/ormmocks/docs.go Co-authored-by: Tyler <[email protected]> * add Backend.WithHooks method Co-authored-by: Tyler <[email protected]>
- Loading branch information
1 parent
4addb73
commit 77ac8fa
Showing
8 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Package ormmocks contains generated mocks for orm types that can be used | ||
// in testing. Right now, this package only contains a mock for ormtable.Hooks | ||
// as this useful way for unit testing using an in-memory database. Rather | ||
// than attempting to mock a whole table or database instance, instead | ||
// a mock Hook instance can be passed in to verify that the expected | ||
// insert/update/delete operations are happening in the database. | ||
// | ||
// The Eq function gomock.Matcher that compares protobuf messages can | ||
// be used in gomock EXPECT functions. | ||
// | ||
// See TestHooks in ormdb/module_test.go for examples of how to use | ||
// mock Hooks in a real-world scenario. | ||
package ormmocks |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ormmocks | ||
|
||
import ( | ||
"github.com/golang/mock/gomock" | ||
"github.com/google/go-cmp/cmp" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
) | ||
|
||
// Code adapted from MIT-licensed https://github.com/budougumi0617/cmpmock/blob/master/diffmatcher.go | ||
|
||
// Eq returns a gomock.Matcher which uses go-cmp to compare protobuf messages. | ||
func Eq(message proto.Message) gomock.Matcher { | ||
return &protoEq{message: message} | ||
} | ||
|
||
type protoEq struct { | ||
message interface{} | ||
diff string | ||
} | ||
|
||
func (p protoEq) Matches(x interface{}) bool { | ||
p.diff = cmp.Diff(x, p.message, protocmp.Transform()) | ||
return len(p.diff) == 0 | ||
} | ||
|
||
func (p protoEq) String() string { | ||
return p.diff | ||
} |