Skip to content

Commit 1dd100a

Browse files
committed
[FAB-4061] Write unit tests for samplesscc
I wrote unit tests for core/scc/samplesyscc Code coverage is 100% Change-Id: I61d4b37c8cf06d8f18f518adaf7f66c9349f95ff Signed-off-by: Yacov Manevich <[email protected]>
1 parent eaf7f4d commit 1dd100a

File tree

2 files changed

+235
-1
lines changed

2 files changed

+235
-1
lines changed

core/scc/samplesyscc/samplesyscc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (t *SampleSysCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
8585

8686
return shim.Success(valbytes)
8787
default:
88-
jsonResp := "{\"Error\":\"Unknown functon " + f + "\"}"
88+
jsonResp := "{\"Error\":\"Unknown function " + f + "\"}"
8989
return shim.Error(jsonResp)
9090
}
9191
}
+234
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package samplesyscc
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/golang/protobuf/ptypes/timestamp"
25+
"github.com/hyperledger/fabric/core/chaincode/shim"
26+
"github.com/hyperledger/fabric/protos/peer"
27+
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/mock"
29+
)
30+
31+
func TestSampleSysCC_Init(t *testing.T) {
32+
assert.Equal(t, shim.Success(nil), (&SampleSysCC{}).Init(&mockStub{}))
33+
}
34+
35+
type testCase struct {
36+
expected peer.Response
37+
mockStub
38+
}
39+
40+
func TestSampleSysCC_Invoke(t *testing.T) {
41+
putvalIncorrectArgNum := &testCase{
42+
mockStub: mockStub{
43+
f: "putval",
44+
args: []string{},
45+
},
46+
expected: shim.Error("need 2 args (key and a value)"),
47+
}
48+
49+
putvalKeyNotFound := &testCase{
50+
mockStub: mockStub{
51+
f: "putval",
52+
args: []string{"key", "value"},
53+
},
54+
expected: shim.Error(fmt.Sprintf("{\"Error\":\"Failed to get val for %s\"}", "key")),
55+
}
56+
putvalKeyNotFound.On("GetState", mock.Anything).Return(nil, errors.New("key not found"))
57+
58+
putvalKeyKeyFoundButPutStateFailed := &testCase{
59+
mockStub: mockStub{
60+
f: "putval",
61+
args: []string{"key", "value"},
62+
},
63+
expected: shim.Error("PutState failed"),
64+
}
65+
putvalKeyKeyFoundButPutStateFailed.On("GetState", mock.Anything).Return([]byte{}, nil)
66+
putvalKeyKeyFoundButPutStateFailed.On("PutState", mock.Anything).Return(errors.New("PutState failed"))
67+
68+
putvalKeyKeyFoundAndPutStateSucceeded := &testCase{
69+
mockStub: mockStub{
70+
f: "putval",
71+
args: []string{"key", "value"},
72+
},
73+
expected: shim.Success(nil),
74+
}
75+
putvalKeyKeyFoundAndPutStateSucceeded.On("GetState", mock.Anything).Return([]byte{}, nil)
76+
putvalKeyKeyFoundAndPutStateSucceeded.On("PutState", mock.Anything).Return(nil)
77+
78+
getvalIncorrectArgNum := &testCase{
79+
mockStub: mockStub{
80+
f: "getval",
81+
args: []string{},
82+
},
83+
expected: shim.Error("Incorrect number of arguments. Expecting key to query"),
84+
}
85+
86+
getvalGetStateFails := &testCase{
87+
mockStub: mockStub{
88+
f: "getval",
89+
args: []string{"key"},
90+
},
91+
expected: shim.Error(fmt.Sprintf("{\"Error\":\"Failed to get state for %s\"}", "key")),
92+
}
93+
getvalGetStateFails.On("GetState", mock.Anything).Return(nil, errors.New("GetState failed"))
94+
95+
getvalGetStateSucceedsButNoData := &testCase{
96+
mockStub: mockStub{
97+
f: "getval",
98+
args: []string{"key"},
99+
},
100+
expected: shim.Error(fmt.Sprintf("{\"Error\":\"Nil val for %s\"}", "key")),
101+
}
102+
var nilSlice []byte
103+
getvalGetStateSucceedsButNoData.On("GetState", mock.Anything).Return(nilSlice, nil)
104+
105+
getvalGetStateSucceeds := &testCase{
106+
mockStub: mockStub{
107+
f: "getval",
108+
args: []string{"key"},
109+
},
110+
expected: shim.Success([]byte("value")),
111+
}
112+
getvalGetStateSucceeds.On("GetState", mock.Anything).Return([]byte("value"), nil)
113+
114+
unknownFunction := &testCase{
115+
mockStub: mockStub{
116+
f: "default",
117+
args: []string{},
118+
},
119+
expected: shim.Error("{\"Error\":\"Unknown function default\"}"),
120+
}
121+
122+
for _, tc := range []*testCase{
123+
putvalIncorrectArgNum,
124+
putvalKeyNotFound,
125+
putvalKeyKeyFoundButPutStateFailed,
126+
putvalKeyKeyFoundAndPutStateSucceeded,
127+
getvalIncorrectArgNum,
128+
getvalGetStateFails,
129+
getvalGetStateSucceedsButNoData,
130+
getvalGetStateSucceeds,
131+
unknownFunction,
132+
} {
133+
resp := (&SampleSysCC{}).Invoke(tc)
134+
assert.Equal(t, tc.expected, resp)
135+
}
136+
}
137+
138+
type mockStub struct {
139+
f string
140+
args []string
141+
mock.Mock
142+
}
143+
144+
func (*mockStub) GetArgs() [][]byte {
145+
panic("implement me")
146+
}
147+
148+
func (*mockStub) GetStringArgs() []string {
149+
panic("implement me")
150+
}
151+
152+
func (s *mockStub) GetFunctionAndParameters() (string, []string) {
153+
return s.f, s.args
154+
}
155+
156+
func (*mockStub) GetTxID() string {
157+
panic("implement me")
158+
}
159+
160+
func (*mockStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) peer.Response {
161+
panic("implement me")
162+
}
163+
164+
func (s *mockStub) GetState(key string) ([]byte, error) {
165+
args := s.Called(key)
166+
if args.Get(1) == nil {
167+
return args.Get(0).([]byte), nil
168+
}
169+
return nil, args.Get(1).(error)
170+
}
171+
172+
func (s *mockStub) PutState(key string, value []byte) error {
173+
args := s.Called(key)
174+
if args.Get(0) == nil {
175+
return nil
176+
}
177+
return args.Get(0).(error)
178+
}
179+
180+
func (*mockStub) DelState(key string) error {
181+
panic("implement me")
182+
}
183+
184+
func (*mockStub) GetStateByRange(startKey, endKey string) (shim.StateQueryIteratorInterface, error) {
185+
panic("implement me")
186+
}
187+
188+
func (*mockStub) GetStateByPartialCompositeKey(objectType string, keys []string) (shim.StateQueryIteratorInterface, error) {
189+
panic("implement me")
190+
}
191+
192+
func (*mockStub) CreateCompositeKey(objectType string, attributes []string) (string, error) {
193+
panic("implement me")
194+
}
195+
196+
func (*mockStub) SplitCompositeKey(compositeKey string) (string, []string, error) {
197+
panic("implement me")
198+
}
199+
200+
func (*mockStub) GetQueryResult(query string) (shim.StateQueryIteratorInterface, error) {
201+
panic("implement me")
202+
}
203+
204+
func (*mockStub) GetHistoryForKey(key string) (shim.HistoryQueryIteratorInterface, error) {
205+
panic("implement me")
206+
}
207+
208+
func (*mockStub) GetCreator() ([]byte, error) {
209+
panic("implement me")
210+
}
211+
212+
func (*mockStub) GetTransient() (map[string][]byte, error) {
213+
panic("implement me")
214+
}
215+
216+
func (*mockStub) GetBinding() ([]byte, error) {
217+
panic("implement me")
218+
}
219+
220+
func (*mockStub) GetSignedProposal() (*peer.SignedProposal, error) {
221+
panic("implement me")
222+
}
223+
224+
func (*mockStub) GetArgsSlice() ([]byte, error) {
225+
panic("implement me")
226+
}
227+
228+
func (*mockStub) GetTxTimestamp() (*timestamp.Timestamp, error) {
229+
panic("implement me")
230+
}
231+
232+
func (*mockStub) SetEvent(name string, payload []byte) error {
233+
panic("implement me")
234+
}

0 commit comments

Comments
 (0)