test_common.go 8.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commontests

import (
20
	"strings"
21
22
	"testing"

23
	"github.com/hyperledger/fabric/common/ledger/testutil"
24
25
26
27
28
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
)

// TestBasicRW tests basic read-write
29
func TestBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {
30
	db, err := dbProvider.GetDBHandle("TestDB")
31
	testutil.AssertNoError(t, err, "")
32
33
34
35
36

	// Test retrieval of non-existent key - returns nil rather than error
	// For more details see https://github.com/hyperledger-archives/fabric/issues/936.
	val, err := db.GetState("ns", "key1")
	testutil.AssertNoError(t, err, "Should receive nil rather than error upon reading non existent key")
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	testutil.AssertNil(t, val)

	batch := statedb.NewUpdateBatch()
	vv1 := statedb.VersionedValue{Value: []byte("value1"), Version: version.NewHeight(1, 1)}
	vv2 := statedb.VersionedValue{Value: []byte("value2"), Version: version.NewHeight(1, 2)}
	vv3 := statedb.VersionedValue{Value: []byte("value3"), Version: version.NewHeight(1, 3)}
	vv4 := statedb.VersionedValue{Value: []byte{}, Version: version.NewHeight(1, 4)}
	batch.Put("ns1", "key1", vv1.Value, vv1.Version)
	batch.Put("ns1", "key2", vv2.Value, vv2.Version)
	batch.Put("ns2", "key3", vv3.Value, vv3.Version)
	batch.Put("ns2", "key4", vv4.Value, vv4.Version)
	savePoint := version.NewHeight(2, 5)
	db.ApplyUpdates(batch, savePoint)

	vv, _ := db.GetState("ns1", "key1")
	testutil.AssertEquals(t, vv, &vv1)

54
55
	vv, _ = db.GetState("ns2", "key4")
	testutil.AssertEquals(t, vv, &vv4)
56
57
58
59
60
61

	sp, err := db.GetLatestSavePoint()
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, sp, savePoint)
}

62
63
// TestMultiDBBasicRW tests basic read-write on multiple dbs
func TestMultiDBBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {
64
65
66
67
68
	db1, err := dbProvider.GetDBHandle("TestDB1")
	testutil.AssertNoError(t, err, "")

	db2, err := dbProvider.GetDBHandle("TestDB2")
	testutil.AssertNoError(t, err, "")
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

	batch1 := statedb.NewUpdateBatch()
	vv1 := statedb.VersionedValue{Value: []byte("value1_db1"), Version: version.NewHeight(1, 1)}
	vv2 := statedb.VersionedValue{Value: []byte("value2_db1"), Version: version.NewHeight(1, 2)}
	batch1.Put("ns1", "key1", vv1.Value, vv1.Version)
	batch1.Put("ns1", "key2", vv2.Value, vv2.Version)
	savePoint1 := version.NewHeight(1, 2)
	db1.ApplyUpdates(batch1, savePoint1)

	batch2 := statedb.NewUpdateBatch()
	vv3 := statedb.VersionedValue{Value: []byte("value1_db2"), Version: version.NewHeight(1, 4)}
	vv4 := statedb.VersionedValue{Value: []byte("value2_db2"), Version: version.NewHeight(1, 5)}
	batch2.Put("ns1", "key1", vv3.Value, vv3.Version)
	batch2.Put("ns1", "key2", vv4.Value, vv4.Version)
	savePoint2 := version.NewHeight(1, 5)
	db2.ApplyUpdates(batch2, savePoint2)

	vv, _ := db1.GetState("ns1", "key1")
	testutil.AssertEquals(t, vv, &vv1)

	sp, err := db1.GetLatestSavePoint()
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, sp, savePoint1)

93
94
	vv, _ = db2.GetState("ns1", "key1")
	testutil.AssertEquals(t, vv, &vv3)
95
96
97
98
99
100

	sp, err = db2.GetLatestSavePoint()
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, sp, savePoint2)
}

101
// TestDeletes tests deteles
102
func TestDeletes(t *testing.T, dbProvider statedb.VersionedDBProvider) {
103
104
	db, err := dbProvider.GetDBHandle("TestDB")
	testutil.AssertNoError(t, err, "")
105

106
107
108
109
110
111
112
113
114
115
116
117
	batch := statedb.NewUpdateBatch()
	vv1 := statedb.VersionedValue{Value: []byte("value1"), Version: version.NewHeight(1, 1)}
	vv2 := statedb.VersionedValue{Value: []byte("value2"), Version: version.NewHeight(1, 2)}
	vv3 := statedb.VersionedValue{Value: []byte("value1"), Version: version.NewHeight(1, 3)}
	vv4 := statedb.VersionedValue{Value: []byte("value2"), Version: version.NewHeight(1, 4)}

	batch.Put("ns", "key1", vv1.Value, vv1.Version)
	batch.Put("ns", "key2", vv2.Value, vv2.Version)
	batch.Put("ns", "key3", vv2.Value, vv3.Version)
	batch.Put("ns", "key4", vv2.Value, vv4.Version)
	batch.Delete("ns", "key3", version.NewHeight(1, 5))
	savePoint := version.NewHeight(1, 5)
118
	err = db.ApplyUpdates(batch, savePoint)
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	testutil.AssertNoError(t, err, "")
	vv, _ := db.GetState("ns", "key2")
	testutil.AssertEquals(t, vv, &vv2)

	vv, err = db.GetState("ns", "key3")
	testutil.AssertNoError(t, err, "")
	testutil.AssertNil(t, vv)

	batch = statedb.NewUpdateBatch()
	batch.Delete("ns", "key2", version.NewHeight(1, 6))
	err = db.ApplyUpdates(batch, savePoint)
	testutil.AssertNoError(t, err, "")
	vv, err = db.GetState("ns", "key2")
	testutil.AssertNoError(t, err, "")
	testutil.AssertNil(t, vv)
}

