code.vegaprotocol.io/vega@v0.79.0/commands/liquidity_provision_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  	"fmt"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/commands"
    23  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestNilLiquidityProvisionSubmissionFails(t *testing.T) {
    29  	err := commands.CheckLiquidityProvisionSubmission(nil)
    30  
    31  	assert.Error(t, err)
    32  }
    33  
    34  func TestLiquidityProvisionSubmission(t *testing.T) {
    35  	cases := []struct {
    36  		lp        commandspb.LiquidityProvisionSubmission
    37  		errString string
    38  	}{
    39  		{
    40  			// this is a valid cancellation.
    41  			lp: commandspb.LiquidityProvisionSubmission{
    42  				CommitmentAmount: "0",
    43  				MarketId:         "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    44  			},
    45  			errString: "liquidity_provision_submission.commitment_amount (is not a valid number)",
    46  		},
    47  		{
    48  			lp: commandspb.LiquidityProvisionSubmission{
    49  				CommitmentAmount: "100",
    50  				Fee:              "abcd",
    51  				MarketId:         "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    52  			},
    53  			errString: "liquidity_provision_submission.fee (is not a valid value)",
    54  		},
    55  		{
    56  			lp: commandspb.LiquidityProvisionSubmission{
    57  				CommitmentAmount: "100",
    58  				Fee:              "-1",
    59  				MarketId:         "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    60  			},
    61  			errString: "liquidity_provision_submission.fee (must be positive)",
    62  		},
    63  		{
    64  			lp: commandspb.LiquidityProvisionSubmission{
    65  				CommitmentAmount: "100",
    66  				Fee:              "0.1",
    67  			},
    68  			errString: "liquidity_provision_submission.market_id (is required)",
    69  		},
    70  
    71  		{
    72  			lp: commandspb.LiquidityProvisionSubmission{
    73  				CommitmentAmount: "100",
    74  				Fee:              "0.1",
    75  				MarketId:         "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
    76  			},
    77  		},
    78  	}
    79  
    80  	for i, c := range cases {
    81  		err := commands.CheckLiquidityProvisionSubmission(&c.lp)
    82  		if len(c.errString) <= 0 {
    83  			assert.NoErrorf(t, err, "unexpected error on position: %d", i)
    84  			continue
    85  		}
    86  
    87  		assert.Errorf(t, err, "expected error on position: %d", i)
    88  		assert.EqualErrorf(t, err, c.errString, "expected error to match on position: %d", i)
    89  	}
    90  }
    91  
    92  func TestCheckLiquidityProvisionCancellation(t *testing.T) {
    93  	type args struct {
    94  		cmd *commandspb.LiquidityProvisionCancellation
    95  	}
    96  	tests := []struct {
    97  		name      string
    98  		args      args
    99  		wantErr   assert.ErrorAssertionFunc
   100  		errString string
   101  	}{
   102  		{
   103  			name: "Should return an error if request is nil",
   104  			args: args{
   105  				cmd: nil,
   106  			},
   107  			wantErr:   assert.Error,
   108  			errString: "liquidity_provision_cancellation (is required)",
   109  		},
   110  		{
   111  			name: "Should return an error if market_id is not provided",
   112  			args: args{
   113  				cmd: &commandspb.LiquidityProvisionCancellation{
   114  					MarketId: "",
   115  				},
   116  			},
   117  			wantErr:   assert.Error,
   118  			errString: "liquidity_provision_cancellation.market_id (is required)",
   119  		},
   120  		{
   121  			name: "Should succeed if market id is provided",
   122  			args: args{
   123  				cmd: &commandspb.LiquidityProvisionCancellation{
   124  					MarketId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
   125  				},
   126  			},
   127  			wantErr: assert.NoError,
   128  		},
   129  	}
   130  	for _, tt := range tests {
   131  		t.Run(tt.name, func(t *testing.T) {
   132  			gotErr := commands.CheckLiquidityProvisionCancellation(tt.args.cmd)
   133  			tt.wantErr(t, gotErr, fmt.Sprintf("CheckLiquidityProvisionCancellation(%v)", tt.args.cmd))
   134  			if tt.errString != "" {
   135  				assert.EqualError(t, gotErr, tt.errString)
   136  			}
   137  		})
   138  	}
   139  }
   140  
   141  func TestCheckLiquidityProvisionAmendment(t *testing.T) {
   142  	type args struct {
   143  		cmd *commandspb.LiquidityProvisionAmendment
   144  	}
   145  	tests := []struct {
   146  		name      string
   147  		args      args
   148  		wantErr   assert.ErrorAssertionFunc
   149  		errString string
   150  	}{
   151  		{
   152  			name: "Should return an error when the command is nil",
   153  			args: args{
   154  				cmd: nil,
   155  			},
   156  			wantErr:   assert.Error,
   157  			errString: "liquidity_provision_amendment (is required)",
   158  		},
   159  		{
   160  			name: "Should return an error when market_id is not provided",
   161  			args: args{
   162  				cmd: &commandspb.LiquidityProvisionAmendment{
   163  					MarketId: "",
   164  				},
   165  			},
   166  			wantErr:   assert.Error,
   167  			errString: "liquidity_provision_amendment.market_id (is required)",
   168  		},
   169  		{
   170  			name: "Should return an error if amendment changes nothing",
   171  			args: args{
   172  				cmd: &commandspb.LiquidityProvisionAmendment{
   173  					MarketId: "abcd",
   174  				},
   175  			},
   176  			wantErr:   assert.Error,
   177  			errString: "liquidity_provision_amendment (is required)",
   178  		},
   179  	}
   180  
   181  	for _, tt := range tests {
   182  		t.Run(tt.name, func(t *testing.T) {
   183  			gotErr := commands.CheckLiquidityProvisionAmendment(tt.args.cmd)
   184  			tt.wantErr(t, gotErr, fmt.Sprintf("CheckLiquidityProvisionAmendment(%v)", tt.args.cmd))
   185  
   186  			if tt.errString != "" {
   187  				assert.EqualError(t, gotErr, tt.errString)
   188  			}
   189  		})
   190  	}
   191  }