Skip to content

Commit

Permalink
[FAB-10686] testutil->testify txmgmt/rwsetutil
Browse files Browse the repository at this point in the history
Change-Id: Iaa4a568bed9ad4de81b31caf09fe4f9c7eaf8747
Signed-off-by: Matthew Sykes <[email protected]>
  • Loading branch information
sykesm committed Sep 22, 2018
1 parent 218bc06 commit 96a606f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 76 deletions.
59 changes: 32 additions & 27 deletions core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ import (

"github.com/golang/protobuf/proto"
bccspfactory "github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
"github.com/stretchr/testify/assert"
)

func TestQueryResultHelper_NoResults(t *testing.T) {
helper, _ := NewRangeQueryResultsHelper(true, 3)
r, h, err := helper.Done()
testutil.AssertNoError(t, err, "")
testutil.AssertNil(t, h)
testutil.AssertNil(t, r)
assert.NoError(t, err)
assert.Nil(t, h)
assert.Nil(t, r)
}

func TestQueryResultHelper_HashNotEnabled(t *testing.T) {
kvReads := buildTestKVReads(t, 5)
r, h := buildTestResults(t, false, 3, kvReads)
testutil.AssertNil(t, h)
testutil.AssertEquals(t, r, kvReads)
assert.Nil(t, h)
assert.Equal(t, kvReads, r)
}

func TestQueryResultHelper_ResultsNoMoreThanMaxDegree(t *testing.T) {
maxDegree := 3
numResults := 3
kvReads := buildTestKVReads(t, numResults)
r, h := buildTestResults(t, true, maxDegree, kvReads)
testutil.AssertNil(t, h)
testutil.AssertEquals(t, r, kvReads)
assert.Nil(t, h)
assert.Equal(t, kvReads, r)
}

