code.vegaprotocol.io/vega@v0.79.0/commands/vote_submission_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands_test
    17  
    18  import (
    19  	"errors"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/commands"
    23  	types "code.vegaprotocol.io/vega/protos/vega"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestSubmittingNilVoteFails(t *testing.T) {
    30  	err := checkVoteSubmission(nil)
    31  
    32  	assert.Contains(t, err.Get("vote_submission"), commands.ErrIsRequired)
    33  }
    34  
    35  func TestVoteSubmission(t *testing.T) {
    36  	cases := []struct {
    37  		vote      commandspb.VoteSubmission
    38  		errString string
    39  	}{
    40  		{
    41  			vote: commandspb.VoteSubmission{
    42  				Value:      types.Vote_VALUE_YES,
    43  				ProposalId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    44  			},
    45  		},
    46  		{
    47  			vote: commandspb.VoteSubmission{
    48  				ProposalId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    49  			},
    50  			errString: "vote_submission.value (is required)",
    51  		},
    52  		{
    53  			vote: commandspb.VoteSubmission{
    54  				Value:      types.Vote_Value(-42),
    55  				ProposalId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    56  			},
    57  			errString: "vote_submission.value (is not a valid value)",
    58  		},
    59  		{
    60  			vote: commandspb.VoteSubmission{
    61  				Value: types.Vote_VALUE_NO,
    62  			},
    63  			errString: "vote_submission.proposal_id (is required)",
    64  		},
    65  		{
    66  			vote:      commandspb.VoteSubmission{},
    67  			errString: "vote_submission.proposal_id (is required), vote_submission.value (is required)",
    68  		},
    69  	}
    70  
    71  	for _, c := range cases {
    72  		err := commands.CheckVoteSubmission(&c.vote)
    73  		if len(c.errString) <= 0 {
    74  			assert.NoError(t, err)
    75  			continue
    76  		}
    77  		assert.Error(t, err)
    78  		assert.EqualError(t, err, c.errString)
    79  	}
    80  }
    81  
    82  func checkVoteSubmission(cmd *commandspb.VoteSubmission) commands.Errors {
    83  	err := commands.CheckVoteSubmission(cmd)
    84  
    85  	var e commands.Errors
    86  	if ok := errors.As(err, &e); !ok {
    87  		return commands.NewErrors()
    88  	}
    89  
    90  	return e
    91  }