-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-9691] Extract PendingQueryResult
Extract and explicitly unit test this object. Change-Id: I2585b2582ab5bfbd6a6f6c5791323e7de05835be Signed-off-by: Matthew Sykes <[email protected]>
- Loading branch information
Showing
6 changed files
with
101 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package chaincode | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
commonledger "github.com/hyperledger/fabric/common/ledger" | ||
pb "github.com/hyperledger/fabric/protos/peer" | ||
) | ||
|
||
type PendingQueryResult struct { | ||
batch []*pb.QueryResultBytes | ||
} | ||
|
||
func (p *PendingQueryResult) Cut() []*pb.QueryResultBytes { | ||
batch := p.batch | ||
p.batch = nil | ||
return batch | ||
} | ||
|
||
func (p *PendingQueryResult) Add(queryResult commonledger.QueryResult) error { | ||
queryResultBytes, err := proto.Marshal(queryResult.(proto.Message)) | ||
if err != nil { | ||
chaincodeLogger.Errorf("failed to marshal query result: %s", err) | ||
return err | ||
} | ||
p.batch = append(p.batch, &pb.QueryResultBytes{ResultBytes: queryResultBytes}) | ||
return nil | ||
} | ||
|
||
func (p *PendingQueryResult) Size() int { | ||
return len(p.batch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package chaincode_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/hyperledger/fabric/core/chaincode" | ||
"github.com/hyperledger/fabric/protos/ledger/queryresult" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPendingQueryResult(t *testing.T) { | ||
pqr := chaincode.PendingQueryResult{} | ||
assert.Equal(t, 0, pqr.Size()) | ||
assert.Nil(t, pqr.Cut()) | ||
|
||
for i := 1; i <= 5; i++ { | ||
kv := &queryresult.KV{Key: fmt.Sprintf("key-%d", i)} | ||
err := pqr.Add(kv) | ||
assert.NoError(t, err) | ||
assert.Equal(t, i, pqr.Size()) | ||
} | ||
|
||
results := pqr.Cut() | ||
assert.Len(t, results, 5) | ||
assert.Equal(t, 0, pqr.Size()) | ||
for i, bytes := range results { | ||
var kv queryresult.KV | ||
err := proto.Unmarshal(bytes.ResultBytes, &kv) | ||
assert.NoError(t, err) | ||
assert.Equal(t, kv.Key, fmt.Sprintf("key-%d", i+1)) | ||
} | ||
} | ||
|
||
func TestPendingQueryResultBadQueryResult(t *testing.T) { | ||
pqr := chaincode.PendingQueryResult{} | ||
err := pqr.Add(brokenProto{}) | ||
assert.EqualError(t, err, "marshal-failed") | ||
} | ||
|
||
type brokenProto struct{} | ||
|
||
func (brokenProto) Reset() {} | ||
func (brokenProto) String() string { return "" } | ||
func (brokenProto) ProtoMessage() {} | ||
func (brokenProto) Marshal() ([]byte, error) { return nil, errors.New("marshal-failed") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters