github.com/iotexproject/iotex-core@v1.14.1-rc1/db/kvstore_impl_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 db 7 8 import ( 9 "testing" 10 11 "github.com/iotexproject/iotex-core/db/batch" 12 13 "github.com/stretchr/testify/require" 14 ) 15 16 var ( 17 _namespace = "ns" 18 19 _k1 = []byte("key_1") 20 _k2 = []byte("key_2") 21 _k3 = []byte("key_3") 22 _k4 = []byte("key_4") 23 24 _v1 = []byte("value_1") 25 _v2 = []byte("value_2") 26 _v3 = []byte("value_3") 27 _v4 = []byte("value_4") 28 ) 29 30 func TestKVStoreImpl(t *testing.T) { 31 require := require.New(t) 32 33 s := NewMemKVStore() 34 35 // 1. read nonexistent _namespace 36 v, err := s.Get(_namespace, _k1) 37 require.Error(err) 38 require.Nil(v) 39 40 // 2. read nonexistent key 41 err = s.Put(_namespace, _k1, _v1) 42 require.NoError(err) 43 v, err = s.Get(_namespace, _k2) 44 require.Error(err) 45 require.Nil(v) 46 47 // 3. write once read many times 48 v, err = s.Get(_namespace, _k1) 49 require.NoError(err) 50 require.Equal(v, _v1) 51 52 // 4. write the same key many times 53 err = s.Put(_namespace, _k1, _v2) 54 require.NoError(err) 55 v, err = s.Get(_namespace, _k1) 56 require.NoError(err) 57 require.Equal(v, _v2) 58 59 err = s.Put(_namespace, _k1, _v3) 60 require.NoError(err) 61 v, err = s.Get(_namespace, _k1) 62 require.NoError(err) 63 require.Equal(v, _v3) 64 65 // 5. delete nonexistent key 66 err = s.Delete(_namespace, _k2) 67 require.NoError(err) 68 69 // 6. delete once 70 err = s.Delete(_namespace, _k1) 71 require.NoError(err) 72 73 // 7. read deleted key 74 v, err = s.Get(_namespace, _k1) 75 require.Error(err) 76 require.Nil(v) 77 78 // 8. write the same key again after deleted 79 err = s.Put(_namespace, _k1, _v1) 80 require.NoError(err) 81 v, err = s.Get(_namespace, _k1) 82 require.NoError(err) 83 require.Equal(v, _v1) 84 85 // 9. delete the same key many times 86 err = s.Delete(_namespace, _k1) 87 require.NoError(err) 88 err = s.Delete(_namespace, _k1) 89 require.NoError(err) 90 err = s.Delete(_namespace, _k1) 91 require.NoError(err) 92 93 // 10. write many key-value pairs 94 err = s.Put(_namespace, _k1, _v1) 95 require.NoError(err) 96 err = s.Put(_namespace, _k2, _v2) 97 require.NoError(err) 98 err = s.Put(_namespace, _k3, _v3) 99 require.NoError(err) 100 101 v, err = s.Get(_namespace, _k1) 102 require.NoError(err) 103 require.Equal(v, _v1) 104 105 v, err = s.Get(_namespace, _k2) 106 require.NoError(err) 107 require.Equal(v, _v2) 108 109 v, err = s.Get(_namespace, _k3) 110 require.NoError(err) 111 require.Equal(v, _v3) 112 113 // 11. batch write for existing KvStore 114 b := batch.NewBatch() 115 b.Put(_namespace, _k1, _v1, "") 116 b.Put(_namespace, _k2, _v3, "") 117 b.Put(_namespace, _k3, _v2, "") 118 b.Put(_namespace, _k4, _v4, "") 119 120 err = s.WriteBatch(b) 121 require.NoError(err) 122 123 v, err = s.Get(_namespace, _k1) 124 require.NoError(err) 125 require.Equal(v, _v1) 126 127 v, err = s.Get(_namespace, _k2) 128 require.NoError(err) 129 require.Equal(v, _v3) 130 131 v, err = s.Get(_namespace, _k3) 132 require.NoError(err) 133 require.Equal(v, _v2) 134 135 v, err = s.Get(_namespace, _k4) 136 require.NoError(err) 137 require.Equal(v, _v4) 138 139 // 12. batch write for brand new KvStore 140 ss := NewMemKVStore() 141 142 bb := batch.NewBatch() 143 bb.Put(_namespace, _k1, _v1, "") 144 bb.Put(_namespace, _k2, _v2, "") 145 bb.Put(_namespace, _k3, _v3, "") 146 bb.Put(_namespace, _k4, _v4, "") 147 148 err = ss.WriteBatch(bb) 149 require.NoError(err) 150 151 v, err = ss.Get(_namespace, _k1) 152 require.NoError(err) 153 require.Equal(v, _v1) 154 155 v, err = ss.Get(_namespace, _k2) 156 require.NoError(err) 157 require.Equal(v, _v2) 158 159 v, err = ss.Get(_namespace, _k3) 160 require.NoError(err) 161 require.Equal(v, _v3) 162 163 v, err = ss.Get(_namespace, _k4) 164 require.NoError(err) 165 require.Equal(v, _v4) 166 }