@@ -17,12 +17,83 @@ limitations under the License.
17
17
package statedb
18
18
19
19
import (
20
+ "sort"
20
21
"testing"
21
22
22
23
"github.com/hyperledger/fabric/common/ledger/testutil"
23
24
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
24
25
)
25
26
27
+ func TestPanic (t * testing.T ) {
28
+ defer func () {
29
+ if r := recover (); r == nil {
30
+ t .Fatalf ("Nil value to Put() did not panic\n " )
31
+ }
32
+ }()
33
+
34
+ batch := NewUpdateBatch ()
35
+ // The following call to Put() should result in panic
36
+ batch .Put ("ns1" , "key1" , nil , nil )
37
+ }
38
+
39
+ //Test Put(), Get(), and Delete()
40
+ func TestPutGetDeleteExistsGetUpdates (t * testing.T ) {
41
+ batch := NewUpdateBatch ()
42
+ batch .Put ("ns1" , "key1" , []byte ("value1" ), version .NewHeight (1 , 1 ))
43
+
44
+ //Get() should return above inserted <k,v> pair
45
+ actualVersionedValue := batch .Get ("ns1" , "key1" )
46
+ testutil .AssertEquals (t , actualVersionedValue , & VersionedValue {Value : []byte ("value1" ), Version : version .NewHeight (1 , 1 )})
47
+ //Exists() should return false as key2 does not exist
48
+ actualResult := batch .Exists ("ns1" , "key2" )
49
+ expectedResult := false
50
+ testutil .AssertEquals (t , actualResult , expectedResult )
51
+
52
+ //Exists() should return false as ns3 does not exist
53
+ actualResult = batch .Exists ("ns3" , "key2" )
54
+ expectedResult = false
55
+ testutil .AssertEquals (t , actualResult , expectedResult )
56
+
57
+ //Get() should return nill as key2 does not exist
58
+ actualVersionedValue = batch .Get ("ns1" , "key2" )
59
+ testutil .AssertNil (t , actualVersionedValue )
60
+ //Get() should return nill as ns3 does not exist
61
+ actualVersionedValue = batch .Get ("ns3" , "key2" )
62
+ testutil .AssertNil (t , actualVersionedValue )
63
+
64
+ batch .Put ("ns1" , "key2" , []byte ("value2" ), version .NewHeight (1 , 2 ))
65
+ //Exists() should return true as key2 exists
66
+ actualResult = batch .Exists ("ns1" , "key2" )
67
+ expectedResult = true
68
+ testutil .AssertEquals (t , actualResult , expectedResult )
69
+
70
+ //GetUpdatedNamespaces should return 3 namespaces
71
+ batch .Put ("ns2" , "key2" , []byte ("value2" ), version .NewHeight (1 , 2 ))
72
+ batch .Put ("ns3" , "key2" , []byte ("value2" ), version .NewHeight (1 , 2 ))
73
+ actualNamespaces := batch .GetUpdatedNamespaces ()
74
+ sort .Strings (actualNamespaces )
75
+ expectedNamespaces := []string {"ns1" , "ns2" , "ns3" }
76
+ testutil .AssertEquals (t , actualNamespaces , expectedNamespaces )
77
+
78
+ //GetUpdates should return two VersionedValues for the namespace ns1
79
+ expectedUpdates := make (map [string ]* VersionedValue )
80
+ expectedUpdates ["key1" ] = & VersionedValue {Value : []byte ("value1" ), Version : version .NewHeight (1 , 1 )}
81
+ expectedUpdates ["key2" ] = & VersionedValue {Value : []byte ("value2" ), Version : version .NewHeight (1 , 2 )}
82
+ actualUpdates := batch .GetUpdates ("ns1" )
83
+ testutil .AssertEquals (t , actualUpdates , expectedUpdates )
84
+
85
+ actualUpdates = batch .GetUpdates ("ns4" )
86
+ testutil .AssertNil (t , actualUpdates )
87
+
88
+ //Delete the above inserted <k,v> pair
89
+ batch .Delete ("ns1" , "key2" , version .NewHeight (1 , 2 ))
90
+ //Exists() should return false as key2 is deleted
91
+ actualResult = batch .Exists ("ns1" , "key2" )
92
+ expectedResult = true
93
+ testutil .AssertEquals (t , actualResult , expectedResult )
94
+
95
+ }
96
+
26
97
func TestUpdateBatchIterator (t * testing.T ) {
27
98
batch := NewUpdateBatch ()
28
99
batch .Put ("ns1" , "key1" , []byte ("value1" ), version .NewHeight (1 , 1 ))
@@ -60,4 +131,5 @@ func checkItrResults(t *testing.T, itr ResultsIterator, expectedResults []*Versi
60
131
lastRes , err := itr .Next ()
61
132
testutil .AssertNoError (t , err , "" )
62
133
testutil .AssertNil (t , lastRes )
134
+ itr .Close ()
63
135
}
0 commit comments