code.vegaprotocol.io/vega@v0.79.0/commands/state_var_proposal_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  	vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
    24  	"code.vegaprotocol.io/vega/protos/vega"
    25  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestNilStateVarProposalFundsFails(t *testing.T) {
    31  	err := checkStateVarProposal(nil)
    32  	assert.Contains(t, err.Get("state_variable_proposal"), commands.ErrIsRequired)
    33  }
    34  
    35  func TestStateVarProposals(t *testing.T) {
    36  	cases := []struct {
    37  		stateVar  commandspb.StateVariableProposal
    38  		errString string
    39  	}{
    40  		{
    41  			stateVar: commandspb.StateVariableProposal{
    42  				Proposal: &vega.StateValueProposal{
    43  					StateVarId: vgcrypto.RandomHash(),
    44  					EventId:    "",
    45  					Kvb: []*vega.KeyValueBundle{
    46  						{
    47  							Key:       vgcrypto.RandomHash(),
    48  							Tolerance: "11000",
    49  							Value:     &vega.StateVarValue{},
    50  						},
    51  					},
    52  				},
    53  			},
    54  			errString: "state_variable_proposal.event_id (is required)",
    55  		},
    56  		{
    57  			stateVar: commandspb.StateVariableProposal{
    58  				Proposal: &vega.StateValueProposal{
    59  					StateVarId: "",
    60  					EventId:    vgcrypto.RandomHash(),
    61  					Kvb: []*vega.KeyValueBundle{
    62  						{
    63  							Key:       vgcrypto.RandomHash(),
    64  							Tolerance: "11000",
    65  							Value:     &vega.StateVarValue{},
    66  						},
    67  					},
    68  				},
    69  			},
    70  			errString: "state_variable_proposal.state_var_id (is required)",
    71  		},
    72  		{
    73  			stateVar: commandspb.StateVariableProposal{
    74  				Proposal: &vega.StateValueProposal{
    75  					StateVarId: "",
    76  					EventId:    vgcrypto.RandomHash(),
    77  					Kvb: []*vega.KeyValueBundle{
    78  						{
    79  							Key:       vgcrypto.RandomHash(),
    80  							Tolerance: "11000",
    81  							Value:     nil,
    82  						},
    83  					},
    84  				},
    85  			},
    86  			errString: "state_variable_proposal.key_value_bundle.0.value (is required)",
    87  		},
    88  		{
    89  			stateVar: commandspb.StateVariableProposal{
    90  				Proposal: &vega.StateValueProposal{
    91  					StateVarId: vgcrypto.RandomHash(),
    92  					EventId:    vgcrypto.RandomHash(),
    93  					Kvb: []*vega.KeyValueBundle{
    94  						{
    95  							Key:       vgcrypto.RandomHash(),
    96  							Tolerance: "11000",
    97  							Value:     &vega.StateVarValue{},
    98  						},
    99  					},
   100  				},
   101  			},
   102  			errString: "",
   103  		},
   104  	}
   105  
   106  	for _, c := range cases {
   107  		err := commands.CheckStateVariableProposal(&c.stateVar)
   108  		if len(c.errString) <= 0 {
   109  			assert.NoError(t, err)
   110  			continue
   111  		}
   112  		assert.Contains(t, err.Error(), c.errString)
   113  	}
   114  }
   115  
   116  func checkStateVarProposal(cmd *commandspb.StateVariableProposal) commands.Errors {
   117  	err := commands.CheckStateVariableProposal(cmd)
   118  
   119  	var e commands.Errors
   120  	if ok := errors.As(err, &e); !ok {
   121  		return commands.NewErrors()
   122  	}
   123  
   124  	return e
   125  }