Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests for WriteBatch #144

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 138 additions & 15 deletions shim/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,17 @@ func TestGetMSPID(t *testing.T) {

func TestChaincodeStubHandlers(t *testing.T) {
var tests = []struct {
name string
resType peer.ChaincodeMessage_Type
payload []byte
testFunc func(*ChaincodeStub, *Handler, *testing.T, []byte)
name string
resType peer.ChaincodeMessage_Type
payload []byte
usePeerWriteBatch bool
testFunc func(*ChaincodeStub, *Handler, *testing.T, []byte)
}{
{
name: "Simple Response",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
name: "Simple Response",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
resp, err := s.GetState("key")
if err != nil {
Expand Down Expand Up @@ -313,13 +315,13 @@ func TestChaincodeStubHandlers(t *testing.T) {
assert.NoError(t, err)
err = s.PurgePrivateData("", "key")
assert.EqualError(t, err, "collection must not be an empty string")

},
},
{
name: "Simple Response with WriteBatch",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
name: "Simple Response with WriteBatch",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
s.StartWriteBatch()
err := s.PutState("key", payload)
Expand Down Expand Up @@ -364,6 +366,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
},
},
),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
resp, err := s.GetStateValidationParameter("key")
if err != nil {
Expand Down Expand Up @@ -392,6 +395,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
),
},
),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
resp := s.InvokeChaincode("cc", [][]byte{}, "channel")
assert.Equal(t, resp.Payload, []byte("invokechaincode"))
Expand Down Expand Up @@ -421,6 +425,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
HasMore: true,
},
),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
expectedResult := &queryresult.KV{
Key: "querykey",
Expand Down Expand Up @@ -557,6 +562,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
HasMore: false,
},
),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
expectedResult := &queryresult.KeyModification{
TxId: "txid",
Expand All @@ -575,9 +581,10 @@ func TestChaincodeStubHandlers(t *testing.T) {
},
},
{
name: "Error Conditions",
resType: peer.ChaincodeMessage_ERROR,
payload: []byte("error"),
name: "Error Conditions",
resType: peer.ChaincodeMessage_ERROR,
payload: []byte("error"),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
_, err := s.GetState("key")
assert.EqualError(t, err, string(payload))
Expand Down Expand Up @@ -619,6 +626,110 @@ func TestChaincodeStubHandlers(t *testing.T) {
assert.NoError(t, err)
},
},
{
name: "WriteBatch - Old peer (usePeerWriteBatch false)",
resType: peer.ChaincodeMessage_ERROR,
payload: []byte("error"),
usePeerWriteBatch: false,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
s.StartWriteBatch()
err := s.PutState("key", payload)
assert.ErrorContains(t, err, string(payload))
err = s.FinishWriteBatch()
assert.NoError(t, err)
},
},
{
name: "WriteBatch - different operations on the same key",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
s.StartWriteBatch()

err := s.PutState("key", payload)
assert.NoError(t, err)
err = s.DelState("key")
assert.NoError(t, err)

err = s.PutPrivateData("col", "key", payload)
assert.NoError(t, err)
err = s.DelPrivateData("col", "key")
assert.NoError(t, err)
err = s.PurgePrivateData("col", "key")
assert.NoError(t, err)

err = s.FinishWriteBatch()
assert.NoError(t, err)

checkWriteBatch(t, s.handler.chatStream, 1, 2)
},
},
{
name: "WriteBatch - State and StateValidationParameter keys are the same",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
s.StartWriteBatch()

err := s.PutState("key", payload)
assert.NoError(t, err)
err = s.SetStateValidationParameter("key", payload)
assert.NoError(t, err)

err = s.PutPrivateData("col", "key", payload)
assert.NoError(t, err)
err = s.SetPrivateDataValidationParameter("col", "key", payload)
assert.NoError(t, err)

err = s.FinishWriteBatch()
assert.NoError(t, err)

checkWriteBatch(t, s.handler.chatStream, 1, 4)
},
},
{
name: "WriteBatch - key uniqueness check c+old - col+d",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
s.StartWriteBatch()

err := s.PutPrivateData("c", "old", payload)
assert.NoError(t, err)
err = s.PutPrivateData("col", "d", payload)
assert.NoError(t, err)

err = s.FinishWriteBatch()
assert.NoError(t, err)

checkWriteBatch(t, s.handler.chatStream, 1, 2)
},
},
{
name: "WriteBatch - starting a write batch twice",
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
usePeerWriteBatch: true,
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
s.StartWriteBatch()

err := s.PutState("key", payload)
assert.NoError(t, err)

s.StartWriteBatch()

err = s.PutState("key1", payload)
assert.NoError(t, err)

err = s.FinishWriteBatch()
assert.NoError(t, err)

checkWriteBatch(t, s.handler.chatStream, 1, 2)
},
},
}

for _, test := range tests {
Expand All @@ -630,7 +741,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
cc: &mockChaincode{},
responseChannels: map[string]chan *peer.ChaincodeMessage{},
state: ready,
usePeerWriteBatch: true,
usePeerWriteBatch: test.usePeerWriteBatch,
maxSizeWriteBatch: 100,
}
stub := &ChaincodeStub{
Expand Down Expand Up @@ -659,3 +770,15 @@ func TestChaincodeStubHandlers(t *testing.T) {
})
}
}

func checkWriteBatch(t *testing.T, pcs PeerChaincodeStream, expectedSendCall int, expectedCountRecs int) {
chatStream, ok := pcs.(*mock.PeerChaincodeStream)
assert.True(t, ok)
assert.Equal(t, expectedSendCall, chatStream.SendCallCount())
peerChaincodeMsg := chatStream.SendArgsForCall(0)
assert.Equal(t, peer.ChaincodeMessage_WRITE_BATCH_STATE, peerChaincodeMsg.Type)
batch := &peer.WriteBatchState{}
err := proto.Unmarshal(peerChaincodeMsg.GetPayload(), batch)
assert.NoError(t, err)
assert.Equal(t, expectedCountRecs, len(batch.GetRec()))
}
Loading