Skip to content

Commit

Permalink
[FAB-3404] Improve unit test for txmgmt/statedb
Browse files Browse the repository at this point in the history
Improves the test coverage from 64% to 100%.

Change-Id: I1c058ba2d3250ef42b89898e3f9f3889d33d7562
Signed-off-by: senthil <cendhu@gmail.com>
  • Loading branch information
cendhu committed Apr 28, 2017
1 parent ecc29dd commit 64a237c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions core/ledger/kvledger/txmgmt/statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,83 @@ limitations under the License.
package statedb

import (
"sort"
"testing"

"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
)

func TestPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("Nil value to Put() did not panic\n")
}
}()

batch := NewUpdateBatch()
// The following call to Put() should result in panic
batch.Put("ns1", "key1", nil, nil)
}

//Test Put(), Get(), and Delete()
func TestPutGetDeleteExistsGetUpdates(t *testing.T) {
batch := NewUpdateBatch()
batch.Put("ns1", "key1", []byte("value1"), version.NewHeight(1, 1))

//Get() should return above inserted <k,v> pair
actualVersionedValue := batch.Get("ns1", "key1")
testutil.AssertEquals(t, actualVersionedValue, &VersionedValue{Value: []byte("value1"), Version: version.NewHeight(1, 1)})
//Exists() should return false as key2 does not exist
actualResult := batch.Exists("ns1", "key2")
expectedResult := false
testutil.AssertEquals(t, actualResult, expectedResult)

//Exists() should return false as ns3 does not exist
actualResult = batch.Exists("ns3", "key2")
expectedResult = false
testutil.AssertEquals(t, actualResult, expectedResult)

//Get() should return nill as key2 does not exist
actualVersionedValue = batch.Get("ns1", "key2")
testutil.AssertNil(t, actualVersionedValue)
//Get() should return nill as ns3 does not exist
actualVersionedValue = batch.Get("ns3", "key2")
testutil.AssertNil(t, actualVersionedValue)

batch.Put("ns1", "key2", []byte("value2"), version.NewHeight(1, 2))
//Exists() should return true as key2 exists
actualResult = batch.Exists("ns1", "key2")
expectedResult = true
testutil.AssertEquals(t, actualResult, expectedResult)

//GetUpdatedNamespaces should return 3 namespaces
batch.Put("ns2", "key2", []byte("value2"), version.NewHeight(1, 2))
batch.Put("ns3", "key2", []byte("value2"), version.NewHeight(1, 2))
actualNamespaces := batch.GetUpdatedNamespaces()
sort.Strings(actualNamespaces)
expectedNamespaces := []string{"ns1", "ns2", "ns3"}
testutil.AssertEquals(t, actualNamespaces, expectedNamespaces)

//GetUpdates should return two VersionedValues for the namespace ns1
expectedUpdates := make(map[string]*VersionedValue)
expectedUpdates["key1"] = &VersionedValue{Value: []byte("value1"), Version: version.NewHeight(1, 1)}
expectedUpdates["key2"] = &VersionedValue{Value: []byte("value2"), Version: version.NewHeight(1, 2)}
actualUpdates := batch.GetUpdates("ns1")
testutil.AssertEquals(t, actualUpdates, expectedUpdates)

actualUpdates = batch.GetUpdates("ns4")
testutil.AssertNil(t, actualUpdates)

//Delete the above inserted <k,v> pair
batch.Delete("ns1", "key2", version.NewHeight(1, 2))
//Exists() should return false as key2 is deleted
actualResult = batch.Exists("ns1", "key2")
expectedResult = true
testutil.AssertEquals(t, actualResult, expectedResult)

}

func TestUpdateBatchIterator(t *testing.T) {
batch := NewUpdateBatch()
batch.Put("ns1", "key1", []byte("value1"), version.NewHeight(1, 1))
Expand Down Expand Up @@ -60,4 +131,5 @@ func checkItrResults(t *testing.T, itr ResultsIterator, expectedResults []*Versi
lastRes, err := itr.Next()
testutil.AssertNoError(t, err, "")
testutil.AssertNil(t, lastRes)
itr.Close()
}

0 comments on commit 64a237c

Please sign in to comment.