Skip to content

Commit

Permalink
statecouchdb pkg -- consistently use testify/require (hyperledger#1449)
Browse files Browse the repository at this point in the history
Signed-off-by: senthil <cendhu@gmail.com>
  • Loading branch information
cendhu authored and Sijo Cherian committed Jun 28, 2020
1 parent 51bae09 commit 967e897
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 423 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import (

"github.com/hyperledger/fabric/core/ledger/internal/version"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetRevision(t *testing.T) {
vdbEnv.init(t, nil)
defer vdbEnv.cleanup()

versionedDB, err := vdbEnv.DBProvider.GetDBHandle("test-get-revisions", nil)
assert.NoError(t, err)
require.NoError(t, err)
db := versionedDB.(*VersionedDB)

// initializing data in couchdb
batch := statedb.NewUpdateBatch()
batch.Put("ns", "key-in-db", []byte("value1"), version.NewHeight(1, 1))
batch.Put("ns", "key-in-both-db-cache", []byte("value2"), version.NewHeight(1, 2))
savePoint := version.NewHeight(1, 2)
assert.NoError(t, db.ApplyUpdates(batch, savePoint))
require.NoError(t, db.ApplyUpdates(batch, savePoint))

// load revision cache with couchDB revision number.
keys := []*statedb.CompositeKey{
Expand Down Expand Up @@ -68,16 +68,16 @@ func TestGetRevision(t *testing.T) {
}

revisionsMap, err := db.getRevisions("ns", nsUpdates)
assert.NoError(t, err)
assert.Equal(t, "revision-cache-number", revisionsMap["key-in-cache"])
assert.NotEqual(t, "", revisionsMap["key-in-db"])
assert.Equal(t, "revision-db-number", revisionsMap["key-in-both-db-cache"])
assert.Equal(t, "", revisionsMap["bad-key"])
require.NoError(t, err)
require.Equal(t, "revision-cache-number", revisionsMap["key-in-cache"])
require.NotEqual(t, "", revisionsMap["key-in-db"])
require.Equal(t, "revision-db-number", revisionsMap["key-in-both-db-cache"])
require.Equal(t, "", revisionsMap["bad-key"])

// Get revisions of non-existing nameSpace.
revisionsMap, err = db.getRevisions("bad-namespace", nsUpdates)
assert.NoError(t, err)
assert.Equal(t, "", revisionsMap["key-in-db"])
require.NoError(t, err)
require.Equal(t, "", revisionsMap["key-in-db"])

}

Expand All @@ -86,15 +86,15 @@ func TestBuildCommittersForNs(t *testing.T) {
defer vdbEnv.cleanup()

versionedDB, err := vdbEnv.DBProvider.GetDBHandle("test-build-committers-for-ns", nil)
assert.NoError(t, err)
require.NoError(t, err)
db := versionedDB.(*VersionedDB)

nsUpdates := map[string]*statedb.VersionedValue{
"bad-key": {},
}

_, err = db.buildCommittersForNs("ns", nsUpdates)
assert.EqualError(t, err, "nil version not supported")
require.EqualError(t, err, "nil version not supported")

nsUpdates = make(map[string]*statedb.VersionedValue)
// populate updates with maxBatchSize + 1.
Expand All @@ -108,10 +108,10 @@ func TestBuildCommittersForNs(t *testing.T) {
}

committers, err := db.buildCommittersForNs("ns", nsUpdates)
assert.NoError(t, err)
assert.Equal(t, 2, len(committers))
assert.Equal(t, "ns", committers[0].namespace)
assert.Equal(t, "ns", committers[1].namespace)
require.NoError(t, err)
require.Equal(t, 2, len(committers))
require.Equal(t, "ns", committers[0].namespace)
require.Equal(t, "ns", committers[1].namespace)

}

Expand All @@ -120,7 +120,7 @@ func TestBuildCommitters(t *testing.T) {
defer vdbEnv.cleanup()

versionedDB, err := vdbEnv.DBProvider.GetDBHandle("test-build-committers", nil)
assert.NoError(t, err)
require.NoError(t, err)
db := versionedDB.(*VersionedDB)

dummyHeight := version.NewHeight(1, 1)
Expand All @@ -135,41 +135,41 @@ func TestBuildCommitters(t *testing.T) {
}

committer, err := db.buildCommitters(batch)
assert.NoError(t, err)
assert.Equal(t, 3, len(committer))
require.NoError(t, err)
require.Equal(t, 3, len(committer))
for _, commit := range committer {
assert.True(t, namespaceSet[commit.namespace])
require.True(t, namespaceSet[commit.namespace])
}

badBatch := statedb.NewUpdateBatch()
badBatch.Put("bad-ns", "bad-key", []byte("bad-value"), nil)

_, err = db.buildCommitters(badBatch)
assert.EqualError(t, err, "nil version not supported")
require.EqualError(t, err, "nil version not supported")
}

func TestExecuteCommitter(t *testing.T) {
vdbEnv.init(t, nil)
defer vdbEnv.cleanup()

versionedDB, err := vdbEnv.DBProvider.GetDBHandle("test-execute-committer", nil)
assert.NoError(t, err)
require.NoError(t, err)
db := versionedDB.(*VersionedDB)

committerDB, err := db.getNamespaceDBHandle("ns")
assert.NoError(t, err)
require.NoError(t, err)
couchDocKey1, err := keyValToCouchDoc(&keyValue{
key: "key1",
revision: "",
VersionedValue: &statedb.VersionedValue{Value: []byte("value1"), Metadata: nil, Version: version.NewHeight(1, 1)},
})
assert.NoError(t, err)
require.NoError(t, err)
couchDocKey2, err := keyValToCouchDoc(&keyValue{
key: "key2",
revision: "",
VersionedValue: &statedb.VersionedValue{Value: nil, Metadata: nil, Version: version.NewHeight(1, 1)},
})
assert.NoError(t, err)
require.NoError(t, err)

committers := []*committer{
{
Expand All @@ -189,11 +189,11 @@ func TestExecuteCommitter(t *testing.T) {
}

err = db.executeCommitter(committers)
assert.NoError(t, err)
require.NoError(t, err)
vv, err := db.GetState("ns", "key1")
assert.NoError(t, err)
assert.Equal(t, vv.Value, []byte("value1"))
assert.Equal(t, vv.Version, version.NewHeight(1, 1))
require.NoError(t, err)
require.Equal(t, vv.Value, []byte("value1"))
require.Equal(t, vv.Version, version.NewHeight(1, 1))

committers = []*committer{
{
Expand All @@ -205,15 +205,15 @@ func TestExecuteCommitter(t *testing.T) {
},
}
err = db.executeCommitter(committers)
assert.EqualError(t, err, "error handling CouchDB request. Error:bad_request, Status Code:400, Reason:`docs` parameter must be an array.")
require.EqualError(t, err, "error handling CouchDB request. Error:bad_request, Status Code:400, Reason:`docs` parameter must be an array.")
}

func TestCommitUpdates(t *testing.T) {
vdbEnv.init(t, nil)
defer vdbEnv.cleanup()

versionedDB, err := vdbEnv.DBProvider.GetDBHandle("test-commitupdates", nil)
assert.NoError(t, err)
require.NoError(t, err)
db := versionedDB.(*VersionedDB)

nsUpdates := map[string]*statedb.VersionedValue{
Expand All @@ -230,9 +230,9 @@ func TestCommitUpdates(t *testing.T) {
}

committerDB, err := db.getNamespaceDBHandle("ns")
assert.NoError(t, err)
require.NoError(t, err)
couchDoc, err := keyValToCouchDoc(&keyValue{key: "key1", revision: "", VersionedValue: nsUpdates["key1"]})
assert.NoError(t, err)
require.NoError(t, err)

var tests = []struct {
committer *committer
Expand Down Expand Up @@ -273,14 +273,14 @@ func TestCommitUpdates(t *testing.T) {
for _, test := range tests {
err := test.committer.commitUpdates()
if test.expectedErr == "" {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.EqualError(t, err, test.expectedErr)
require.EqualError(t, err, test.expectedErr)
}
}

couchDoc, err = keyValToCouchDoc(&keyValue{key: "key2", revision: "", VersionedValue: nsUpdates["key2"]})
assert.NoError(t, err)
require.NoError(t, err)

committer := &committer{
db: committerDB,
Expand All @@ -290,8 +290,8 @@ func TestCommitUpdates(t *testing.T) {
cacheEnabled: true,
}

assert.Empty(t, committer.cacheKVs["key2"].AdditionalInfo)
require.Empty(t, committer.cacheKVs["key2"].AdditionalInfo)
err = committer.commitUpdates()
assert.NoError(t, err)
assert.NotEmpty(t, committer.cacheKVs["key2"].AdditionalInfo)
require.NoError(t, err)
require.NotEmpty(t, committer.cacheKVs["key2"].AdditionalInfo)
}
Loading

0 comments on commit 967e897

Please sign in to comment.