github.com/turingchain2020/turingchain@v1.1.21/blockchain/localdb_test.go (about) 1 package blockchain_test 2 3 import ( 4 "testing" 5 6 "github.com/turingchain2020/turingchain/common" 7 "github.com/turingchain2020/turingchain/types" 8 "github.com/turingchain2020/turingchain/util/testnode" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestLocalDBRollback(t *testing.T) { 13 mock33 := testnode.New("", nil) 14 defer mock33.Close() 15 //测试localdb 16 api := mock33.GetAPI() 17 param := &types.LocalDBGet{} 18 param.Keys = append(param.Keys, []byte("hello")) 19 values, err := api.LocalGet(param) 20 assert.Equal(t, err, nil) 21 assert.Equal(t, 1, len(values.Values)) 22 assert.Nil(t, values.Values[0]) 23 24 param2 := &types.LocalDBSet{} 25 param2.KV = append(param2.KV, &types.KeyValue{Key: []byte("hello"), Value: []byte("world")}) 26 err = api.LocalSet(param2) 27 assert.Equal(t, err, types.ErrNotSetInTransaction) 28 29 //set in transaction 30 id, err := api.LocalNew(false) 31 assert.Nil(t, err) 32 assert.True(t, id.Data > 0) 33 34 err = api.LocalClose(id) 35 assert.Nil(t, err) 36 37 id, err = api.LocalNew(false) 38 assert.Nil(t, err) 39 assert.True(t, id.Data > 0) 40 41 err = api.LocalBegin(id) 42 assert.Nil(t, err) 43 44 param2 = &types.LocalDBSet{Txid: id.Data} 45 param2.KV = append(param2.KV, &types.KeyValue{Key: []byte("hello"), Value: []byte("world")}) 46 err = api.LocalSet(param2) 47 assert.Nil(t, err) 48 49 values, err = api.LocalGet(param) 50 assert.Equal(t, err, nil) 51 assert.Equal(t, 1, len(values.Values)) 52 assert.Nil(t, values.Values[0]) 53 54 param.Txid = id.Data 55 values, err = api.LocalGet(param) 56 assert.Equal(t, err, nil) 57 assert.Equal(t, 1, len(values.Values)) 58 assert.Equal(t, []byte("world"), values.Values[0]) 59 60 param2 = &types.LocalDBSet{Txid: id.Data} 61 param2.KV = append(param2.KV, &types.KeyValue{Key: []byte("hello2"), Value: []byte("world2")}) 62 err = api.LocalSet(param2) 63 assert.Nil(t, err) 64 65 param.Txid = id.Data 66 param.Keys = append(param.Keys, []byte("hello2")) 67 param.Keys = append(param.Keys, []byte("hello3")) 68 69 values, err = api.LocalGet(param) 70 assert.Equal(t, err, nil) 71 assert.Equal(t, 3, len(values.Values)) 72 assert.Equal(t, []byte("world"), values.Values[0]) 73 assert.Equal(t, []byte("world2"), values.Values[1]) 74 assert.Nil(t, values.Values[2]) 75 76 list := &types.LocalDBList{ 77 Txid: id.Data, 78 Prefix: []byte("hello"), 79 Direction: 1, 80 } 81 values, err = api.LocalList(list) 82 assert.Equal(t, err, nil) 83 assert.Equal(t, 2, len(values.Values)) 84 assert.Equal(t, []byte("world"), values.Values[0]) 85 assert.Equal(t, []byte("world2"), values.Values[1]) 86 87 err = api.LocalRollback(id) 88 assert.Nil(t, err) 89 90 list = &types.LocalDBList{ 91 Txid: id.Data, 92 Prefix: []byte("hello"), 93 Direction: 1, 94 } 95 _, err = api.LocalList(list) 96 assert.Nil(t, err) 97 98 list = &types.LocalDBList{ 99 Prefix: []byte("hello"), 100 Direction: 1, 101 } 102 values, err = api.LocalList(list) 103 assert.Equal(t, err, nil) 104 assert.Equal(t, 0, len(values.Values)) 105 } 106 107 func TestLocalDBCommit(t *testing.T) { 108 mock33 := testnode.New("", nil) 109 defer mock33.Close() 110 111 //测试localdb 112 api := mock33.GetAPI() 113 param := &types.LocalDBGet{} 114 param.Keys = append(param.Keys, []byte("hello")) 115 values, err := api.LocalGet(param) 116 assert.Equal(t, err, nil) 117 assert.Equal(t, 1, len(values.Values)) 118 assert.Nil(t, values.Values[0]) 119 120 param2 := &types.LocalDBSet{} 121 param2.KV = append(param2.KV, &types.KeyValue{Key: []byte("hello"), Value: []byte("world")}) 122 err = api.LocalSet(param2) 123 assert.Equal(t, err, types.ErrNotSetInTransaction) 124 125 //set in transaction 126 id, err := api.LocalNew(false) 127 assert.Nil(t, err) 128 assert.True(t, id.Data > 0) 129 130 err = api.LocalBegin(id) 131 assert.Nil(t, err) 132 133 param2 = &types.LocalDBSet{Txid: id.Data} 134 param2.KV = append(param2.KV, &types.KeyValue{Key: []byte("hello"), Value: []byte("world")}) 135 err = api.LocalSet(param2) 136 assert.Nil(t, err) 137 138 values, err = api.LocalGet(param) 139 assert.Equal(t, err, nil) 140 assert.Equal(t, 1, len(values.Values)) 141 assert.Nil(t, values.Values[0]) 142 143 param.Txid = id.Data 144 values, err = api.LocalGet(param) 145 assert.Equal(t, err, nil) 146 assert.Equal(t, 1, len(values.Values)) 147 assert.Equal(t, []byte("world"), values.Values[0]) 148 149 param2 = &types.LocalDBSet{Txid: id.Data} 150 param2.KV = append(param2.KV, &types.KeyValue{Key: []byte("hello2"), Value: []byte("world2")}) 151 err = api.LocalSet(param2) 152 assert.Nil(t, err) 153 154 param.Txid = id.Data 155 param.Keys = append(param.Keys, []byte("hello2")) 156 values, err = api.LocalGet(param) 157 assert.Equal(t, err, nil) 158 assert.Equal(t, 2, len(values.Values)) 159 assert.Equal(t, []byte("world"), values.Values[0]) 160 assert.Equal(t, []byte("world2"), values.Values[1]) 161 162 list := &types.LocalDBList{ 163 Txid: id.Data, 164 Prefix: []byte("hello"), 165 Direction: 1, 166 } 167 values, err = api.LocalList(list) 168 assert.Equal(t, err, nil) 169 assert.Equal(t, 2, len(values.Values)) 170 assert.Equal(t, []byte("world"), values.Values[0]) 171 assert.Equal(t, []byte("world2"), values.Values[1]) 172 173 err = api.LocalCommit(id) 174 assert.Nil(t, err) 175 176 err = api.LocalClose(id) 177 assert.Nil(t, err) 178 179 list = &types.LocalDBList{ 180 Txid: id.Data, 181 Prefix: []byte("hello"), 182 Direction: 1, 183 } 184 _, err = api.LocalList(list) 185 assert.Equal(t, err, common.ErrPointerNotFound) 186 187 //系统只读,无法写入数据 188 list = &types.LocalDBList{ 189 Prefix: []byte("hello"), 190 Direction: 1, 191 } 192 values, err = api.LocalList(list) 193 assert.Equal(t, err, nil) 194 assert.Equal(t, 0, len(values.Values)) 195 }