@@ -73,7 +73,7 @@ describe('HLFQueryHandler', () => {
73
73
74
74
it ( 'should not switch to another peer if peer returns a payload which is an error' , async ( ) => {
75
75
const response = new Error ( 'my chaincode error' ) ;
76
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
76
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
77
77
let qspSpy = sinon . spy ( queryHandler , 'querySinglePeer' ) ;
78
78
try {
79
79
await queryHandler . queryChaincode ( mockTransactionID , 'myfunc' , [ 'arg1' , 'arg2' ] ) ;
@@ -175,11 +175,11 @@ describe('HLFQueryHandler', () => {
175
175
176
176
it ( 'should query a single peer' , async ( ) => {
177
177
const response = Buffer . from ( 'hello world' ) ;
178
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
178
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
179
179
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
180
180
let result = await queryHandler . querySinglePeer ( mockPeer2 , mockTransactionID , 'myfunc' , [ 'arg1' , 'arg2' ] ) ;
181
- sinon . assert . calledOnce ( mockChannel . queryByChaincode ) ;
182
- sinon . assert . calledWith ( mockChannel . queryByChaincode , {
181
+ sinon . assert . calledOnce ( queryHandler . queryByChaincode ) ;
182
+ sinon . assert . calledWith ( queryHandler . queryByChaincode , {
183
183
chaincodeId : 'org-acme-biznet' ,
184
184
txId : mockTransactionID ,
185
185
fcn : 'myfunc' ,
@@ -191,18 +191,18 @@ describe('HLFQueryHandler', () => {
191
191
} ) ;
192
192
193
193
it ( 'should throw if no responses are returned' , ( ) => {
194
- mockChannel . queryByChaincode . resolves ( [ ] ) ;
194
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ ] ) ;
195
195
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
196
196
. should . be . rejectedWith ( / N o p a y l o a d s w e r e r e t u r n e d f r o m t h e q u e r y r e q u e s t / ) ;
197
197
} ) ;
198
198
199
199
it ( 'should return any responses that are errors and not UNAVAILABLE' , async ( ) => {
200
200
const response = new Error ( 'such error' ) ;
201
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
201
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
202
202
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
203
203
let result = await queryHandler . querySinglePeer ( mockPeer2 , mockTransactionID , 'myfunc' , [ 'arg1' , 'arg2' ] ) ;
204
- sinon . assert . calledOnce ( mockChannel . queryByChaincode ) ;
205
- sinon . assert . calledWith ( mockChannel . queryByChaincode , {
204
+ sinon . assert . calledOnce ( queryHandler . queryByChaincode ) ;
205
+ sinon . assert . calledWith ( queryHandler . queryByChaincode , {
206
206
chaincodeId : 'org-acme-biznet' ,
207
207
txId : mockTransactionID ,
208
208
fcn : 'myfunc' ,
@@ -216,7 +216,7 @@ describe('HLFQueryHandler', () => {
216
216
it ( 'should throw any responses that are errors and code 14 being unavailable.' , ( ) => {
217
217
const response = new Error ( '14 UNAVAILABLE: Connect Failed' ) ;
218
218
response . code = 14 ;
219
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
219
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
220
220
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
221
221
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
222
222
. should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
@@ -225,7 +225,7 @@ describe('HLFQueryHandler', () => {
225
225
it ( 'should throw any responses that are errors and code 1 being unavailable.' , ( ) => {
226
226
const response = new Error ( '1 UNAVAILABLE: Connect Failed' ) ;
227
227
response . code = 1 ;
228
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
228
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
229
229
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
230
230
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
231
231
. should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
@@ -234,17 +234,136 @@ describe('HLFQueryHandler', () => {
234
234
it ( 'should throw any responses that are errors and code 4 being unavailable.' , ( ) => {
235
235
const response = new Error ( '4 UNAVAILABLE: Connect Failed' ) ;
236
236
response . code = 4 ;
237
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
237
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
238
238
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
239
239
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
240
240
. should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
241
241
} ) ;
242
242
243
243
it ( 'should throw if query request fails' , ( ) => {
244
- mockChannel . queryByChaincode . rejects ( new Error ( 'Query Failed' ) ) ;
244
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . rejects ( new Error ( 'Query Failed' ) ) ;
245
245
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
246
246
. should . be . rejectedWith ( / Q u e r y F a i l e d / ) ;
247
247
} ) ;
248
248
} ) ;
249
249
250
+ describe ( '#queryByChaincode' , ( ) => {
251
+ it ( 'should handle single good response' , async ( ) => {
252
+ const request = {
253
+ id : 1
254
+ } ;
255
+ const results = [
256
+ [ {
257
+ response : {
258
+ payload : 'some payload'
259
+ }
260
+ } ]
261
+ ] ;
262
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
263
+ const responses = await queryHandler . queryByChaincode ( request ) ;
264
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
265
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
266
+ responses . length . should . equal ( 1 ) ;
267
+ responses [ 0 ] . should . equal ( 'some payload' ) ;
268
+ } ) ;
269
+
270
+ it ( 'should handle multiple good responses' , async ( ) => {
271
+ const request = {
272
+ id : 1
273
+ } ;
274
+ const results = [ [
275
+ {
276
+ response : {
277
+ payload : 'some payload'
278
+ }
279
+ } ,
280
+ {
281
+ response : {
282
+ payload : 'another payload'
283
+ }
284
+ } ,
285
+ {
286
+ response : {
287
+ payload : 'final payload'
288
+ }
289
+ }
290
+ ] ] ;
291
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
292
+ const responses = await queryHandler . queryByChaincode ( request ) ;
293
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
294
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
295
+ responses . length . should . equal ( 3 ) ;
296
+ responses [ 0 ] . should . equal ( 'some payload' ) ;
297
+ responses [ 1 ] . should . equal ( 'another payload' ) ;
298
+ responses [ 2 ] . should . equal ( 'final payload' ) ;
299
+ } ) ;
300
+
301
+ it ( 'should handle single error response' , async ( ) => {
302
+ const request = {
303
+ id : 1
304
+ } ;
305
+ const results = [
306
+ [ new Error ( 'some error' ) ]
307
+ ] ;
308
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
309
+ const responses = await queryHandler . queryByChaincode ( request ) ;
310
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
311
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
312
+ responses . length . should . equal ( 1 ) ;
313
+ responses [ 0 ] . should . be . instanceOf ( Error ) ;
314
+ responses [ 0 ] . message . should . equal ( 'some error' ) ;
315
+ } ) ;
316
+
317
+ it ( 'should handle multiple different response types' , async ( ) => {
318
+ const request = {
319
+ id : 1
320
+ } ;
321
+ const results = [ [
322
+
323
+ new Error ( 'some error' ) ,
324
+
325
+ {
326
+ response : {
327
+ payload : 'another payload'
328
+ }
329
+ } ,
330
+ {
331
+ response : 'a strange error'
332
+ } ,
333
+ {
334
+ data : 'I am not just an android'
335
+ }
336
+ ] ] ;
337
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
338
+ const responses = await queryHandler . queryByChaincode ( request ) ;
339
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
340
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
341
+ responses . length . should . equal ( 4 ) ;
342
+ responses [ 0 ] . should . be . instanceOf ( Error ) ;
343
+ responses [ 0 ] . message . should . equal ( 'some error' ) ;
344
+ responses [ 1 ] . should . equal ( 'another payload' ) ;
345
+ responses [ 2 ] . should . be . instanceOf ( Error ) ;
346
+ responses [ 3 ] . should . be . instanceOf ( Error ) ;
347
+ } ) ;
348
+
349
+ it ( 'should handle no responses' , async ( ) => {
350
+ const request = {
351
+ id : 1
352
+ } ;
353
+ let results = [ ] ;
354
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
355
+ await queryHandler . queryByChaincode ( request ) . should . be . rejectedWith ( / P a y l o a d r e s u l t s a r e m i s s i n g / ) ;
356
+ results = [ 'not an array' ] ;
357
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
358
+ await queryHandler . queryByChaincode ( request ) . should . be . rejectedWith ( / P a y l o a d r e s u l t s a r e m i s s i n g / ) ;
359
+ } ) ;
360
+
361
+ it ( 'should handle error from sendTransactionProposal' , async ( ) => {
362
+ const request = {
363
+ id : 1
364
+ } ;
365
+ mockChannel . sendTransactionProposal . rejects ( new Error ( 'sendTxProp error' ) ) ;
366
+ await queryHandler . queryByChaincode ( request ) . should . be . rejectedWith ( / s e n d T x P r o p e r r o r / ) ;
367
+ } ) ;
368
+ } ) ;
250
369
} ) ;
0 commit comments