Skip to content

Commit

Permalink
[FAB-5853] Add additional unit tests for GetVersion
Browse files Browse the repository at this point in the history
GetVersion() was introduced in FAB-2960 and requires additional unit tests.

Tests were added to verify retrieval by namespace and key.

Negative test added to test for response for non-existent namespace and key.

Change-Id: Ia2aa96b63cc62a5d97e672375fe13de948ca5305
Signed-off-by: Chris Elder <chris.elder@us.ibm.com>
  • Loading branch information
Chris Elder committed Sep 6, 2017
1 parent 93f3c9b commit eea9396
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
58 changes: 58 additions & 0 deletions core/ledger/kvledger/txmgmt/statedb/commontests/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,61 @@ func TestQuery(t *testing.T, dbProvider statedb.VersionedDBProvider) {
testutil.AssertNil(t, queryResult2)

}

// TestGetVersion tests retrieving the version by namespace and key
func TestGetVersion(t *testing.T, dbProvider statedb.VersionedDBProvider) {

db, err := dbProvider.GetDBHandle("testgetversion")
testutil.AssertNoError(t, err, "")

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)
savePoint := version.NewHeight(1, 5)
err = db.ApplyUpdates(batch, savePoint)
testutil.AssertNoError(t, err, "")

//check to see if the bulk optimizable interface is supported (couchdb)
if bulkdb, ok := db.(statedb.BulkOptimizable); ok {
//clear the cached versions, this will force a read when getVerion is called
bulkdb.ClearCachedVersions()
}

//retrieve a version by namespace and key
resp, err := db.GetVersion("ns", "key2")
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, resp, version.NewHeight(1, 2))

//attempt to retrieve an non-existent namespace and key
resp, err = db.GetVersion("ns2", "key2")
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, resp, nil)

//check to see if the bulk optimizable interface is supported (couchdb)
if bulkdb, ok := db.(statedb.BulkOptimizable); ok {

//clear the cached versions, this will force a read when getVerion is called
bulkdb.ClearCachedVersions()

// initialize a key list
loadKeys := []*statedb.CompositeKey{}
//create a composite key and add to the key list
compositeKey := statedb.CompositeKey{Namespace: "ns", Key: "key3"}
loadKeys = append(loadKeys, &compositeKey)
//load the committed versions
bulkdb.LoadCommittedVersions(loadKeys)

//retrieve a version by namespace and key
resp, err := db.GetVersion("ns", "key3")
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, resp, version.NewHeight(1, 3))

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ func TestGetStateMultipleKeys(t *testing.T) {
commontests.TestGetStateMultipleKeys(t, env.DBProvider)
}
}

func TestGetVersion(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {
env := NewTestVDBEnv(t)
env.Cleanup("testgetversion")
defer env.Cleanup("testgetversion")
commontests.TestGetVersion(t, env.DBProvider)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ func TestGetStateMultipleKeys(t *testing.T) {
defer env.Cleanup()
commontests.TestGetStateMultipleKeys(t, env.DBProvider)
}

func TestGetVersion(t *testing.T) {
env := NewTestVDBEnv(t)
defer env.Cleanup()
commontests.TestGetVersion(t, env.DBProvider)
}

0 comments on commit eea9396

Please sign in to comment.