feat(orm): add mock hooks (#11135)
* feat(orm): add mock hooks * add hooks * add tests * update docs * Update orm/testing/ormmocks/docs.go Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com> * add Backend.WithHooks method Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
@@ -0,0 +1,77 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: orm/model/ormtable/hooks.go
|
||||
|
||||
// Package ormmocks is a generated GoMock package.
|
||||
package ormmocks
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
proto "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// MockHooks is a mock of Hooks interface.
|
||||
type MockHooks struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockHooksMockRecorder
|
||||
}
|
||||
|
||||
// MockHooksMockRecorder is the mock recorder for MockHooks.
|
||||
type MockHooksMockRecorder struct {
|
||||
mock *MockHooks
|
||||
}
|
||||
|
||||
// NewMockHooks creates a new mock instance.
|
||||
func NewMockHooks(ctrl *gomock.Controller) *MockHooks {
|
||||
mock := &MockHooks{ctrl: ctrl}
|
||||
mock.recorder = &MockHooksMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockHooks) EXPECT() *MockHooksMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// OnDelete mocks base method.
|
||||
func (m *MockHooks) OnDelete(arg0 proto.Message) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "OnDelete", arg0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// OnDelete indicates an expected call of OnDelete.
|
||||
func (mr *MockHooksMockRecorder) OnDelete(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnDelete", reflect.TypeOf((*MockHooks)(nil).OnDelete), arg0)
|
||||
}
|
||||
|
||||
// OnInsert mocks base method.
|
||||
func (m *MockHooks) OnInsert(arg0 proto.Message) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "OnInsert", arg0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// OnInsert indicates an expected call of OnInsert.
|
||||
func (mr *MockHooksMockRecorder) OnInsert(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnInsert", reflect.TypeOf((*MockHooks)(nil).OnInsert), arg0)
|
||||
}
|
||||
|
||||
// OnUpdate mocks base method.
|
||||
func (m *MockHooks) OnUpdate(existing, new proto.Message) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "OnUpdate", existing, new)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// OnUpdate indicates an expected call of OnUpdate.
|
||||
func (mr *MockHooksMockRecorder) OnUpdate(existing, new interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnUpdate", reflect.TypeOf((*MockHooks)(nil).OnUpdate), existing, new)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user