Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/gov: fix NormalizeProposalType() return values (bp #8808) #8816

Merged
merged 6 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions x/gov/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ func (s *IntegrationTestSuite) TestCmdGetProposals() {
},
false,
},
{
"get proposals with invalid status",
[]string{
"--status=unknown",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
},
}

for _, tc := range testCases {
Expand Down
22 changes: 18 additions & 4 deletions x/gov/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,33 @@ func (s *IntegrationTestSuite) TestGetProposalsGRPC() {
val := s.network.Validators[0]

testCases := []struct {
name string
url string
headers map[string]string
expErr bool
name string
url string
headers map[string]string
wantNumProposals int
expErr bool
}{
{
"get proposals with height 1",
fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals", val.APIAddress),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
0,
true,
},
{
"valid request",
fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals", val.APIAddress),
map[string]string{},
3,
false,
},
{
"valid request with filter by status",
fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals?proposal_status=1", val.APIAddress),
map[string]string{},
1,
false,
},
}
Expand All @@ -141,7 +151,11 @@ func (s *IntegrationTestSuite) TestGetProposalsGRPC() {
s.Require().Empty(proposals.Proposals)
} else {
s.Require().NoError(err)
<<<<<<< HEAD
s.Require().Len(proposals.Proposals, 2)
=======
s.Require().Len(proposals.Proposals, tc.wantNumProposals)
>>>>>>> be23295bd... x/gov: fix NormalizeProposalType() return values (#8808)
}
})
}
Expand Down
56 changes: 56 additions & 0 deletions x/gov/client/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,62 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov/types"
)

func (s *IntegrationTestSuite) TestLegacyGetAllProposals() {
val := s.network.Validators[0]

testCases := []struct {
name string
url string
numProposals int
expErr bool
expErrMsg string
}{
{
"get all existing proposals",
fmt.Sprintf("%s/gov/proposals", val.APIAddress),
3, false, "",
},
{
"get proposals in deposit period",
fmt.Sprintf("%s/gov/proposals?status=deposit_period", val.APIAddress),
1, false, "",
},
{
"get proposals in voting period",
fmt.Sprintf("%s/gov/proposals?status=voting_period", val.APIAddress),
2, false, "",
},
{
"wrong status parameter",
fmt.Sprintf("%s/gov/proposals?status=invalidstatus", val.APIAddress),
0, true, "'invalidstatus' is not a valid proposal status",
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
respJSON, err := rest.GetRequest(tc.url)
s.Require().NoError(err)

if tc.expErr {
var errResp rest.ErrorResponse
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(respJSON, &errResp))
s.Require().Equal(errResp.Error, tc.expErrMsg)
} else {
var resp = rest.ResponseWithHeight{}
err = val.ClientCtx.LegacyAmino.UnmarshalJSON(respJSON, &resp)
s.Require().NoError(err)

// Check results.
var proposals types.Proposals
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(resp.Result, &proposals))
s.Require().Equal(tc.numProposals, len(proposals))
}
})
}
}

func (s *IntegrationTestSuite) TestLegacyGetVote() {
val := s.network.Validators[0]
voterAddressBech32 := val.Address.String()
Expand Down
8 changes: 4 additions & 4 deletions x/gov/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func NormalizeProposalType(proposalType string) string {
func NormalizeProposalStatus(status string) string {
switch status {
case "DepositPeriod", "deposit_period":
return "DepositPeriod"
return types.StatusDepositPeriod.String()
case "VotingPeriod", "voting_period":
return "VotingPeriod"
return types.StatusVotingPeriod.String()
case "Passed", "passed":
return "Passed"
return types.StatusPassed.String()
case "Rejected", "rejected":
return "Rejected"
return types.StatusRejected.String()
default:
return status
}
Expand Down
84 changes: 84 additions & 0 deletions x/gov/client/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package utils_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/x/gov/client/utils"
)

func TestNormalizeWeightedVoteOptions(t *testing.T) {
cases := map[string]struct {
options string
normalized string
}{
"simple Yes": {
options: "Yes",
normalized: "VOTE_OPTION_YES=1",
},
"simple yes": {
options: "yes",
normalized: "VOTE_OPTION_YES=1",
},
"formal yes": {
options: "yes=1",
normalized: "VOTE_OPTION_YES=1",
},
"half yes half no": {
options: "yes=0.5,no=0.5",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.5",
},
"3 options": {
options: "Yes=0.5,No=0.4,NoWithVeto=0.1",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.4,VOTE_OPTION_NO_WITH_VETO=0.1",
},
"zero weight option": {
options: "Yes=0.5,No=0.5,NoWithVeto=0",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.5,VOTE_OPTION_NO_WITH_VETO=0",
},
"minus weight option": {
options: "Yes=0.5,No=0.6,NoWithVeto=-0.1",
normalized: "VOTE_OPTION_YES=0.5,VOTE_OPTION_NO=0.6,VOTE_OPTION_NO_WITH_VETO=-0.1",
},
"empty options": {
options: "",
normalized: "=1",
},
"not available option": {
options: "Yessss=1",
normalized: "Yessss=1",
},
}

for _, tc := range cases {
normalized := utils.NormalizeWeightedVoteOptions(tc.options)
require.Equal(t, normalized, tc.normalized)
}
}

func TestNormalizeProposalStatus(t *testing.T) {
type args struct {
status string
}
tests := []struct {
name string
args args
want string
}{
{"invalid", args{"unknown"}, "unknown"},
{"deposit_period", args{"deposit_period"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"},
{"DepositPeriod", args{"DepositPeriod"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"},
{"voting_period", args{"deposit_period"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"},
{"VotingPeriod", args{"DepositPeriod"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"},
{"passed", args{"passed"}, "PROPOSAL_STATUS_PASSED"},
{"Passed", args{"Passed"}, "PROPOSAL_STATUS_PASSED"},
{"Rejected", args{"Rejected"}, "PROPOSAL_STATUS_REJECTED"},
{"rejected", args{"rejected"}, "PROPOSAL_STATUS_REJECTED"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, utils.NormalizeProposalStatus(tt.args.status))
})
}
}