@@ -265,7 +265,7 @@ func TestQueryRawContractState(t *testing.T) {
265265 }
266266}
267267
268- func TestQueryContractListByCodeOrdering (t * testing.T ) {
268+ func TestQueryContractsByCode (t * testing.T ) {
269269 ctx , keepers := CreateTestInput (t , false , AvailableCapabilities )
270270 keeper := keepers .WasmKeeper
271271
@@ -298,26 +298,93 @@ func TestQueryContractListByCodeOrdering(t *testing.T) {
298298 return ctx
299299 }
300300
301+ contractAddrs := make ([]string , 0 , 10 )
301302 // create 10 contracts with real block/gas setup
302303 for i := 0 ; i < 10 ; i ++ {
303304 // 3 tx per block, so we ensure both comparisons work
304305 if i % 3 == 0 {
305306 ctx = setBlock (ctx , h )
306307 h ++
307308 }
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 ())
309311 require .NoError (t , err )
310312 }
311313
312- // query and check the results are properly sorted
313314 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 )
318379
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+ })
321388 }
322389}
323390
0 commit comments