github.com/hyperledger-labs/bdls@v2.1.1+incompatible/core/chaincode/pending_query_result_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package chaincode_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	"github.com/hyperledger/fabric-protos-go/ledger/queryresult"
    14  	"github.com/hyperledger/fabric/core/chaincode"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	"github.com/pkg/errors"
    18  )
    19  
    20  var _ = Describe("PendingQueryResult", func() {
    21  	var pqr *chaincode.PendingQueryResult
    22  
    23  	BeforeEach(func() {
    24  		pqr = &chaincode.PendingQueryResult{}
    25  	})
    26  
    27  	Describe("Size", func() {
    28  		It("returns the number of results in the batch", func() {
    29  			Expect(pqr.Size()).To(Equal(0))
    30  
    31  			for i := 1; i <= 10; i++ {
    32  				kv := &queryresult.KV{Key: fmt.Sprintf("key-%d", i)}
    33  				err := pqr.Add(kv)
    34  				Expect(err).NotTo(HaveOccurred())
    35  				Expect(pqr.Size()).To(Equal(i))
    36  			}
    37  		})
    38  
    39  		Describe("Add and Cut", func() {
    40  			It("tracks the query results", func() {
    41  				By("adding the results")
    42  				for i := 1; i <= 10; i++ {
    43  					kv := &queryresult.KV{Key: fmt.Sprintf("key-%d", i)}
    44  					err := pqr.Add(kv)
    45  					Expect(err).NotTo(HaveOccurred())
    46  					Expect(pqr.Size()).To(Equal(i))
    47  				}
    48  
    49  				By("cutting the results")
    50  				results := pqr.Cut()
    51  				Expect(results).To(HaveLen(10))
    52  				for i, result := range results {
    53  					var kv queryresult.KV
    54  					err := proto.Unmarshal(result.ResultBytes, &kv)
    55  					Expect(err).NotTo(HaveOccurred())
    56  					Expect(kv.Key).To(Equal(fmt.Sprintf("key-%d", i+1)))
    57  				}
    58  			})
    59  
    60  			Context("when the result cannot be marshaled", func() {
    61  				It("returns an error", func() {
    62  					err := pqr.Add(brokenProto{})
    63  					Expect(err).To(MatchError("marshal-failed"))
    64  				})
    65  			})
    66  		})
    67  
    68  		Describe("Cut", func() {
    69  			BeforeEach(func() {
    70  				for i := 1; i <= 10; i++ {
    71  					kv := &queryresult.KV{Key: fmt.Sprintf("key-%d", i)}
    72  					err := pqr.Add(kv)
    73  					Expect(err).NotTo(HaveOccurred())
    74  					Expect(pqr.Size()).To(Equal(i))
    75  				}
    76  			})
    77  
    78  			It("resets the size to 0", func() {
    79  				Expect(pqr.Size()).To(Equal(10))
    80  				pqr.Cut()
    81  				Expect(pqr.Size()).To(Equal(0))
    82  			})
    83  
    84  			Context("when cutting an empty batch", func() {
    85  				It("returns a nil batch", func() {
    86  					pqr.Cut()
    87  					results := pqr.Cut()
    88  					Expect(results).To(BeNil())
    89  				})
    90  			})
    91  		})
    92  	})
    93  })
    94  
    95  type brokenProto struct{}
    96  
    97  func (brokenProto) Reset()                   {}
    98  func (brokenProto) String() string           { return "" }
    99  func (brokenProto) ProtoMessage()            {}
   100  func (brokenProto) Marshal() ([]byte, error) { return nil, errors.New("marshal-failed") }