github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/legacy/v043/store_test.go (about)

     1  package v043_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/cosmos/cosmos-sdk/simapp"
     8  	gogotypes "github.com/gogo/protobuf/types"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/cosmos/cosmos-sdk/testutil"
    13  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    14  	sdk "github.com/cosmos/cosmos-sdk/types"
    15  
    16  	v042liquidity "github.com/gravity-devs/liquidity/x/liquidity/legacy/v042"
    17  	v043liquidity "github.com/gravity-devs/liquidity/x/liquidity/legacy/v043"
    18  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    19  )
    20  
    21  func TestStoreMigration(t *testing.T) {
    22  	encCfg := simapp.MakeTestEncodingConfig()
    23  	liquidityKey := sdk.NewKVStoreKey(v042liquidity.ModuleName)
    24  	ctx := testutil.DefaultContext(liquidityKey, sdk.NewTransientStoreKey("transient_test"))
    25  	store := ctx.KVStore(liquidityKey)
    26  
    27  	_, _, reserveAcc1 := testdata.KeyTestPubAddr()
    28  	_, _, reserveAcc2 := testdata.KeyTestPubAddr()
    29  
    30  	// Use dummy value for all keys.
    31  	value := encCfg.Marshaler.MustMarshal(&gogotypes.UInt64Value{Value: 1})
    32  
    33  	testCases := []struct {
    34  		name   string
    35  		oldKey []byte
    36  		newKey []byte
    37  	}{
    38  		{
    39  			"reserveAcc1",
    40  			v042liquidity.GetPoolByReserveAccIndexKey(reserveAcc1),
    41  			types.GetPoolByReserveAccIndexKey(reserveAcc1),
    42  		},
    43  		{
    44  			"reserveAcc2",
    45  			v042liquidity.GetPoolByReserveAccIndexKey(reserveAcc2),
    46  			types.GetPoolByReserveAccIndexKey(reserveAcc2),
    47  		},
    48  		{
    49  			"poolBatchIndexKeyPrefix1",
    50  			v042liquidity.GetPoolBatchIndexKey(1),
    51  			nil,
    52  		},
    53  		{
    54  			"poolBatchIndexKeyPrefix2",
    55  			v042liquidity.GetPoolBatchIndexKey(2),
    56  			nil,
    57  		},
    58  	}
    59  
    60  	// Set all the old keys to the store
    61  	for _, tc := range testCases {
    62  		store.Set(tc.oldKey, value)
    63  	}
    64  
    65  	// Run migrations.
    66  	err := v043liquidity.MigrateStore(ctx, liquidityKey)
    67  	require.NoError(t, err)
    68  
    69  	// Make sure the new keys are set and old keys are deleted.
    70  	for _, tc := range testCases {
    71  		tc := tc
    72  		t.Run(tc.name, func(t *testing.T) {
    73  			if !bytes.Equal(tc.oldKey, tc.newKey) {
    74  				require.Nil(t, store.Get(tc.oldKey))
    75  			}
    76  			if tc.newKey != nil {
    77  				require.Equal(t, value, store.Get(tc.newKey))
    78  			}
    79  		})
    80  	}
    81  }