github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/types/genesis_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    10  )
    11  
    12  func TestValidateGenesis(t *testing.T) {
    13  	testCases := []struct {
    14  		name      string
    15  		configure func(*types.GenesisState)
    16  		errString string
    17  	}{
    18  		{
    19  			"InvalidParams",
    20  			func(genState *types.GenesisState) {
    21  				params := types.DefaultParams()
    22  				params.SwapFeeRate = sdk.NewDec(-1)
    23  				genState.Params = params
    24  			},
    25  			"swap fee rate must not be negative: -1.000000000000000000",
    26  		},
    27  		{
    28  			"InvalidPoolRecords",
    29  			func(genState *types.GenesisState) {
    30  				genState.PoolRecords = []types.PoolRecord{{}}
    31  			},
    32  			"bad msg index of the batch",
    33  		},
    34  	}
    35  	for _, tc := range testCases {
    36  		t.Run(tc.name, func(t *testing.T) {
    37  			genState := types.DefaultGenesisState()
    38  			tc.configure(genState)
    39  			err := types.ValidateGenesis(*genState)
    40  			require.EqualError(t, err, tc.errString)
    41  		})
    42  	}
    43  }
    44  
    45  func TestPoolRecord_Validate(t *testing.T) {
    46  	testCases := []struct {
    47  		name       string
    48  		poolRecord types.PoolRecord
    49  		shouldFail bool
    50  	}{
    51  		{
    52  			"ValidPoolRecord",
    53  			types.PoolRecord{
    54  				PoolBatch: types.PoolBatch{
    55  					DepositMsgIndex:  1,
    56  					WithdrawMsgIndex: 1,
    57  					SwapMsgIndex:     1,
    58  				},
    59  				DepositMsgStates:  nil,
    60  				WithdrawMsgStates: nil,
    61  				SwapMsgStates:     nil,
    62  			},
    63  			false,
    64  		},
    65  		{
    66  			"InvalidPoolBatchDepositMsgIndex",
    67  			types.PoolRecord{
    68  				PoolBatch: types.PoolBatch{
    69  					DepositMsgIndex:  0,
    70  					WithdrawMsgIndex: 1,
    71  					SwapMsgIndex:     1,
    72  				},
    73  			},
    74  			true,
    75  		},
    76  		{
    77  			"InvalidPoolBatchWithdrawMsgIndex",
    78  			types.PoolRecord{
    79  				PoolBatch: types.PoolBatch{
    80  					DepositMsgIndex:  0,
    81  					WithdrawMsgIndex: 1,
    82  					SwapMsgIndex:     0,
    83  				},
    84  			},
    85  			true,
    86  		},
    87  		{
    88  			"InvalidPoolBatchSwapMsgIndex",
    89  			types.PoolRecord{
    90  				PoolBatch: types.PoolBatch{
    91  					DepositMsgIndex:  1,
    92  					WithdrawMsgIndex: 1,
    93  					SwapMsgIndex:     0,
    94  				},
    95  			},
    96  			true,
    97  		},
    98  		{
    99  			"MismatchingPoolBatchDepositMsgIndex",
   100  			types.PoolRecord{
   101  				PoolBatch: types.PoolBatch{
   102  					DepositMsgIndex:  10,
   103  					WithdrawMsgIndex: 1,
   104  					SwapMsgIndex:     1,
   105  				},
   106  				DepositMsgStates:  []types.DepositMsgState{{MsgIndex: 1}},
   107  				WithdrawMsgStates: nil,
   108  				SwapMsgStates:     nil,
   109  			},
   110  			true,
   111  		},
   112  		{
   113  			"MismatchingPoolBatchWithdrawMsgIndex",
   114  			types.PoolRecord{
   115  				PoolBatch: types.PoolBatch{
   116  					DepositMsgIndex:  1,
   117  					WithdrawMsgIndex: 10,
   118  					SwapMsgIndex:     1,
   119  				},
   120  				DepositMsgStates:  nil,
   121  				WithdrawMsgStates: []types.WithdrawMsgState{{MsgIndex: 1}},
   122  				SwapMsgStates:     nil,
   123  			},
   124  			true,
   125  		},
   126  		{
   127  			"MismatchingPoolBatchSwapMsgIndex",
   128  			types.PoolRecord{
   129  				PoolBatch: types.PoolBatch{
   130  					DepositMsgIndex:  1,
   131  					WithdrawMsgIndex: 1,
   132  					SwapMsgIndex:     10,
   133  				},
   134  				DepositMsgStates:  nil,
   135  				WithdrawMsgStates: nil,
   136  				SwapMsgStates:     []types.SwapMsgState{{MsgIndex: 1}},
   137  			},
   138  			true,
   139  		},
   140  	}
   141  
   142  	for _, tc := range testCases {
   143  		t.Run(tc.name, func(t *testing.T) {
   144  			err := tc.poolRecord.Validate()
   145  			if tc.shouldFail {
   146  				require.ErrorIs(t, err, types.ErrBadBatchMsgIndex)
   147  			} else {
   148  				require.NoError(t, err)
   149  			}
   150  		})
   151  	}
   152  }