Skip to content

Commit

Permalink
feat(group): add units tests for msgs (cosmos#10920)
Browse files Browse the repository at this point in the history
## Description

Closes: cosmos#10905 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
aleem1314 authored Jan 11, 2022
1 parent ddf5639 commit 2e30929
Show file tree
Hide file tree
Showing 3 changed files with 803 additions and 18 deletions.
2 changes: 1 addition & 1 deletion x/group/client/testutil/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (s *IntegrationTestSuite) TestTxCreateGroup() {
commonFlags...,
),
true,
"message validation failed: members: address: empty address string is not allowed",
"message validation failed: address: empty address string is not allowed",
nil,
0,
},
Expand Down
49 changes: 32 additions & 17 deletions x/group/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
const (
TypeMsgCreateGroup = "create_group"
TypeMsgUpdateGroupAdmin = "update_group_admin"
TypeMsgUpdateGroupComment = "update_group_comment"
TypeMsgUpdateGroupMetadata = "update_group_metadata"
TypeMsgUpdateGroupMembers = "update_group_members"
TypeMsgCreateGroupPolicy = "create_group_policy"
TypeMsgUpdateGroupPolicyAdmin = "update_group_policy_admin"
TypeMsgUpdateGroupPolicyDecisionPolicy = "update_group_policy_decision_policy"
TypeMsgUpdateGroupPolicyComment = "update_group_policy_comment"
TypeMsgUpdateGroupPolicyMetadata = "update_group_policy_metadata"
TypeMsgCreateProposal = "create_proposal"
TypeMsgVote = "vote"
TypeMsgExec = "exec"
Expand Down Expand Up @@ -57,16 +57,29 @@ func (m MsgCreateGroup) ValidateBasic() error {
return sdkerrors.Wrap(err, "admin")
}

members := Members{Members: m.Members}
if err := members.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "members")
}
return m.validateMembers()
}

func (m MsgCreateGroup) validateMembers() error {
index := make(map[string]struct{}, len(m.Members))
for i := range m.Members {
member := m.Members[i]
_, err := sdk.AccAddressFromBech32(member.Address)
if err != nil {
return sdkerrors.Wrap(err, "address")
}

if _, err := math.NewPositiveDecFromString(member.Weight); err != nil {
return sdkerrors.Wrap(err, "member weight")
return sdkerrors.Wrap(err, "weight")
}

addr := member.Address
if _, exists := index[addr]; exists {
return sdkerrors.Wrapf(errors.ErrDuplicate, "address: %s", addr)
}
index[addr] = struct{}{}
}

return nil
}

