From 85b0157d1c961c86d1b39c5a1d7f4abed4023225 Mon Sep 17 00:00:00 2001 From: Matthew Witkowski Date: Thu, 25 Jan 2024 13:49:28 -0500 Subject: [PATCH] Add test for nil address and add tests for TestIsGroupAddress. --- app/group.go | 3 +++ app/group_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/group.go b/app/group.go index b6b0b230c6..691d71797c 100644 --- a/app/group.go +++ b/app/group.go @@ -17,6 +17,9 @@ type GroupPolicyQuerier interface { // IsGroupAddress checks if the account is a group address. func (t GroupCheckerFunc) IsGroupAddress(ctx sdk.Context, account sdk.AccAddress) bool { + if account == nil { + return false + } return t(ctx, account) } diff --git a/app/group_test.go b/app/group_test.go index 4138beab49..819789b2cd 100644 --- a/app/group_test.go +++ b/app/group_test.go @@ -1,14 +1,72 @@ package app -import "testing" +import ( + "context" + "testing" -type MockGroupPolicyQuerier struct { -} + "github.com/stretchr/testify/assert" + + cerrs "cosmossdk.io/errors" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/group" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" +) func TestNewGroupCheckerFunc(t *testing.T) { } func TestIsGroupAddress(t *testing.T) { + tests := []struct { + name string + querySuccess bool + address sdk.AccAddress + }{ + { + name: "should be true with group address", + querySuccess: true, + address: sdk.AccAddress("test"), + }, + { + name: "should return false with non group address", + querySuccess: false, + address: sdk.AccAddress("test"), + }, + { + name: "should return false with nil address", + querySuccess: false, + address: nil, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + querier := NewMockGroupPolicyQuerier(tc.querySuccess) + checker := NewGroupCheckerFunc(querier) + ctx := sdk.NewContext(nil, tmproto.Header{}, true, nil) + success := checker.IsGroupAddress(ctx, tc.address) + assert.Equal(t, tc.querySuccess, success, "should correctly detect if the supplied address is a group address") + }) + } +} + +// MockGroupPolicyQuerier mocks the querier so a GroupKeeper isn't needed. +type MockGroupPolicyQuerier struct { + isGroupAddress bool +} + +// NewMockGroupPolicyQuerier creates a new MockGroupPolicyQuerier. +func NewMockGroupPolicyQuerier(isGroupAddress bool) *MockGroupPolicyQuerier { + return &MockGroupPolicyQuerier{ + isGroupAddress: isGroupAddress, + } +} +// GroupPolicyInfo provides a stubbed implementation of the GroupPolicyInfo method. +func (t MockGroupPolicyQuerier) GroupPolicyInfo(goCtx context.Context, request *group.QueryGroupPolicyInfoRequest) (*group.QueryGroupPolicyInfoResponse, error) { + var err error + if !t.isGroupAddress { + err = cerrs.New("", 1, "") + } + return nil, err }