Skip to content

Commit

Permalink
Add test for nil address and add tests for TestIsGroupAddress.
Browse files Browse the repository at this point in the history
  • Loading branch information
Taztingo committed Jan 25, 2024
1 parent e458581 commit 85b0157
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
64 changes: 61 additions & 3 deletions app/group_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 85b0157

Please sign in to comment.