|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package queryutil_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "errors" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/hyperledger/fabric/common/flogging" |
| 15 | + commonledger "github.com/hyperledger/fabric/common/ledger" |
| 16 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/queryutil" |
| 17 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/queryutil/mock" |
| 18 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb" |
| 19 | + statedbmock "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/mock" |
| 20 | + "github.com/hyperledger/fabric/protos/ledger/queryresult" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func TestMain(m *testing.M) { |
| 25 | + flogging.SetModuleLevel("util", "debug") |
| 26 | + flogging.SetModuleLevel("statedb", "debug") |
| 27 | + os.Exit(m.Run()) |
| 28 | +} |
| 29 | + |
| 30 | +func TestCombinerGetState(t *testing.T) { |
| 31 | + batch1 := statedb.NewUpdateBatch() |
| 32 | + batch1.Put("ns1", "key1", []byte("b1_value1"), nil) |
| 33 | + batch1.Delete("ns1", "key2", nil) |
| 34 | + batch1.Put("ns1", "key3", []byte("b1_value3"), nil) |
| 35 | + |
| 36 | + batch2 := statedb.NewUpdateBatch() |
| 37 | + batch2.Put("ns1", "key1", []byte("b2_value1"), nil) |
| 38 | + batch2.Put("ns1", "key2", []byte("b2_value2"), nil) |
| 39 | + batch2.Put("ns1", "key3", []byte("b2_value3"), nil) |
| 40 | + |
| 41 | + batch3 := statedb.NewUpdateBatch() |
| 42 | + batch3.Put("ns1", "key1", []byte("b3_value1"), nil) |
| 43 | + batch3.Put("ns1", "key2", []byte("b3_value2"), nil) |
| 44 | + batch3.Delete("ns1", "key3", nil) |
| 45 | + |
| 46 | + combiner := &queryutil.QECombiner{ |
| 47 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 48 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch1}, |
| 49 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch2}, |
| 50 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch3}, |
| 51 | + }} |
| 52 | + |
| 53 | + val, err := combiner.GetState("ns1", "key1") |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.Equal(t, []byte("b1_value1"), val) |
| 56 | + |
| 57 | + val, err = combiner.GetState("ns1", "key2") |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.Nil(t, val) |
| 60 | + |
| 61 | + val, err = combiner.GetState("ns1", "key3") |
| 62 | + assert.NoError(t, err) |
| 63 | + assert.Equal(t, []byte("b1_value3"), val) |
| 64 | + |
| 65 | + combiner = &queryutil.QECombiner{ |
| 66 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 67 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch3}, |
| 68 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch2}, |
| 69 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch1}, |
| 70 | + }} |
| 71 | + val, err = combiner.GetState("ns1", "key1") |
| 72 | + assert.NoError(t, err) |
| 73 | + assert.Equal(t, []byte("b3_value1"), val) |
| 74 | + |
| 75 | + val, err = combiner.GetState("ns1", "key2") |
| 76 | + assert.NoError(t, err) |
| 77 | + assert.Equal(t, []byte("b3_value2"), val) |
| 78 | + |
| 79 | + val, err = combiner.GetState("ns1", "key3") |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.Nil(t, val) |
| 82 | +} |
| 83 | + |
| 84 | +func TestCombinerRangeScan(t *testing.T) { |
| 85 | + batch1 := statedb.NewUpdateBatch() |
| 86 | + batch1.Put("ns1", "key1", []byte("batch1_value1"), nil) |
| 87 | + batch1.Delete("ns1", "key2", nil) |
| 88 | + batch1.Put("ns1", "key3", []byte("batch1_value3"), nil) |
| 89 | + |
| 90 | + batch2 := statedb.NewUpdateBatch() |
| 91 | + batch2.Put("ns1", "key1", []byte("batch2_value1"), nil) |
| 92 | + batch2.Put("ns1", "key2", []byte("batch2_value2"), nil) |
| 93 | + batch2.Delete("ns1", "key3", nil) |
| 94 | + batch2.Put("ns1", "key4", []byte("batch2_value4"), nil) |
| 95 | + |
| 96 | + batch3 := statedb.NewUpdateBatch() |
| 97 | + batch3.Put("ns1", "key0", []byte("batch3_value0"), nil) |
| 98 | + batch3.Put("ns1", "key1", []byte("batch3_value1"), nil) |
| 99 | + batch3.Put("ns1", "key2", []byte("batch3_value2"), nil) |
| 100 | + batch3.Put("ns1", "key3", []byte("batch3_value3"), nil) |
| 101 | + batch3.Put("ns1", "key4", []byte("batch3_value4"), nil) |
| 102 | + batch3.Put("ns1", "key5", []byte("batch3_value5"), nil) |
| 103 | + |
| 104 | + combiner := &queryutil.QECombiner{ |
| 105 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 106 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch1}, |
| 107 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch2}, |
| 108 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch3}, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + itr, err := combiner.GetStateRangeScanIterator("ns1", "key1", "key4") |
| 113 | + assert.NoError(t, err) |
| 114 | + expectedResults := []*queryresult.KV{ |
| 115 | + {Namespace: "ns1", Key: "key1", Value: []byte("batch1_value1")}, |
| 116 | + {Namespace: "ns1", Key: "key3", Value: []byte("batch1_value3")}, |
| 117 | + } |
| 118 | + testutilCheckIteratorResults(t, itr, expectedResults) |
| 119 | + |
| 120 | + itr, err = combiner.GetStateRangeScanIterator("ns1", "key0", "key6") |
| 121 | + assert.NoError(t, err) |
| 122 | + expectedResults = []*queryresult.KV{ |
| 123 | + {Namespace: "ns1", Key: "key0", Value: []byte("batch3_value0")}, |
| 124 | + {Namespace: "ns1", Key: "key1", Value: []byte("batch1_value1")}, |
| 125 | + {Namespace: "ns1", Key: "key3", Value: []byte("batch1_value3")}, |
| 126 | + {Namespace: "ns1", Key: "key4", Value: []byte("batch2_value4")}, |
| 127 | + {Namespace: "ns1", Key: "key5", Value: []byte("batch3_value5")}, |
| 128 | + } |
| 129 | + testutilCheckIteratorResults(t, itr, expectedResults) |
| 130 | + |
| 131 | + combiner = &queryutil.QECombiner{ |
| 132 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 133 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch3}, |
| 134 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch2}, |
| 135 | + &queryutil.UpdateBatchBackedQueryExecuter{UpdateBatch: batch1}, |
| 136 | + }, |
| 137 | + } |
| 138 | + itr, err = combiner.GetStateRangeScanIterator("ns1", "key0", "key6") |
| 139 | + assert.NoError(t, err) |
| 140 | + expectedResults = []*queryresult.KV{ |
| 141 | + {Namespace: "ns1", Key: "key0", Value: []byte("batch3_value0")}, |
| 142 | + {Namespace: "ns1", Key: "key1", Value: []byte("batch3_value1")}, |
| 143 | + {Namespace: "ns1", Key: "key2", Value: []byte("batch3_value2")}, |
| 144 | + {Namespace: "ns1", Key: "key3", Value: []byte("batch3_value3")}, |
| 145 | + {Namespace: "ns1", Key: "key4", Value: []byte("batch3_value4")}, |
| 146 | + {Namespace: "ns1", Key: "key5", Value: []byte("batch3_value5")}, |
| 147 | + } |
| 148 | + testutilCheckIteratorResults(t, itr, expectedResults) |
| 149 | +} |
| 150 | + |
| 151 | +func TestGetStateError(t *testing.T) { |
| 152 | + qe1 := &mock.QueryExecuter{} |
| 153 | + qe1.GetStateReturns(&statedb.VersionedValue{Value: []byte("testValue")}, nil) |
| 154 | + qe2 := &mock.QueryExecuter{} |
| 155 | + qe2.GetStateReturns(nil, errors.New("Error for testing")) |
| 156 | + combiner1 := &queryutil.QECombiner{ |
| 157 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 158 | + qe1, qe2, |
| 159 | + }, |
| 160 | + } |
| 161 | + _, err := combiner1.GetState("ns", "key1") |
| 162 | + assert.NoError(t, err) |
| 163 | + |
| 164 | + combiner2 := &queryutil.QECombiner{ |
| 165 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 166 | + qe2, qe1, |
| 167 | + }, |
| 168 | + } |
| 169 | + _, err = combiner2.GetState("ns", "key1") |
| 170 | + assert.Error(t, err) |
| 171 | +} |
| 172 | + |
| 173 | +func TestGetRangeScanError(t *testing.T) { |
| 174 | + itr1 := &statedbmock.ResultsIterator{} |
| 175 | + itr1.NextReturns( |
| 176 | + &statedb.VersionedKV{ |
| 177 | + CompositeKey: statedb.CompositeKey{Namespace: "ns", Key: "dummyKey"}, |
| 178 | + VersionedValue: statedb.VersionedValue{Value: []byte("dummyVal")}, |
| 179 | + }, |
| 180 | + nil, |
| 181 | + ) |
| 182 | + |
| 183 | + qe1 := &mock.QueryExecuter{} |
| 184 | + qe1.GetStateRangeScanIteratorReturns(itr1, nil) |
| 185 | + qe2 := &mock.QueryExecuter{} |
| 186 | + qe2.GetStateRangeScanIteratorReturns(nil, errors.New("dummy error on getting the iterator")) |
| 187 | + combiner := &queryutil.QECombiner{ |
| 188 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 189 | + qe1, qe2, |
| 190 | + }, |
| 191 | + } |
| 192 | + _, err := combiner.GetStateRangeScanIterator("ns", "startKey", "endKey") |
| 193 | + assert.Error(t, err) |
| 194 | +} |
| 195 | + |
| 196 | +func TestGetRangeScanUnderlyingIteratorReturnsError(t *testing.T) { |
| 197 | + itr1 := &statedbmock.ResultsIterator{} |
| 198 | + itr1.NextReturns( |
| 199 | + &statedb.VersionedKV{ |
| 200 | + CompositeKey: statedb.CompositeKey{Namespace: "ns", Key: "dummyKey"}, |
| 201 | + VersionedValue: statedb.VersionedValue{Value: []byte("dummyVal")}, |
| 202 | + }, |
| 203 | + nil, |
| 204 | + ) |
| 205 | + |
| 206 | + itr2 := &statedbmock.ResultsIterator{} |
| 207 | + itr2.NextReturns( |
| 208 | + nil, |
| 209 | + errors.New("dummyErrorOnIteratorNext"), |
| 210 | + ) |
| 211 | + |
| 212 | + qe1 := &mock.QueryExecuter{} |
| 213 | + qe1.GetStateRangeScanIteratorReturns(itr1, nil) |
| 214 | + qe2 := &mock.QueryExecuter{} |
| 215 | + qe2.GetStateRangeScanIteratorReturns(itr2, nil) |
| 216 | + combiner := &queryutil.QECombiner{ |
| 217 | + QueryExecuters: []queryutil.QueryExecuter{ |
| 218 | + qe1, qe2, |
| 219 | + }, |
| 220 | + } |
| 221 | + _, err := combiner.GetStateRangeScanIterator("ns", "startKey", "endKey") |
| 222 | + assert.Error(t, err) |
| 223 | +} |
| 224 | + |
| 225 | +func testutilCheckIteratorResults(t *testing.T, itr commonledger.ResultsIterator, expectedResults []*queryresult.KV) { |
| 226 | + results := []*queryresult.KV{} |
| 227 | + for { |
| 228 | + result, err := itr.Next() |
| 229 | + assert.NoError(t, err) |
| 230 | + if result == nil { |
| 231 | + break |
| 232 | + } |
| 233 | + results = append(results, result.(*queryresult.KV)) |
| 234 | + } |
| 235 | + assert.Equal(t, expectedResults, results) |
| 236 | +} |
0 commit comments