diff --git a/Makefile b/Makefile index 023b6aab..c7076993 100644 --- a/Makefile +++ b/Makefile @@ -379,6 +379,9 @@ mock: ## Generate all the mocks (for tests) @mockgen -destination x/logic/testutil/gas_mocks.go -package testutil cosmossdk.io/store/types GasMeter @mockgen -destination x/logic/testutil/fs_mocks.go -package testutil io/fs FS @mockgen -destination x/logic/testutil/read_file_fs_mocks.go -package testutil io/fs ReadFileFS + @mockgen -source "$$(go list -f '{{.Dir}}' github.com/cosmos/cosmos-sdk/codec/types)/interface_registry.go" \ + -package testutil \ + -destination x/logic/testutil/interface_registry_mocks.go ## Release: .PHONY: release-assets diff --git a/x/logic/predicate/bank_test.go b/x/logic/predicate/bank_test.go index a7590703..668e0c3c 100644 --- a/x/logic/predicate/bank_test.go +++ b/x/logic/predicate/bank_test.go @@ -4,7 +4,6 @@ package predicate import ( "context" "fmt" - "strconv" "strings" "testing" @@ -26,10 +25,8 @@ import ( "cosmossdk.io/x/evidence" codecaddress "github.com/cosmos/cosmos-sdk/codec/address" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - query "github.com/cosmos/cosmos-sdk/types/query" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" bank "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -489,53 +486,7 @@ func TestBank(t *testing.T) { return authtypes.NewBaseAccountWithAddress(accAddr) }) - authQueryServiceKeeper. - EXPECT(). - Accounts(gomock.Any(), gomock.Any()). - AnyTimes(). - DoAndReturn(func(_ context.Context, req *authtypes.QueryAccountsRequest) (*authtypes.QueryAccountsResponse, error) { - start := 0 - limit := 5 - toCursor := func(idx int) []byte { return []byte(fmt.Sprintf("%d", idx)) } - fromCursor := func(k []byte) int { - idx, err := strconv.Atoi(string(k)) - c.So(err, ShouldBeNil) - - return idx - } - - if req.Pagination != nil { - if req.Pagination.Key != nil { - start = fromCursor(req.Pagination.Key) - } - if req.Pagination.Limit != 0 { - limit = int(req.Pagination.GetLimit()) - } - } - accounts := lo.Map( - lo.Slice(addresses, start, start+limit), - func(acc string, _ int) *codectypes.Any { - addr, err := sdk.AccAddressFromBech32(acc) - c.So(err, ShouldBeNil) - - accI := authtypes.ProtoBaseAccount() - err = accI.SetAddress(addr) - c.So(err, ShouldBeNil) - - anyV, err := codectypes.NewAnyWithValue(accI) - c.So(err, ShouldBeNil) - - return anyV - }) - - return &authtypes.QueryAccountsResponse{ - Accounts: accounts, - Pagination: &query.PageResponse{ - NextKey: toCursor(start + 1), - Total: 0, - }, - }, nil - }) + testutil.MockAuthQueryServiceWithAddresses(authQueryServiceKeeper, addresses) for _, balance := range tc.balances { bankKeeper. EXPECT(). @@ -750,7 +701,22 @@ func TestAccount(t *testing.T) { Variables: []string{}, Results: []types.Result{ { - Error: "error(resource_error(resource_context),account/1)", + Error: "error(resource_error(resource_context(authKeeper)),account/1)", + Substitutions: nil, + }, + }, + }, + }, + { + ctx: context.WithValue(context.Background(), types.AuthKeeperContextKey, testutil.NewMockAccountKeeper(ctrl)), + addresses: []string{}, + query: `account(dont_care).`, + wantAnswer: &types.Answer{ + HasMore: false, + Variables: []string{}, + Results: []types.Result{ + { + Error: "error(resource_error(resource_context(authQueryService)),account/1)", Substitutions: nil, }, }, @@ -812,57 +778,11 @@ func TestAccount(t *testing.T) { return authtypes.NewBaseAccountWithAddress(accAddr) }) - authQueryServiceKeeper. - EXPECT(). - Accounts(gomock.Any(), gomock.Any()). - AnyTimes(). - DoAndReturn(func(_ context.Context, req *authtypes.QueryAccountsRequest) (*authtypes.QueryAccountsResponse, error) { - if tc.authQueryServiceKeeperError { - return nil, status.Error(codes.PermissionDenied, "not allowed") - } - - start := 0 - limit := 5 - toCursor := func(idx int) []byte { return []byte(fmt.Sprintf("%d", idx)) } - fromCursor := func(k []byte) int { - idx, err := strconv.Atoi(string(k)) - c.So(err, ShouldBeNil) - - return idx - } - - if req.Pagination != nil { - if req.Pagination.Key != nil { - start = fromCursor(req.Pagination.Key) - } - if req.Pagination.Limit != 0 { - limit = int(req.Pagination.GetLimit()) - } - } - accounts := lo.Map( - lo.Slice(tc.addresses, start, start+limit), - func(acc string, _ int) *codectypes.Any { - addr, err := sdk.AccAddressFromBech32(acc) - c.So(err, ShouldBeNil) - - accI := authtypes.ProtoBaseAccount() - err = accI.SetAddress(addr) - c.So(err, ShouldBeNil) - - anyV, err := codectypes.NewAnyWithValue(accI) - c.So(err, ShouldBeNil) - - return anyV - }) - - return &authtypes.QueryAccountsResponse{ - Accounts: accounts, - Pagination: &query.PageResponse{ - NextKey: toCursor(start + 1), - Total: 0, - }, - }, nil - }) + if tc.authQueryServiceKeeperError { + testutil.MockAuthQueryServiceWithError(authQueryServiceKeeper, status.Error(codes.PermissionDenied, "not allowed")) + } else { + testutil.MockAuthQueryServiceWithAddresses(authQueryServiceKeeper, tc.addresses) + } Convey("and a vm with the account predicate registered", func() { interpreter := testutil.NewLightInterpreterMust(ctx) diff --git a/x/logic/predicate/util.go b/x/logic/predicate/util.go index 88e4b93b..70380fef 100644 --- a/x/logic/predicate/util.go +++ b/x/logic/predicate/util.go @@ -66,7 +66,7 @@ func Accounts(ctx context.Context, authQueryService types.AuthQueryService) func key = res.Pagination.NextKey - interfaceRegistry := ctx.Value(types.InterfaceRegistryContextKey).(cdctypes.InterfaceRegistry) + interfaceRegistry := ctx.Value(types.InterfaceRegistryContextKey).(cdctypes.AnyUnpacker) var account sdk.AccountI if err := interfaceRegistry.UnpackAny(res.Accounts[0], &account); err != nil { return lo.Tuple2[sdk.AccountI, error]{A: nil, B: err}, true diff --git a/x/logic/predicate/util_test.go b/x/logic/predicate/util_test.go new file mode 100644 index 00000000..3828a6cb --- /dev/null +++ b/x/logic/predicate/util_test.go @@ -0,0 +1,93 @@ +package predicate + +import ( + "errors" + "testing" + + dbm "github.com/cosmos/cosmos-db" + "github.com/golang/mock/gomock" + + . "github.com/smartystreets/goconvey/convey" + + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + + "cosmossdk.io/log" + "cosmossdk.io/store" + "cosmossdk.io/store/metrics" + "cosmossdk.io/x/evidence" + + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + + "github.com/axone-protocol/axoned/v10/x/logic/testutil" + "github.com/axone-protocol/axoned/v10/x/logic/types" +) + +func TestAccounts(t *testing.T) { + Convey("Given a mocked context and authQueryService", t, func() { + ctrl := gomock.NewController(t) + sdk.GetConfig().SetBech32PrefixForAccount("axone", "axonepub") + addresses := []string{ + "axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa", + "axone1wze8mn5nsgl9qrgazq6a92fvh7m5e6ps372aep", + } + authQueryServiceKeeper := testutil.NewMockAuthQueryService(ctrl) + testutil.MockAuthQueryServiceWithAddresses(authQueryServiceKeeper, addresses) + encCfg := moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{}) + + db := dbm.NewMemDB() + stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics()) + + Convey("Given a mocked context", func() { + ctx := sdk. + NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()). + WithValue(types.AuthQueryServiceContextKey, authQueryServiceKeeper). + WithValue(types.InterfaceRegistryContextKey, encCfg.InterfaceRegistry) + + Convey("When Accounts is called", func() { + next := Accounts(ctx, authQueryServiceKeeper) + + Convey("Then next() returns all the addresses", func() { + for i := 0; i < 2; i++ { + result, ok := next() + So(ok, ShouldBeTrue) + So(result.A.GetAddress().String(), ShouldEqual, addresses[i]) + So(result.B, ShouldBeNil) + } + for i := 0; i < 2; i++ { + result, ok := next() + So(ok, ShouldBeFalse) + So(result.A, ShouldBeNil) + So(result.B, ShouldBeNil) + } + }) + }) + }) + + Convey("Given a badly mocked context", func() { + interfaceRegistry := testutil.NewMockInterfaceRegistry(ctrl) + + ctx := sdk. + NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()). + WithValue(types.AuthQueryServiceContextKey, authQueryServiceKeeper). + WithValue(types.InterfaceRegistryContextKey, interfaceRegistry) + + interfaceRegistry. + EXPECT(). + UnpackAny(gomock.Any(), gomock.Any()). + AnyTimes(). + Return(errors.New("I was told not to unpack anything")) + + Convey("When Accounts is called", func() { + next := Accounts(ctx, authQueryServiceKeeper) + + Convey("Then next() returns an error", func() { + result, ok := next() + So(ok, ShouldBeTrue) + So(result.A, ShouldBeNil) + So(result.B, ShouldBeError, errors.New("I was told not to unpack anything")) + }) + }) + }) + }) +} diff --git a/x/logic/testutil/interface_registry_mocks.go b/x/logic/testutil/interface_registry_mocks.go new file mode 100644 index 00000000..9df40a55 --- /dev/null +++ b/x/logic/testutil/interface_registry_mocks.go @@ -0,0 +1,285 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: /Users/chris/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.50.9/codec/types/interface_registry.go + +// Package testutil is a generated GoMock package. +package testutil + +import ( + reflect "reflect" + + signing "cosmossdk.io/x/tx/signing" + types "github.com/cosmos/cosmos-sdk/codec/types" + proto "github.com/cosmos/gogoproto/proto" + gomock "github.com/golang/mock/gomock" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" +) + +// MockAnyUnpacker is a mock of AnyUnpacker interface. +type MockAnyUnpacker struct { + ctrl *gomock.Controller + recorder *MockAnyUnpackerMockRecorder +} + +// MockAnyUnpackerMockRecorder is the mock recorder for MockAnyUnpacker. +type MockAnyUnpackerMockRecorder struct { + mock *MockAnyUnpacker +} + +// NewMockAnyUnpacker creates a new mock instance. +func NewMockAnyUnpacker(ctrl *gomock.Controller) *MockAnyUnpacker { + mock := &MockAnyUnpacker{ctrl: ctrl} + mock.recorder = &MockAnyUnpackerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAnyUnpacker) EXPECT() *MockAnyUnpackerMockRecorder { + return m.recorder +} + +// UnpackAny mocks base method. +func (m *MockAnyUnpacker) UnpackAny(any *types.Any, iface interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UnpackAny", any, iface) + ret0, _ := ret[0].(error) + return ret0 +} + +// UnpackAny indicates an expected call of UnpackAny. +func (mr *MockAnyUnpackerMockRecorder) UnpackAny(any, iface interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnpackAny", reflect.TypeOf((*MockAnyUnpacker)(nil).UnpackAny), any, iface) +} + +// MockInterfaceRegistry is a mock of InterfaceRegistry interface. +type MockInterfaceRegistry struct { + ctrl *gomock.Controller + recorder *MockInterfaceRegistryMockRecorder +} + +// MockInterfaceRegistryMockRecorder is the mock recorder for MockInterfaceRegistry. +type MockInterfaceRegistryMockRecorder struct { + mock *MockInterfaceRegistry +} + +// NewMockInterfaceRegistry creates a new mock instance. +func NewMockInterfaceRegistry(ctrl *gomock.Controller) *MockInterfaceRegistry { + mock := &MockInterfaceRegistry{ctrl: ctrl} + mock.recorder = &MockInterfaceRegistryMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockInterfaceRegistry) EXPECT() *MockInterfaceRegistryMockRecorder { + return m.recorder +} + +// EnsureRegistered mocks base method. +func (m *MockInterfaceRegistry) EnsureRegistered(iface interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "EnsureRegistered", iface) + ret0, _ := ret[0].(error) + return ret0 +} + +// EnsureRegistered indicates an expected call of EnsureRegistered. +func (mr *MockInterfaceRegistryMockRecorder) EnsureRegistered(iface interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EnsureRegistered", reflect.TypeOf((*MockInterfaceRegistry)(nil).EnsureRegistered), iface) +} + +// FindDescriptorByName mocks base method. +func (m *MockInterfaceRegistry) FindDescriptorByName(arg0 protoreflect.FullName) (protoreflect.Descriptor, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindDescriptorByName", arg0) + ret0, _ := ret[0].(protoreflect.Descriptor) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FindDescriptorByName indicates an expected call of FindDescriptorByName. +func (mr *MockInterfaceRegistryMockRecorder) FindDescriptorByName(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindDescriptorByName", reflect.TypeOf((*MockInterfaceRegistry)(nil).FindDescriptorByName), arg0) +} + +// FindFileByPath mocks base method. +func (m *MockInterfaceRegistry) FindFileByPath(arg0 string) (protoreflect.FileDescriptor, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindFileByPath", arg0) + ret0, _ := ret[0].(protoreflect.FileDescriptor) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FindFileByPath indicates an expected call of FindFileByPath. +func (mr *MockInterfaceRegistryMockRecorder) FindFileByPath(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindFileByPath", reflect.TypeOf((*MockInterfaceRegistry)(nil).FindFileByPath), arg0) +} + +// ListAllInterfaces mocks base method. +func (m *MockInterfaceRegistry) ListAllInterfaces() []string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListAllInterfaces") + ret0, _ := ret[0].([]string) + return ret0 +} + +// ListAllInterfaces indicates an expected call of ListAllInterfaces. +func (mr *MockInterfaceRegistryMockRecorder) ListAllInterfaces() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListAllInterfaces", reflect.TypeOf((*MockInterfaceRegistry)(nil).ListAllInterfaces)) +} + +// ListImplementations mocks base method. +func (m *MockInterfaceRegistry) ListImplementations(ifaceTypeURL string) []string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListImplementations", ifaceTypeURL) + ret0, _ := ret[0].([]string) + return ret0 +} + +// ListImplementations indicates an expected call of ListImplementations. +func (mr *MockInterfaceRegistryMockRecorder) ListImplementations(ifaceTypeURL interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListImplementations", reflect.TypeOf((*MockInterfaceRegistry)(nil).ListImplementations), ifaceTypeURL) +} + +// RangeFiles mocks base method. +func (m *MockInterfaceRegistry) RangeFiles(f func(protoreflect.FileDescriptor) bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RangeFiles", f) +} + +// RangeFiles indicates an expected call of RangeFiles. +func (mr *MockInterfaceRegistryMockRecorder) RangeFiles(f interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RangeFiles", reflect.TypeOf((*MockInterfaceRegistry)(nil).RangeFiles), f) +} + +// RegisterImplementations mocks base method. +func (m *MockInterfaceRegistry) RegisterImplementations(iface interface{}, impls ...proto.Message) { + m.ctrl.T.Helper() + varargs := []interface{}{iface} + for _, a := range impls { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "RegisterImplementations", varargs...) +} + +// RegisterImplementations indicates an expected call of RegisterImplementations. +func (mr *MockInterfaceRegistryMockRecorder) RegisterImplementations(iface interface{}, impls ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{iface}, impls...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterImplementations", reflect.TypeOf((*MockInterfaceRegistry)(nil).RegisterImplementations), varargs...) +} + +// RegisterInterface mocks base method. +func (m *MockInterfaceRegistry) RegisterInterface(protoName string, iface interface{}, impls ...proto.Message) { + m.ctrl.T.Helper() + varargs := []interface{}{protoName, iface} + for _, a := range impls { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "RegisterInterface", varargs...) +} + +// RegisterInterface indicates an expected call of RegisterInterface. +func (mr *MockInterfaceRegistryMockRecorder) RegisterInterface(protoName, iface interface{}, impls ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{protoName, iface}, impls...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterInterface", reflect.TypeOf((*MockInterfaceRegistry)(nil).RegisterInterface), varargs...) +} + +// Resolve mocks base method. +func (m *MockInterfaceRegistry) Resolve(typeUrl string) (proto.Message, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Resolve", typeUrl) + ret0, _ := ret[0].(proto.Message) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Resolve indicates an expected call of Resolve. +func (mr *MockInterfaceRegistryMockRecorder) Resolve(typeUrl interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Resolve", reflect.TypeOf((*MockInterfaceRegistry)(nil).Resolve), typeUrl) +} + +// SigningContext mocks base method. +func (m *MockInterfaceRegistry) SigningContext() *signing.Context { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SigningContext") + ret0, _ := ret[0].(*signing.Context) + return ret0 +} + +// SigningContext indicates an expected call of SigningContext. +func (mr *MockInterfaceRegistryMockRecorder) SigningContext() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SigningContext", reflect.TypeOf((*MockInterfaceRegistry)(nil).SigningContext)) +} + +// UnpackAny mocks base method. +func (m *MockInterfaceRegistry) UnpackAny(any *types.Any, iface interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UnpackAny", any, iface) + ret0, _ := ret[0].(error) + return ret0 +} + +// UnpackAny indicates an expected call of UnpackAny. +func (mr *MockInterfaceRegistryMockRecorder) UnpackAny(any, iface interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnpackAny", reflect.TypeOf((*MockInterfaceRegistry)(nil).UnpackAny), any, iface) +} + +// mustEmbedInterfaceRegistry mocks base method. +func (m *MockInterfaceRegistry) mustEmbedInterfaceRegistry() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "mustEmbedInterfaceRegistry") +} + +// mustEmbedInterfaceRegistry indicates an expected call of mustEmbedInterfaceRegistry. +func (mr *MockInterfaceRegistryMockRecorder) mustEmbedInterfaceRegistry() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "mustEmbedInterfaceRegistry", reflect.TypeOf((*MockInterfaceRegistry)(nil).mustEmbedInterfaceRegistry)) +} + +// MockUnpackInterfacesMessage is a mock of UnpackInterfacesMessage interface. +type MockUnpackInterfacesMessage struct { + ctrl *gomock.Controller + recorder *MockUnpackInterfacesMessageMockRecorder +} + +// MockUnpackInterfacesMessageMockRecorder is the mock recorder for MockUnpackInterfacesMessage. +type MockUnpackInterfacesMessageMockRecorder struct { + mock *MockUnpackInterfacesMessage +} + +// NewMockUnpackInterfacesMessage creates a new mock instance. +func NewMockUnpackInterfacesMessage(ctrl *gomock.Controller) *MockUnpackInterfacesMessage { + mock := &MockUnpackInterfacesMessage{ctrl: ctrl} + mock.recorder = &MockUnpackInterfacesMessageMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUnpackInterfacesMessage) EXPECT() *MockUnpackInterfacesMessageMockRecorder { + return m.recorder +} + +// UnpackInterfaces mocks base method. +func (m *MockUnpackInterfacesMessage) UnpackInterfaces(unpacker types.AnyUnpacker) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UnpackInterfaces", unpacker) + ret0, _ := ret[0].(error) + return ret0 +} + +// UnpackInterfaces indicates an expected call of UnpackInterfaces. +func (mr *MockUnpackInterfacesMessageMockRecorder) UnpackInterfaces(unpacker interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnpackInterfaces", reflect.TypeOf((*MockUnpackInterfacesMessage)(nil).UnpackInterfaces), unpacker) +} diff --git a/x/logic/testutil/keeper_mocks.go b/x/logic/testutil/keeper_mocks.go new file mode 100644 index 00000000..210aa8bc --- /dev/null +++ b/x/logic/testutil/keeper_mocks.go @@ -0,0 +1,83 @@ +package testutil + +import ( + context "context" + "fmt" + "strconv" + + "github.com/golang/mock/gomock" + "github.com/samber/lo" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +func MockAuthQueryServiceWithAddresses(mock *MockAuthQueryService, addresses []string) { + mock. + EXPECT(). + Accounts(gomock.Any(), gomock.Any()). + AnyTimes(). + DoAndReturn(func(_ context.Context, req *authtypes.QueryAccountsRequest) (*authtypes.QueryAccountsResponse, error) { + start := 0 + limit := 5 + toCursor := func(idx int) []byte { return []byte(fmt.Sprintf("%d", idx)) } + fromCursor := func(k []byte) int { + idx, err := strconv.Atoi(string(k)) + if err != nil { + panic(err) + } + + return idx + } + + if req.Pagination != nil { + if req.Pagination.Key != nil { + start = fromCursor(req.Pagination.Key) + } + if req.Pagination.Limit != 0 { + limit = int(req.Pagination.GetLimit()) + } + } + accounts := lo.Map( + lo.Slice(addresses, start, start+limit), + func(acc string, _ int) *codectypes.Any { + addr, err := sdk.AccAddressFromBech32(acc) + if err != nil { + panic(err) + } + + accI := authtypes.ProtoBaseAccount() + err = accI.SetAddress(addr) + if err != nil { + panic(err) + } + + anyV, err := codectypes.NewAnyWithValue(accI) + if err != nil { + panic(err) + } + + return anyV + }) + + return &authtypes.QueryAccountsResponse{ + Accounts: accounts, + Pagination: &query.PageResponse{ + NextKey: toCursor(start + 1), + Total: 0, + }, + }, nil + }) +} + +func MockAuthQueryServiceWithError(mock *MockAuthQueryService, err error) { + mock. + EXPECT(). + Accounts(gomock.Any(), gomock.Any()). + AnyTimes(). + DoAndReturn(func(_ context.Context, _ *authtypes.QueryAccountsRequest) (*authtypes.QueryAccountsResponse, error) { + return nil, err + }) +}