github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/simulation/decoder_test.go (about) 1 package simulation_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" 10 "github.com/cosmos/cosmos-sdk/simapp" 11 sdk "github.com/cosmos/cosmos-sdk/types" 12 "github.com/cosmos/cosmos-sdk/types/kv" 13 14 "github.com/gravity-devs/liquidity/x/liquidity/simulation" 15 "github.com/gravity-devs/liquidity/x/liquidity/types" 16 ) 17 18 var ( 19 pk1 = ed25519.GenPrivKey().PubKey() 20 reserveAccAddr1 = sdk.AccAddress(pk1.Address()) 21 reserveCoinDenoms = []string{"dzkiv", "imwo"} 22 poolName = types.PoolName(reserveCoinDenoms, uint32(1)) 23 poolCoinDenom = types.GetPoolCoinDenom(poolName) 24 ) 25 26 func TestDecodeLiquidityStore(t *testing.T) { 27 cdc := simapp.MakeTestEncodingConfig().Marshaler 28 dec := simulation.NewDecodeStore(cdc) 29 30 pool := types.Pool{ 31 Id: uint64(1), 32 TypeId: uint32(1), 33 ReserveCoinDenoms: reserveCoinDenoms, 34 ReserveAccountAddress: reserveAccAddr1.String(), 35 PoolCoinDenom: poolCoinDenom, 36 } 37 batch := types.NewPoolBatch(1, 1) 38 depositMsgState := types.DepositMsgState{ 39 MsgHeight: int64(50), 40 MsgIndex: uint64(1), 41 Executed: true, 42 Succeeded: true, 43 ToBeDeleted: true, 44 Msg: &types.MsgDepositWithinBatch{PoolId: uint64(1)}, 45 } 46 withdrawMsgState := types.WithdrawMsgState{ 47 MsgHeight: int64(50), 48 MsgIndex: uint64(1), 49 Executed: true, 50 Succeeded: true, 51 ToBeDeleted: true, 52 Msg: &types.MsgWithdrawWithinBatch{PoolId: uint64(1)}, 53 } 54 swapMsgState := types.SwapMsgState{ 55 MsgHeight: int64(50), 56 MsgIndex: uint64(1), 57 Executed: true, 58 Succeeded: true, 59 ToBeDeleted: true, 60 Msg: &types.MsgSwapWithinBatch{PoolId: uint64(1)}, 61 } 62 63 kvPairs := kv.Pairs{ 64 Pairs: []kv.Pair{ 65 {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&pool)}, 66 {Key: types.PoolByReserveAccIndexKeyPrefix, Value: reserveAccAddr1.Bytes()}, 67 {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&batch)}, 68 {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&depositMsgState)}, 69 {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&withdrawMsgState)}, 70 {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&swapMsgState)}, 71 {Key: []byte{0x99}, Value: []byte{0x99}}, 72 }, 73 } 74 75 tests := []struct { 76 name string 77 expectedLog string 78 }{ 79 {"Pool", fmt.Sprintf("%v\n%v", pool, pool)}, 80 {"PoolByReserveAccIndex", fmt.Sprintf("%v\n%v", reserveAccAddr1, reserveAccAddr1)}, 81 {"PoolBatchKey", fmt.Sprintf("%v\n%v", batch, batch)}, 82 {"PoolBatchDepositMsgStateIndex", fmt.Sprintf("%v\n%v", depositMsgState, depositMsgState)}, 83 {"PoolBatchWithdrawMsgStateIndex", fmt.Sprintf("%v\n%v", withdrawMsgState, withdrawMsgState)}, 84 {"PoolBatchSwapMsgStateIndex", fmt.Sprintf("%v\n%v", swapMsgState, swapMsgState)}, 85 {"other", ""}, 86 } 87 for i, tt := range tests { 88 i, tt := i, tt 89 t.Run(tt.name, func(t *testing.T) { 90 switch i { 91 case len(tests) - 1: 92 require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name) 93 default: 94 require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name) 95 } 96 }) 97 } 98 }