@@ -265,7 +265,7 @@ func TestQueryRawContractState(t *testing.T) {
265
265
}
266
266
}
267
267
268
- func TestQueryContractListByCodeOrdering (t * testing.T ) {
268
+ func TestQueryContractsByCode (t * testing.T ) {
269
269
ctx , keepers := CreateTestInput (t , false , AvailableCapabilities )
270
270
keeper := keepers .WasmKeeper
271
271
@@ -298,26 +298,93 @@ func TestQueryContractListByCodeOrdering(t *testing.T) {
298
298
return ctx
299
299
}
300
300
301
+ contractAddrs := make ([]string , 0 , 10 )
301
302
// create 10 contracts with real block/gas setup
302
303
for i := 0 ; i < 10 ; i ++ {
303
304
// 3 tx per block, so we ensure both comparisons work
304
305
if i % 3 == 0 {
305
306
ctx = setBlock (ctx , h )
306
307
h ++
307
308
}
308
- _ , _ , err = keepers .ContractKeeper .Instantiate (ctx , codeID , creator , nil , initMsgBz , fmt .Sprintf ("contract %d" , i ), topUp )
309
+ addr , _ , err := keepers .ContractKeeper .Instantiate (ctx , codeID , creator , nil , initMsgBz , fmt .Sprintf ("contract %d" , i ), topUp )
310
+ contractAddrs = append (contractAddrs , addr .String ())
309
311
require .NoError (t , err )
310
312
}
311
313
312
- // query and check the results are properly sorted
313
314
q := Querier (keeper )
314
- res , err := q .ContractsByCode (sdk .WrapSDKContext (ctx ), & types.QueryContractsByCodeRequest {CodeId : codeID })
315
- require .NoError (t , err )
316
-
317
- require .Equal (t , 10 , len (res .Contracts ))
315
+ specs := map [string ]struct {
316
+ req * types.QueryContractsByCodeRequest
317
+ expAddr []string
318
+ expErr error
319
+ }{
320
+ "with empty request" : {
321
+ req : nil ,
322
+ expErr : status .Error (codes .InvalidArgument , "empty request" ),
323
+ },
324
+ "req.CodeId=0" : {
325
+ req : & types.QueryContractsByCodeRequest {CodeId : 0 },
326
+ expErr : errorsmod .Wrap (types .ErrInvalid , "code id" ),
327
+ },
328
+ "not exist codeID" : {
329
+ req : & types.QueryContractsByCodeRequest {CodeId : codeID + 1 },
330
+ expAddr : []string {},
331
+ },
332
+ "query all and check the results are properly sorted" : {
333
+ req : & types.QueryContractsByCodeRequest {
334
+ CodeId : codeID ,
335
+ },
336
+ expAddr : contractAddrs ,
337
+ },
338
+ "with pagination offset" : {
339
+ req : & types.QueryContractsByCodeRequest {
340
+ CodeId : codeID ,
341
+ Pagination : & query.PageRequest {
342
+ Offset : 5 ,
343
+ },
344
+ },
345
+ expAddr : contractAddrs [5 :10 ],
346
+ },
347
+ "with invalid pagination key" : {
348
+ req : & types.QueryContractsByCodeRequest {
349
+ CodeId : codeID ,
350
+ Pagination : & query.PageRequest {
351
+ Offset : 1 ,
352
+ Key : []byte ("test" ),
353
+ },
354
+ },
355
+ expErr : fmt .Errorf ("invalid request, either offset or key is expected, got both" ),
356
+ },
357
+ "with pagination limit" : {
358
+ req : & types.QueryContractsByCodeRequest {
359
+ CodeId : codeID ,
360
+ Pagination : & query.PageRequest {
361
+ Limit : 5 ,
362
+ },
363
+ },
364
+ expAddr : contractAddrs [0 :5 ],
365
+ },
366
+ "with pagination next key" : {
367
+ req : & types.QueryContractsByCodeRequest {
368
+ CodeId : codeID ,
369
+ Pagination : & query.PageRequest {
370
+ Key : fromBase64 ("AAAAAAAAAAoAAAAAAAOc/4cuhNIMvyvID4NhhfROlbQNuZ0fl0clmBPoWHtKYazH" ),
371
+ },
372
+ },
373
+ expAddr : contractAddrs [1 :10 ],
374
+ },
375
+ }
376
+ for msg , spec := range specs {
377
+ t .Run (msg , func (t * testing.T ) {
378
+ got , err := q .ContractsByCode (sdk .WrapSDKContext (ctx ), spec .req )
318
379
319
- for _ , contractAddr := range res .Contracts {
320
- assert .NotEmpty (t , contractAddr )
380
+ if spec .expErr != nil {
381
+ assert .NotNil (t , err )
382
+ assert .EqualError (t , err , spec .expErr .Error ())
383
+ return
384
+ }
385
+ assert .NotNil (t , got )
386
+ assert .Equal (t , spec .expAddr , got .Contracts )
387
+ })
321
388
}
322
389
}
323
390
0 commit comments