// TestIterator tests the iterator
137
func TestIterator(t *testing.T, dbProvider statedb.VersionedDBProvider) {
138
139
	db, err := dbProvider.GetDBHandle("TestDB")
	testutil.AssertNoError(t, err, "")
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
	db.Open()
	defer db.Close()
	batch := statedb.NewUpdateBatch()
	batch.Put("ns1", "key1", []byte("value1"), version.NewHeight(1, 1))
	batch.Put("ns1", "key2", []byte("value2"), version.NewHeight(1, 2))
	batch.Put("ns1", "key3", []byte("value3"), version.NewHeight(1, 3))
	batch.Put("ns1", "key4", []byte("value4"), version.NewHeight(1, 4))
	batch.Put("ns2", "key5", []byte("value5"), version.NewHeight(1, 5))
	batch.Put("ns2", "key6", []byte("value6"), version.NewHeight(1, 6))
	batch.Put("ns3", "key7", []byte("value7"), version.NewHeight(1, 7))
	savePoint := version.NewHeight(2, 5)
	db.ApplyUpdates(batch, savePoint)

	itr1, _ := db.GetStateRangeScanIterator("ns1", "key1", "")
	testItr(t, itr1, []string{"key1", "key2", "key3", "key4"})

	itr2, _ := db.GetStateRangeScanIterator("ns1", "key2", "key3")
	testItr(t, itr2, []string{"key2"})

	itr3, _ := db.GetStateRangeScanIterator("ns1", "", "")
	testItr(t, itr3, []string{"key1", "key2", "key3", "key4"})

	itr4, _ := db.GetStateRangeScanIterator("ns2", "", "")
	testItr(t, itr4, []string{"key5", "key6"})
}

func testItr(t *testing.T, itr statedb.ResultsIterator, expectedKeys []string) {
	defer itr.Close()
	for _, expectedKey := range expectedKeys {
169
170
		queryResult, _ := itr.Next()
		vkv := queryResult.(*statedb.VersionedKV)
171
172
173
174
175
176
177
		key := vkv.Key
		testutil.AssertEquals(t, key, expectedKey)
	}
	last, err := itr.Next()
	testutil.AssertNoError(t, err, "")
	testutil.AssertNil(t, last)
}
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

// TestQuery tests queries
func TestQuery(t *testing.T, dbProvider statedb.VersionedDBProvider) {
	db, err := dbProvider.GetDBHandle("TestDB")
	testutil.AssertNoError(t, err, "")
	db.Open()
	defer db.Close()
	batch := statedb.NewUpdateBatch()
	jsonValue1 := "{\"asset_name\": \"marble1\",\"color\": \"blue\",\"size\": 35,\"owner\": \"tom\"}"
	batch.Put("ns1", "key1", []byte(jsonValue1), version.NewHeight(1, 1))
	jsonValue2 := "{\"asset_name\": \"marble1\",\"color\": \"blue\",\"size\": 35,\"owner\": \"jerry\"}"
	batch.Put("ns1", "key2", []byte(jsonValue2), version.NewHeight(1, 2))
	savePoint := version.NewHeight(2, 5)
	db.ApplyUpdates(batch, savePoint)

	// query for owner=jerry
	itr, err := db.ExecuteQuery("{\"selector\":{\"owner\":\"jerry\"}}")
	testutil.AssertNoError(t, err, "")

	// verify one jerry result
	queryResult1, err := itr.Next()
	testutil.AssertNoError(t, err, "")
	testutil.AssertNotNil(t, queryResult1)
	versionedQueryRecord := queryResult1.(*statedb.VersionedQueryRecord)
	stringRecord := string(versionedQueryRecord.Record)
	bFoundJerry := strings.Contains(stringRecord, "jerry")
	testutil.AssertEquals(t, bFoundJerry, true)

	// verify no more results
	queryResult2, err := itr.Next()
	testutil.AssertNoError(t, err, "")
	testutil.AssertNil(t, queryResult2)

	// query using bad query string
	itr, err = db.ExecuteQuery("this is an invalid query string")
	testutil.AssertError(t, err, "Should have received an error for invalid query string")

	// query returns 0 records
	itr, err = db.ExecuteQuery("{\"selector\":{\"owner\":\"not_a_valid_name\"}}")
	testutil.AssertNoError(t, err, "")

	// verify no results
	queryResult3, err := itr.Next()
	testutil.AssertNoError(t, err, "")
	testutil.AssertNil(t, queryResult3)
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

	// query with fields
	itr, err = db.ExecuteQuery("{\"selector\":{\"owner\":\"jerry\"},\"fields\": [\"owner\", \"asset_name\", \"color\", \"size\"]}")
	testutil.AssertNoError(t, err, "")

	// verify one jerry result
	queryResult1, err = itr.Next()
	testutil.AssertNoError(t, err, "")
	testutil.AssertNotNil(t, queryResult1)
	versionedQueryRecord = queryResult1.(*statedb.VersionedQueryRecord)
	stringRecord = string(versionedQueryRecord.Record)
	bFoundJerry = strings.Contains(stringRecord, "jerry")
	testutil.AssertEquals(t, bFoundJerry, true)

	// verify no more results
	queryResult2, err = itr.Next()
	testutil.AssertNoError(t, err, "")
	testutil.AssertNil(t, queryResult2)

242
}