github.com/iotexproject/iotex-core@v1.14.1-rc1/db/trie/kvstoreimpl_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package trie
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/pkg/errors"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/iotexproject/iotex-core/db"
    15  )
    16  
    17  func TestKVStore_Get(t *testing.T) {
    18  	var (
    19  		require = require.New(t)
    20  		tests   = []struct{ k, v []byte }{
    21  			{[]byte("key"), []byte("value")},
    22  			{[]byte("k"), []byte("v")},
    23  			{[]byte("iotex"), []byte("coin")},
    24  			{[]byte("puppy"), []byte("dog")},
    25  		}
    26  	)
    27  
    28  	store, err := NewKVStore("test", db.NewMemKVStore())
    29  	require.NoError(err)
    30  
    31  	for _, test := range tests {
    32  		require.NoError(store.Put(test.k, test.v))
    33  	}
    34  	for _, test := range tests {
    35  		val, err := store.Get(test.k)
    36  		require.NoError(err)
    37  		require.Equal(test.v, val)
    38  	}
    39  }
    40  
    41  func TestKvStore_Put(t *testing.T) {
    42  	var (
    43  		require = require.New(t)
    44  		key     = []byte("key")
    45  		tests   = []struct{ k, v []byte }{
    46  			{key, []byte("value1")},
    47  			{key, []byte("value2")},
    48  			{key, []byte("value3")},
    49  		}
    50  	)
    51  
    52  	store, err := NewKVStore("test", db.NewMemKVStore())
    53  	require.NoError(err)
    54  
    55  	for _, test := range tests {
    56  		require.NoError(store.Put(test.k, test.v))
    57  	}
    58  	val, err := store.Get(key)
    59  	require.NoError(err)
    60  	require.Equal(tests[len(tests)-1].v, val)
    61  }
    62  
    63  func TestKvStore_Delete(t *testing.T) {
    64  	var (
    65  		require = require.New(t)
    66  		tests   = []struct{ k, v []byte }{
    67  			{[]byte("key"), []byte("value")},
    68  			{[]byte("k"), []byte("v")},
    69  			{[]byte("iotex"), []byte("coin")},
    70  			{[]byte("puppy"), []byte("dog")},
    71  		}
    72  	)
    73  
    74  	store, err := NewKVStore("test", db.NewMemKVStore())
    75  	require.NoError(err)
    76  
    77  	for _, test := range tests {
    78  		_, err = store.Get(test.k)
    79  		require.Equal(ErrNotExist, errors.Cause(err))
    80  
    81  		require.NoError(store.Put(test.k, test.v))
    82  	}
    83  	for _, test := range tests {
    84  		val, err := store.Get(test.k)
    85  		require.NoError(err)
    86  		require.Equal(test.v, val)
    87  
    88  		require.NoError(store.Delete(test.k))
    89  		_, err = store.Get(test.k)
    90  		require.Equal(ErrNotExist, errors.Cause(err))
    91  	}
    92  }