Expand All @@ -75,6 +88,7 @@ func (m Member) ValidateBasic() error {
if err != nil {
return sdkerrors.Wrap(err, "address")
}

if _, err := math.NewNonNegativeDecFromString(m.Weight); err != nil {
return sdkerrors.Wrap(err, "weight")
}
Expand Down Expand Up @@ -109,7 +123,7 @@ func (m MsgUpdateGroupAdmin) GetSigners() []sdk.AccAddress {
// ValidateBasic does a sanity check on the provided data
func (m MsgUpdateGroupAdmin) ValidateBasic() error {
if m.GroupId == 0 {
return sdkerrors.Wrap(errors.ErrEmpty, "group")
return sdkerrors.Wrap(errors.ErrEmpty, "group id")
}

admin, err := sdk.AccAddressFromBech32(m.Admin)
Expand Down Expand Up @@ -140,7 +154,7 @@ func (m MsgUpdateGroupMetadata) Route() string {
}

// Type Implements Msg.
func (m MsgUpdateGroupMetadata) Type() string { return TypeMsgUpdateGroupComment }
func (m MsgUpdateGroupMetadata) Type() string { return TypeMsgUpdateGroupMetadata }

// GetSignBytes Implements Msg.
func (m MsgUpdateGroupMetadata) GetSignBytes() []byte {
Expand All @@ -159,13 +173,14 @@ func (m MsgUpdateGroupMetadata) GetSigners() []sdk.AccAddress {
// ValidateBasic does a sanity check on the provided data
func (m MsgUpdateGroupMetadata) ValidateBasic() error {
if m.GroupId == 0 {
return sdkerrors.Wrap(errors.ErrEmpty, "group")
return sdkerrors.Wrap(errors.ErrEmpty, "group id")

}
_, err := sdk.AccAddressFromBech32(m.Admin)
if err != nil {
return sdkerrors.Wrap(err, "admin")
}

return nil
}

Expand Down Expand Up @@ -202,7 +217,7 @@ func (m MsgUpdateGroupMembers) GetSigners() []sdk.AccAddress {
// ValidateBasic does a sanity check on the provided data
func (m MsgUpdateGroupMembers) ValidateBasic() error {
if m.GroupId == 0 {
return sdkerrors.Wrap(errors.ErrEmpty, "group")
return sdkerrors.Wrap(errors.ErrEmpty, "group id")

}
_, err := sdk.AccAddressFromBech32(m.Admin)
Expand Down Expand Up @@ -255,7 +270,7 @@ func (m MsgCreateGroupPolicy) ValidateBasic() error {
return sdkerrors.Wrap(err, "admin")
}
if m.GroupId == 0 {
return sdkerrors.Wrap(errors.ErrEmpty, "group")
return sdkerrors.Wrap(errors.ErrEmpty, "group id")
}

policy := m.GetDecisionPolicy()
Expand Down Expand Up @@ -311,7 +326,7 @@ func (m MsgUpdateGroupPolicyAdmin) ValidateBasic() error {
}

if admin.Equals(newAdmin) {
return sdkerrors.Wrap(errors.ErrInvalid, "new and old admin are the same")
return sdkerrors.Wrap(errors.ErrInvalid, "new and old admin are same")
}
return nil
}
Expand All @@ -334,7 +349,7 @@ func NewMsgUpdateGroupPolicyDecisionPolicyRequest(admin sdk.AccAddress, address
func (m *MsgUpdateGroupPolicyDecisionPolicy) SetDecisionPolicy(decisionPolicy DecisionPolicy) error {
msg, ok := decisionPolicy.(proto.Message)
if !ok {
return fmt.Errorf("can't proto marshal %T", msg)
return sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
Expand Down Expand Up @@ -414,7 +429,7 @@ func (m MsgUpdateGroupPolicyMetadata) Route() string {
}

// Type Implements Msg.
func (m MsgUpdateGroupPolicyMetadata) Type() string { return TypeMsgUpdateGroupPolicyComment }
func (m MsgUpdateGroupPolicyMetadata) Type() string { return TypeMsgUpdateGroupPolicyMetadata }

// GetSignBytes Implements Msg.
func (m MsgUpdateGroupPolicyMetadata) GetSignBytes() []byte {
Expand Down Expand Up @@ -630,7 +645,7 @@ func (m MsgVote) ValidateBasic() error {
return sdkerrors.Wrap(err, "voter")
}
if m.ProposalId == 0 {
return sdkerrors.Wrap(errors.ErrEmpty, "proposal")
return sdkerrors.Wrap(errors.ErrEmpty, "proposal id")
}
if m.Choice == Choice_CHOICE_UNSPECIFIED {
return sdkerrors.Wrap(errors.ErrEmpty, "choice")
Expand Down Expand Up @@ -672,7 +687,7 @@ func (m MsgExec) ValidateBasic() error {
return sdkerrors.Wrap(err, "signer")
}
if m.ProposalId == 0 {
return sdkerrors.Wrap(errors.ErrEmpty, "proposal")
return sdkerrors.Wrap(errors.ErrEmpty, "proposal id")
}
return nil
}
Loading

0 comments on commit 2e30929

Please sign in to comment.