func TestQueryResultHelper_Hash_OneLevel(t *testing.T) {
Expand All @@ -59,11 +59,12 @@ func TestQueryResultHelper_Hash_OneLevel(t *testing.T) {
level1_1 := computeTestHashKVReads(t, kvReads[0:4])
level1_2 := computeTestHashKVReads(t, kvReads[4:8])
level1_3 := computeTestHashKVReads(t, kvReads[8:])
testutil.AssertNil(t, r)
testutil.AssertEquals(t, h, &kvrwset.QueryReadsMerkleSummary{
assert.Nil(t, r)
assert.Equal(t, &kvrwset.QueryReadsMerkleSummary{
MaxDegree: uint32(maxDegree),
MaxLevel: 1,
MaxLevelHashes: hashesToBytes([]Hash{level1_1, level1_2, level1_3})})
MaxLevelHashes: hashesToBytes([]Hash{level1_1, level1_2, level1_3})}, h)

}

func TestQueryResultHelper_Hash_TwoLevel(t *testing.T) {
Expand All @@ -80,11 +81,12 @@ func TestQueryResultHelper_Hash_TwoLevel(t *testing.T) {

level2_1 := computeTestCombinedHash(t, level1_1, level1_2, level1_3, level1_4)
level2_2 := computeTestCombinedHash(t, level1_5, level1_6, level1_7)
testutil.AssertNil(t, r)
testutil.AssertEquals(t, h, &kvrwset.QueryReadsMerkleSummary{
assert.Nil(t, r)
assert.Equal(t, &kvrwset.QueryReadsMerkleSummary{
MaxDegree: uint32(maxDegree),
MaxLevel: 2,
MaxLevelHashes: hashesToBytes([]Hash{level2_1, level2_2})})
MaxLevelHashes: hashesToBytes([]Hash{level2_1, level2_2})}, h)

}

func TestQueryResultHelper_Hash_ThreeLevel(t *testing.T) {
Expand Down Expand Up @@ -116,11 +118,12 @@ func TestQueryResultHelper_Hash_ThreeLevel(t *testing.T) {

level3_1 := computeTestCombinedHash(t, level2_1, level2_2, level2_3, level2_4)
level3_2 := level1_17
testutil.AssertNil(t, r)
testutil.AssertEquals(t, h, &kvrwset.QueryReadsMerkleSummary{
assert.Nil(t, r)
assert.Equal(t, &kvrwset.QueryReadsMerkleSummary{
MaxDegree: uint32(maxDegree),
MaxLevel: 3,
MaxLevelHashes: hashesToBytes([]Hash{level3_1, level3_2})})
MaxLevelHashes: hashesToBytes([]Hash{level3_1, level3_2})}, h)

}

func TestQueryResultHelper_Hash_MaxLevelIncrementNeededInDone(t *testing.T) {
Expand All @@ -142,11 +145,12 @@ func TestQueryResultHelper_Hash_MaxLevelIncrementNeededInDone(t *testing.T) {

level3_1 := computeTestCombinedHash(t, level2_1, level2_2, level2_3)

testutil.AssertNil(t, r)
testutil.AssertEquals(t, h, &kvrwset.QueryReadsMerkleSummary{
assert.Nil(t, r)
assert.Equal(t, &kvrwset.QueryReadsMerkleSummary{
MaxDegree: uint32(maxDegree),
MaxLevel: 3,
MaxLevelHashes: hashesToBytes([]Hash{level3_1})})
MaxLevelHashes: hashesToBytes([]Hash{level3_1})}, h)

}

func TestQueryResultHelper_Hash_FirstLevelSkipNeededInDone(t *testing.T) {
Expand Down Expand Up @@ -178,11 +182,12 @@ func TestQueryResultHelper_Hash_FirstLevelSkipNeededInDone(t *testing.T) {
level3_1 := computeTestCombinedHash(t, level2_1, level2_2, level2_3)
level3_2 := computeTestCombinedHash(t, level2_4, level2_5)

testutil.AssertNil(t, r)
testutil.AssertEquals(t, h, &kvrwset.QueryReadsMerkleSummary{
assert.Nil(t, r)
assert.Equal(t, &kvrwset.QueryReadsMerkleSummary{
MaxDegree: uint32(maxDegree),
MaxLevel: 3,
MaxLevelHashes: hashesToBytes([]Hash{level3_1, level3_2})})
MaxLevelHashes: hashesToBytes([]Hash{level3_1, level3_2})}, h)

}

func buildTestResults(t *testing.T, enableHashing bool, maxDegree int, kvReads []*kvrwset.KVRead) ([]*kvrwset.KVRead, *kvrwset.QueryReadsMerkleSummary) {
Expand All @@ -191,7 +196,7 @@ func buildTestResults(t *testing.T, enableHashing bool, maxDegree int, kvReads [
helper.AddResult(kvRead)
}
r, h, err := helper.Done()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
return r, h
}

Expand All @@ -207,14 +212,14 @@ func computeTestHashKVReads(t *testing.T, kvReads []*kvrwset.KVRead) Hash {
queryReads := &kvrwset.QueryReads{}
queryReads.KvReads = kvReads
b, err := proto.Marshal(queryReads)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
h, err := bccspfactory.GetDefault().Hash(b, hashOpts)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
return h
}

func computeTestCombinedHash(t *testing.T, hashes ...Hash) Hash {
h, err := computeCombinedHash(hashes)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
return h
}
15 changes: 7 additions & 8 deletions core/ledger/kvledger/txmgmt/rwsetutil/rwset_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/ledger/rwset"
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestTxSimulationResultWithOnlyPubData(t *testing.T) {
rwSetBuilder.AddToWriteSet("ns2", "key3", []byte("value3"))

txSimulationResults, err := rwSetBuilder.GetTxSimulationResults()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)

ns1KVRWSet := &kvrwset.KVRWSet{
Reads: []*kvrwset.KVRead{NewKVRead("key1", version.NewHeight(1, 1)), NewKVRead("key2", version.NewHeight(1, 2))},
Expand All @@ -76,9 +75,9 @@ func TestTxSimulationResultWithOnlyPubData(t *testing.T) {
}

expectedTxRWSet := &rwset.TxReadWriteSet{NsRwset: []*rwset.NsReadWriteSet{ns1RWSet, ns2RWSet}}
testutil.AssertEquals(t, txSimulationResults.PubSimulationResults, expectedTxRWSet)
testutil.AssertNil(t, txSimulationResults.PvtSimulationResults)
testutil.AssertNil(t, txSimulationResults.PubSimulationResults.NsRwset[0].CollectionHashedRwset)
assert.Equal(t, expectedTxRWSet, txSimulationResults.PubSimulationResults)
assert.Nil(t, txSimulationResults.PvtSimulationResults)
assert.Nil(t, txSimulationResults.PubSimulationResults.NsRwset[0].CollectionHashedRwset)
}

func TestTxSimulationResultWithPvtData(t *testing.T) {
Expand All @@ -99,7 +98,7 @@ func TestTxSimulationResultWithPvtData(t *testing.T) {
rwSetBuilder.AddToPvtAndHashedWriteSet("ns2", "coll2", "key1", []byte("pvt-ns2-coll2-key1-value"))

actualSimRes, err := rwSetBuilder.GetTxSimulationResults()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)

///////////////////////////////////////////////////////
// construct the expected pvt rwset and compare with the one present in the txSimulationResults
Expand Down Expand Up @@ -236,7 +235,7 @@ func TestTxSimulationResultWithMetadata(t *testing.T) {
rwSetBuilder.AddToHashedMetadataWriteSet("ns1", "coll2", "key1", nil) // pvt-data metadata delete

actualSimRes, err := rwSetBuilder.GetTxSimulationResults()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)

// construct the expected pvt rwset and compare with the one present in the txSimulationResults
pvtNs1Coll1 := &kvrwset.KVRWSet{
Expand Down Expand Up @@ -355,6 +354,6 @@ func constructTestPvtKVWriteHash(t *testing.T, key string, value []byte) *kvrwse

func serializeTestProtoMsg(t *testing.T, protoMsg proto.Message) []byte {
msgBytes, err := proto.Marshal(protoMsg)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
return msgBytes
}
85 changes: 44 additions & 41 deletions core/ledger/kvledger/txmgmt/rwsetutil/rwset_proto_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
"github.com/kr/pretty"
"github.com/stretchr/testify/assert"
)

func TestTxRWSetMarshalUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -59,69 +60,69 @@ func TestTxRWSetMarshalUnmarshal(t *testing.T) {
}

protoBytes, err := txRwSet.ToProtoBytes()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
txRwSet1 := &TxRwSet{}
testutil.AssertNoError(t, txRwSet1.FromProtoBytes(protoBytes), "")
assert.NoError(t, txRwSet1.FromProtoBytes(protoBytes))
t.Logf("txRwSet=%s, txRwSet1=%s", spew.Sdump(txRwSet), spew.Sdump(txRwSet1))
testutil.AssertEquals(t, len(txRwSet.NsRwSets), len(txRwSet1.NsRwSets))
assert.Equal(t, len(txRwSet1.NsRwSets), len(txRwSet.NsRwSets))
for i, rwset := range txRwSet.NsRwSets {
testutil.AssertEquals(t, rwset.NameSpace, txRwSet1.NsRwSets[i].NameSpace)
testutil.AssertEquals(t, rwset.KvRwSet, txRwSet1.NsRwSets[i].KvRwSet)
testutil.AssertEquals(t, rwset.CollHashedRwSets, txRwSet1.NsRwSets[i].CollHashedRwSets)
assert.Equal(t, txRwSet1.NsRwSets[i].NameSpace, rwset.NameSpace)
assert.True(t, proto.Equal(txRwSet1.NsRwSets[i].KvRwSet, rwset.KvRwSet), "proto messages are not equal")
assert.Equal(t, txRwSet1.NsRwSets[i].CollHashedRwSets, rwset.CollHashedRwSets)
}
}

func TestTxRwSetConversion(t *testing.T) {
txRwSet := sampleTxRwSet()
protoMsg, err := txRwSet.toProtoMsg()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
txRwSet1, err := TxRwSetFromProtoMsg(protoMsg)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
t.Logf("txRwSet=%s, txRwSet1=%s", spew.Sdump(txRwSet), spew.Sdump(txRwSet1))
testutil.AssertEquals(t, len(txRwSet.NsRwSets), len(txRwSet1.NsRwSets))
assert.Equal(t, len(txRwSet1.NsRwSets), len(txRwSet.NsRwSets))
for i, rwset := range txRwSet.NsRwSets {
testutil.AssertEquals(t, rwset.NameSpace, txRwSet1.NsRwSets[i].NameSpace)
testutil.AssertEquals(t, rwset.KvRwSet, txRwSet1.NsRwSets[i].KvRwSet)
assert.Equal(t, txRwSet1.NsRwSets[i].NameSpace, rwset.NameSpace)
assert.True(t, proto.Equal(txRwSet1.NsRwSets[i].KvRwSet, rwset.KvRwSet), "proto messages are not equal")
for j, hashedRwSet := range rwset.CollHashedRwSets {
testutil.AssertEquals(t, hashedRwSet.CollectionName, txRwSet1.NsRwSets[i].CollHashedRwSets[j].CollectionName)
testutil.AssertEquals(t, hashedRwSet.HashedRwSet, txRwSet1.NsRwSets[i].CollHashedRwSets[j].HashedRwSet)
testutil.AssertEquals(t, hashedRwSet.PvtRwSetHash, txRwSet1.NsRwSets[i].CollHashedRwSets[j].PvtRwSetHash)
assert.Equal(t, txRwSet1.NsRwSets[i].CollHashedRwSets[j].CollectionName, hashedRwSet.CollectionName)
assert.True(t, proto.Equal(txRwSet1.NsRwSets[i].CollHashedRwSets[j].HashedRwSet, hashedRwSet.HashedRwSet), "proto messages are not equal")
assert.Equal(t, txRwSet1.NsRwSets[i].CollHashedRwSets[j].PvtRwSetHash, hashedRwSet.PvtRwSetHash)
}
}
}

func TestNsRwSetConversion(t *testing.T) {
nsRwSet := sampleNsRwSet("ns-1")
protoMsg, err := nsRwSet.toProtoMsg()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
nsRwSet1, err := nsRwSetFromProtoMsg(protoMsg)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
t.Logf("nsRwSet=%s, nsRwSet1=%s", spew.Sdump(nsRwSet), spew.Sdump(nsRwSet1))
testutil.AssertEquals(t, nsRwSet.NameSpace, nsRwSet1.NameSpace)
testutil.AssertEquals(t, nsRwSet.KvRwSet, nsRwSet1.KvRwSet)
assert.Equal(t, nsRwSet1.NameSpace, nsRwSet.NameSpace)
assert.True(t, proto.Equal(nsRwSet1.KvRwSet, nsRwSet.KvRwSet), "proto messages are not equal")
for j, hashedRwSet := range nsRwSet.CollHashedRwSets {
testutil.AssertEquals(t, hashedRwSet.CollectionName, nsRwSet1.CollHashedRwSets[j].CollectionName)
testutil.AssertEquals(t, hashedRwSet.HashedRwSet, nsRwSet1.CollHashedRwSets[j].HashedRwSet)
testutil.AssertEquals(t, hashedRwSet.PvtRwSetHash, nsRwSet1.CollHashedRwSets[j].PvtRwSetHash)
assert.Equal(t, nsRwSet1.CollHashedRwSets[j].CollectionName, hashedRwSet.CollectionName)
assert.True(t, proto.Equal(nsRwSet1.CollHashedRwSets[j].HashedRwSet, hashedRwSet.HashedRwSet), "proto messages are not equal")
assert.Equal(t, nsRwSet1.CollHashedRwSets[j].PvtRwSetHash, hashedRwSet.PvtRwSetHash)
}
}

func TestNsRWSetConversionNoCollHashedRWs(t *testing.T) {
nsRwSet := sampleNsRwSetWithNoCollHashedRWs("ns-1")
protoMsg, err := nsRwSet.toProtoMsg()
testutil.AssertNoError(t, err, "")
testutil.AssertNil(t, protoMsg.CollectionHashedRwset)
assert.NoError(t, err)
assert.Nil(t, protoMsg.CollectionHashedRwset)
}

func TestCollHashedRwSetConversion(t *testing.T) {
collHashedRwSet := sampleCollHashedRwSet("coll-1")
protoMsg, err := collHashedRwSet.toProtoMsg()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
collHashedRwSet1, err := collHashedRwSetFromProtoMsg(protoMsg)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, collHashedRwSet1.CollectionName, collHashedRwSet.CollectionName)
testutil.AssertEquals(t, collHashedRwSet1.HashedRwSet, collHashedRwSet.HashedRwSet)
testutil.AssertEquals(t, collHashedRwSet1.PvtRwSetHash, collHashedRwSet.PvtRwSetHash)
assert.NoError(t, err)
assert.Equal(t, collHashedRwSet.CollectionName, collHashedRwSet1.CollectionName)
assert.True(t, proto.Equal(collHashedRwSet.HashedRwSet, collHashedRwSet1.HashedRwSet), "proto messages are not equal")
assert.Equal(t, collHashedRwSet.PvtRwSetHash, collHashedRwSet1.PvtRwSetHash)
}

func sampleTxRwSet() *TxRwSet {
Expand All @@ -132,8 +133,10 @@ func sampleTxRwSet() *TxRwSet {
}

func sampleNsRwSet(ns string) *NsRwSet {
nsRwSet := &NsRwSet{NameSpace: ns,
KvRwSet: sampleKvRwSet()}
nsRwSet := &NsRwSet{
NameSpace: ns,
KvRwSet: sampleKvRwSet(),
}
nsRwSet.CollHashedRwSets = append(nsRwSet.CollHashedRwSets, sampleCollHashedRwSet("coll-1"))
nsRwSet.CollHashedRwSets = append(nsRwSet.CollHashedRwSets, sampleCollHashedRwSet("coll-2"))
return nsRwSet
Expand Down Expand Up @@ -184,16 +187,16 @@ func sampleCollHashedRwSet(collectionName string) *CollHashedRwSet {
func TestTxPvtRwSetConversion(t *testing.T) {
txPvtRwSet := sampleTxPvtRwSet()
protoMsg, err := txPvtRwSet.toProtoMsg()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
txPvtRwSet1, err := TxPvtRwSetFromProtoMsg(protoMsg)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
t.Logf("txPvtRwSet=%s, txPvtRwSet1=%s, Diff:%s", spew.Sdump(txPvtRwSet), spew.Sdump(txPvtRwSet1), pretty.Diff(txPvtRwSet, txPvtRwSet1))
testutil.AssertEquals(t, len(txPvtRwSet.NsPvtRwSet), len(txPvtRwSet1.NsPvtRwSet))
assert.Equal(t, len(txPvtRwSet1.NsPvtRwSet), len(txPvtRwSet.NsPvtRwSet))
for i, rwset := range txPvtRwSet.NsPvtRwSet {
testutil.AssertEquals(t, rwset.NameSpace, txPvtRwSet1.NsPvtRwSet[i].NameSpace)
assert.Equal(t, txPvtRwSet1.NsPvtRwSet[i].NameSpace, rwset.NameSpace)
for j, hashedRwSet := range rwset.CollPvtRwSets {
testutil.AssertEquals(t, hashedRwSet.CollectionName, txPvtRwSet1.NsPvtRwSet[i].CollPvtRwSets[j].CollectionName)
testutil.AssertEquals(t, hashedRwSet.KvRwSet, txPvtRwSet1.NsPvtRwSet[i].CollPvtRwSets[j].KvRwSet)
assert.Equal(t, txPvtRwSet1.NsPvtRwSet[i].CollPvtRwSets[j].CollectionName, hashedRwSet.CollectionName)
assert.True(t, proto.Equal(txPvtRwSet1.NsPvtRwSet[i].CollPvtRwSets[j].KvRwSet, hashedRwSet.KvRwSet), "proto messages are not equal")
}
}
}
Expand Down Expand Up @@ -224,10 +227,10 @@ func TestVersionConversion(t *testing.T) {
protoVer := &kvrwset.Version{BlockNum: 5, TxNum: 2}
internalVer := version.NewHeight(5, 2)
// convert proto to internal
testutil.AssertNil(t, NewVersion(nil))
testutil.AssertEquals(t, NewVersion(protoVer), internalVer)
assert.Nil(t, NewVersion(nil))
assert.Equal(t, internalVer, NewVersion(protoVer))

// convert internal to proto
testutil.AssertNil(t, newProtoVersion(nil))
testutil.AssertEquals(t, newProtoVersion(internalVer), protoVer)
assert.Nil(t, newProtoVersion(nil))
assert.Equal(t, protoVer, newProtoVersion(internalVer))
}

0 comments on commit 96a606f

Please sign in to comment.