From f0217260cd77bf16d0ca79bbdf3117133fad05c9 Mon Sep 17 00:00:00 2001 From: Daniel Wedul Date: Wed, 25 Oct 2023 14:35:55 -0600 Subject: [PATCH] [1699]: Unit tests on the GetAssetOrders query. --- x/exchange/keeper/grpc_query_test.go | 721 ++++++++++++++++++++++++++- 1 file changed, 720 insertions(+), 1 deletion(-) diff --git a/x/exchange/keeper/grpc_query_test.go b/x/exchange/keeper/grpc_query_test.go index 9008f52564..6d9e58aafb 100644 --- a/x/exchange/keeper/grpc_query_test.go +++ b/x/exchange/keeper/grpc_query_test.go @@ -2256,7 +2256,726 @@ func (s *TestSuite) TestQueryServer_GetOwnerOrders() { } } -// TODO[1658]: func (s *TestSuite) TestQueryServer_GetAssetOrders() +func (s *TestSuite) TestQueryServer_GetAssetOrders() { + queryName := "GetAssetOrders" + runner := func(req *exchange.QueryGetAssetOrdersRequest) queryRunner { + return func(goCtx context.Context) (interface{}, error) { + return keeper.NewQueryServer(s.k).GetAssetOrders(goCtx, req) + } + } + makeKey := func(order *exchange.Order) []byte { + return keeper.Uint64Bz(order.OrderId) + } + + denom1, denom2, denom3 := "one", "two", "three" + denoms := []string{denom1, denom2, denom3} + denomCount := len(denoms) + ordersPerDenom := 20 + denomOrders := make(map[string][]*exchange.Order, denomCount) + denomAskOrders := make(map[string][]*exchange.Order, denomCount) + denomBidOrders := make(map[string][]*exchange.Order, denomCount) + for _, denom := range denoms { + denomOrders[denom] = make([]*exchange.Order, 0, ordersPerDenom) + denomAskOrders[denom] = make([]*exchange.Order, 0, ordersPerDenom/2) + denomBidOrders[denom] = make([]*exchange.Order, 0, ordersPerDenom/2) + } + mainStore := s.getStore() + for i := 1; i <= denomCount*ordersPerDenom; i++ { + orderID := uint64(i) + denom := denoms[i%denomCount] + order := exchange.NewOrder(orderID) + if orderID%2 == 0 { + order.WithAsk(&exchange.AskOrder{ + MarketId: uint32(5000 + i), + Seller: sdk.AccAddress(fmt.Sprintf("seller_%d____________", orderID)[:20]).String(), + Assets: sdk.NewInt64Coin(denom, int64(i)), + Price: sdk.NewInt64Coin("plum", int64(i)), + AllowPartial: orderID%4 < 2, + ExternalId: fmt.Sprintf("external-id-%d", i), + }) + denomAskOrders[denom] = append(denomAskOrders[denom], order) + } else { + order.WithBid(&exchange.BidOrder{ + MarketId: uint32(5000 + i), + Buyer: sdk.AccAddress(fmt.Sprintf("buyer_%d_____________", orderID)[:20]).String(), + Assets: sdk.NewInt64Coin(denom, int64(i)), + Price: sdk.NewInt64Coin("plum", int64(i)), + AllowPartial: orderID%4 < 2, + ExternalId: fmt.Sprintf("external-id-%d", i), + }) + denomBidOrders[denom] = append(denomBidOrders[denom], order) + } + denomOrders[denom] = append(denomOrders[denom], order) + s.requireSetOrderInStore(mainStore, order) + } + + // OrderIDs for each denom: + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + //denom1: 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58 + //denom2: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59 + //denom3: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60 + + tests := []struct { + name string + setup querySetupFunc + req *exchange.QueryGetAssetOrdersRequest + expResp *exchange.QueryGetAssetOrdersResponse + expInErr []string + }{ + // Tests on errors and non-normal conditions. + { + name: "nil req", + req: nil, + expInErr: []string{invalidArgErr, "empty request"}, + }, + { + name: "empty asset", + req: &exchange.QueryGetAssetOrdersRequest{Asset: ""}, + expInErr: []string{invalidArgErr, "empty request"}, + }, + { + name: "unknown order type", + req: &exchange.QueryGetAssetOrdersRequest{Asset: denom1, OrderType: "burger and fries"}, + expInErr: []string{invalidArgErr, "error iterating orders for asset " + denom1 + ": unknown order type \"burger and fries\""}, + }, + { + name: "no orders", + req: &exchange.QueryGetAssetOrdersRequest{Asset: "four"}, + expResp: &exchange.QueryGetAssetOrdersResponse{Orders: nil, Pagination: &query.PageResponse{}}, + }, + { + name: "bad index entry", + setup: func(ctx sdk.Context) { + store := s.k.GetStore(ctx) + s.requireSetOrderInStore(store, exchange.NewOrder(98).WithAsk(&exchange.AskOrder{ + MarketId: 7, Seller: s.addr1.String(), Assets: s.coin("98apple"), Price: s.coin("98prune"), + })) + key99, value99, err := s.k.GetOrderStoreKeyValue(*exchange.NewOrder(99).WithAsk(&exchange.AskOrder{ + MarketId: 8, Seller: s.addr2.String(), Assets: s.coin("99apple"), Price: s.coin("99prune"), + })) + s.Require().NoError(err, "GetOrderStoreKeyValue 99") + store.Set(key99, value99) + idxKey := keeper.MakeIndexKeyMarketToOrder(8, 99) + idxKey[len(idxKey)-2] = idxKey[len(idxKey)-1] + store.Set(idxKey[:len(idxKey)-1], []byte{keeper.OrderKeyTypeAsk}) + s.requireSetOrderInStore(store, exchange.NewOrder(100).WithAsk(&exchange.AskOrder{ + MarketId: 9, Seller: s.addr3.String(), Assets: s.coin("100apple"), Price: s.coin("100prune"), + })) + }, + req: &exchange.QueryGetAssetOrdersRequest{Asset: "apple"}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: []*exchange.Order{ + exchange.NewOrder(98).WithAsk(&exchange.AskOrder{ + MarketId: 7, Seller: s.addr1.String(), Assets: s.coin("98apple"), Price: s.coin("98prune"), + }), + exchange.NewOrder(100).WithAsk(&exchange.AskOrder{ + MarketId: 9, Seller: s.addr3.String(), Assets: s.coin("100apple"), Price: s.coin("100prune"), + }), + }, + Pagination: &query.PageResponse{Total: 2}, + }, + }, + { + name: "index entry to order that does not exist", + setup: func(ctx sdk.Context) { + store := s.k.GetStore(ctx) + s.requireSetOrderInStore(store, exchange.NewOrder(98).WithAsk(&exchange.AskOrder{ + MarketId: 7, Seller: s.addr1.String(), Assets: s.coin("98acorn"), Price: s.coin("98prune"), + })) + key := keeper.MakeIndexKeyAssetToOrder("acorn", 99) + store.Set(key, []byte{keeper.OrderKeyTypeAsk}) + s.requireSetOrderInStore(store, exchange.NewOrder(100).WithAsk(&exchange.AskOrder{ + MarketId: 9, Seller: s.addr3.String(), Assets: s.coin("100acorn"), Price: s.coin("100prune"), + })) + }, + req: &exchange.QueryGetAssetOrdersRequest{Asset: "acorn"}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: []*exchange.Order{ + exchange.NewOrder(98).WithAsk(&exchange.AskOrder{ + MarketId: 7, Seller: s.addr1.String(), Assets: s.coin("98acorn"), Price: s.coin("98prune"), + }), + exchange.NewOrder(100).WithAsk(&exchange.AskOrder{ + MarketId: 9, Seller: s.addr3.String(), Assets: s.coin("100acorn"), Price: s.coin("100prune"), + }), + }, + Pagination: &query.PageResponse{Total: 3}, + }, + }, + { + name: "error reading an order", + setup: func(ctx sdk.Context) { + store := s.k.GetStore(ctx) + s.requireSetOrderInStore(store, exchange.NewOrder(98).WithAsk(&exchange.AskOrder{ + MarketId: 7, Seller: s.addr1.String(), Assets: s.coin("98acorn"), Price: s.coin("98prune"), + })) + key99, value99, err := s.k.GetOrderStoreKeyValue(*exchange.NewOrder(99).WithAsk(&exchange.AskOrder{ + MarketId: 8, Seller: s.addr2.String(), Assets: s.coin("99acorn"), Price: s.coin("99prune"), + })) + s.Require().NoError(err, "GetOrderStoreKeyValue 99") + value99[0] = 8 + store.Set(key99, value99) + idxKey := keeper.MakeIndexKeyAssetToOrder("acorn", 99) + store.Set(idxKey, []byte{8}) + s.requireSetOrderInStore(store, exchange.NewOrder(100).WithAsk(&exchange.AskOrder{ + MarketId: 9, Seller: s.addr3.String(), Assets: s.coin("100acorn"), Price: s.coin("100prune"), + })) + }, + req: &exchange.QueryGetAssetOrdersRequest{Asset: "acorn"}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: []*exchange.Order{ + exchange.NewOrder(98).WithAsk(&exchange.AskOrder{ + MarketId: 7, Seller: s.addr1.String(), Assets: s.coin("98acorn"), Price: s.coin("98prune"), + }), + exchange.NewOrder(100).WithAsk(&exchange.AskOrder{ + MarketId: 9, Seller: s.addr3.String(), Assets: s.coin("100acorn"), Price: s.coin("100prune"), + }), + }, + Pagination: &query.PageResponse{Total: 3}, + }, + }, + + // Forward, no order type. + { + name: "forward, no order type, no after order, get all", + req: &exchange.QueryGetAssetOrdersRequest{Asset: denom1}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom1], + Pagination: &query.PageResponse{Total: 20}, + }, + }, + { + name: "forward, no order type, no after order, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, + Pagination: &query.PageRequest{Limit: 3, Key: makeKey(denomOrders[denom2][2])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom2][2:5], + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom2][5])}, + }, + }, + { + name: "forward, no order type, no after order, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, + Pagination: &query.PageRequest{Limit: 5, Offset: 8, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom3][8:13], + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom3][13])}, + }, + }, + { + name: "forward, no order type, no after order, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, + Pagination: &query.PageRequest{Limit: 5, Offset: 6, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom1][6:11], + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom1][11]), Total: 20}, + }, + }, + { + name: "forward, no order type, after order 30, get all", + req: &exchange.QueryGetAssetOrdersRequest{Asset: denom2, AfterOrderId: 30}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom2][10:], + Pagination: &query.PageResponse{Total: 10}, + }, + }, + { + name: "forward, no order type, after order 30, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 2, Key: makeKey(denomOrders[denom1][15])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom1][15:17], + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom1][17])}, + }, + }, + { + name: "forward, no order type, after order 30, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 3, Offset: 2, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom1][12:15], + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom1][15])}, + }, + }, + { + name: "forward, no order type, after order 30, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 1, Offset: 7, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomOrders[denom3][17:18], + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom3][18]), Total: 10}, + }, + }, + + // Forward, only ask orders + { + name: "forward, ask orders, no after order, get all", + req: &exchange.QueryGetAssetOrdersRequest{Asset: denom3, OrderType: "ask"}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom3], + Pagination: &query.PageResponse{Total: 10}, + }, + }, + { + name: "forward, ask orders, no after order, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "asks", + Pagination: &query.PageRequest{Limit: 3, Key: makeKey(denomAskOrders[denom1][4])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom1][4:7], + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom1][7])}, + }, + }, + { + name: "forward, ask orders, no after order, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "ASK", + Pagination: &query.PageRequest{Limit: 3, Offset: 8, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom2][8:], + Pagination: &query.PageResponse{}, + }, + }, + { + name: "forward, ask orders, no after order, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "ASKS", + Pagination: &query.PageRequest{Limit: 3, Offset: 6, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom2][6:9], + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom2][9]), Total: 10}, + }, + }, + { + name: "forward, ask orders, after order 30, get all", + req: &exchange.QueryGetAssetOrdersRequest{Asset: denom3, OrderType: "AskOrders", AfterOrderId: 30}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom3][5:], + Pagination: &query.PageResponse{Total: 5}, + }, + }, + { + name: "forward, ask orders, after order 30, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "ask orders", AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 1, Key: makeKey(denomAskOrders[denom2][7])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom2][7:8], + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom2][8])}, + }, + }, + { + name: "forward, ask orders, after order 30, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "askOrder", AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 2, Offset: 2, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom1][7:9], + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom1][9])}, + }, + }, + { + name: "forward, ask orders, after order 30, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "aSKs", AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 2, Offset: 1, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomAskOrders[denom1][6:8], + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom1][8]), Total: 5}, + }, + }, + + // Forward, only bid orders + { + name: "forward, bid orders, no after order, get all", + req: &exchange.QueryGetAssetOrdersRequest{Asset: denom3, OrderType: "bid"}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom3], + Pagination: &query.PageResponse{Total: 10}, + }, + }, + { + name: "forward, bid orders, no after order, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "bids", + Pagination: &query.PageRequest{Limit: 3, Key: makeKey(denomBidOrders[denom1][4])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom1][4:7], + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom1][7])}, + }, + }, + { + name: "forward, bid orders, no after order, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "BID", + Pagination: &query.PageRequest{Limit: 3, Offset: 8, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom2][8:], + Pagination: &query.PageResponse{}, + }, + }, + { + name: "forward, bid orders, no after order, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "BIDS", + Pagination: &query.PageRequest{Limit: 3, Offset: 6, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom2][6:9], + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom2][9]), Total: 10}, + }, + }, + { + name: "forward, bid orders, after order 30, get all", + req: &exchange.QueryGetAssetOrdersRequest{Asset: denom3, OrderType: "BidOrders", AfterOrderId: 30}, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom3][5:], + Pagination: &query.PageResponse{Total: 5}, + }, + }, + { + name: "forward, bid orders, after order 30, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "bid orders", AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 1, Key: makeKey(denomBidOrders[denom2][7])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom2][7:8], + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom2][8])}, + }, + }, + { + name: "forward, bid orders, after order 30, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "bidOrder", AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 2, Offset: 2, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom1][7:9], + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom1][9])}, + }, + }, + { + name: "forward, bid orders, after order 30, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "bIDs", AfterOrderId: 30, + Pagination: &query.PageRequest{Limit: 2, Offset: 1, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: denomBidOrders[denom1][6:8], + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom1][8]), Total: 5}, + }, + }, + + // Reverse, no order type. + { + name: "reverse, no order type, no after order, get all", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, + Pagination: &query.PageRequest{Reverse: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom1]), + Pagination: &query.PageResponse{Total: 20}, + }, + }, + { + name: "reverse, no order type, no after order, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Key: makeKey(denomOrders[denom2][12])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom2][10:13]), + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom2][9])}, + }, + }, + { + name: "reverse, no order type, no after order, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, + Pagination: &query.PageRequest{Reverse: true, Limit: 5, Offset: 8, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom3][7:12]), + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom3][6])}, + }, + }, + { + name: "reverse, no order type, no after order, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, + Pagination: &query.PageRequest{Reverse: true, Limit: 5, Offset: 6, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom1][9:14]), + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom1][8]), Total: 20}, + }, + }, + { + name: "reverse, no order type, after order 30, get all", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom2][10:]), + Pagination: &query.PageResponse{Total: 10}, + }, + }, + { + name: "reverse, no order type, after order 30, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 2, Key: makeKey(denomOrders[denom1][15])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom1][14:16]), + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom1][13])}, + }, + }, + { + name: "reverse, no order type, after order 30, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Offset: 2, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom1][15:18]), + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom1][14])}, + }, + }, + { + name: "reverse, no order type, after order 30, limit with offset and count", + // A key point of this test is that order 30 is in market 3. The AfterOrderID order + // should NOT be included in results, though, so there should still only be 10 results here. + // This validates that the "afterOrderID + 1" is correct in the getOrderIterator reverse block. + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 1, Offset: 7, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomOrders[denom3][12:13]), + Pagination: &query.PageResponse{NextKey: makeKey(denomOrders[denom3][11]), Total: 10}, + }, + }, + + // Reverse, only ask orders + { + name: "reverse, ask orders, no after order, get all", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, OrderType: "ask", + Pagination: &query.PageRequest{Reverse: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom3]), + Pagination: &query.PageResponse{Total: 10}, + }, + }, + { + name: "reverse, ask orders, no after order, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "asks", + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Key: makeKey(denomAskOrders[denom1][4])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom1][2:5]), + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom1][1])}, + }, + }, + { + name: "reverse, ask orders, no after order, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "ASK", + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Offset: 8, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom2][:2]), + Pagination: &query.PageResponse{}, + }, + }, + { + name: "reverse, ask orders, no after order, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "ASKS", + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Offset: 1, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom2][6:9]), + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom2][5]), Total: 10}, + }, + }, + { + name: "reverse, ask orders, after order 30, get all", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, OrderType: "AskOrders", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom3][5:]), + Pagination: &query.PageResponse{Total: 5}, + }, + }, + { + name: "reverse, ask orders, after order 30, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "ask orders", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 1, Key: makeKey(denomAskOrders[denom2][7])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom2][7:8]), + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom2][6])}, + }, + }, + { + name: "reverse, ask orders, after order 30, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "askOrder", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 2, Offset: 2, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom1][6:8]), + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom1][5])}, + }, + }, + { + name: "reverse, ask orders, after order 30, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "aSKs", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Offset: 1, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomAskOrders[denom1][6:9]), + Pagination: &query.PageResponse{NextKey: makeKey(denomAskOrders[denom1][5]), Total: 5}, + }, + }, + + // Reverse, only bid orders + { + name: "reverse, bid orders, no after order, get all", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, OrderType: "bid", + Pagination: &query.PageRequest{Reverse: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom3]), + Pagination: &query.PageResponse{Total: 10}, + }, + }, + { + name: "reverse, bid orders, no after order, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "bids", + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Key: makeKey(denomBidOrders[denom1][4])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom1][2:5]), + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom1][1])}, + }, + }, + { + name: "reverse, bid orders, no after order, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "BID", + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Offset: 8, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom2][:2]), + Pagination: &query.PageResponse{}, + }, + }, + { + name: "reverse, bid orders, no after order, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "BIDS", + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Offset: 1, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom2][6:9]), + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom2][5]), Total: 10}, + }, + }, + { + name: "reverse, bid orders, after order 30, get all", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom3, OrderType: "BidOrders", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom3][5:]), + Pagination: &query.PageResponse{Total: 5}, + }, + }, + { + name: "reverse, bid orders, after order 30, limit with key", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom2, OrderType: "bid orders", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 1, Key: makeKey(denomBidOrders[denom2][7])}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom2][7:8]), + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom2][6])}, + }, + }, + { + name: "reverse, bid orders, after order 30, limit with offset, no count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "bidOrder", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 2, Offset: 2, CountTotal: false}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom1][6:8]), + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom1][5])}, + }, + }, + { + name: "reverse, bid orders, after order 30, limit with offset and count", + req: &exchange.QueryGetAssetOrdersRequest{ + Asset: denom1, OrderType: "bIDs", AfterOrderId: 30, + Pagination: &query.PageRequest{Reverse: true, Limit: 3, Offset: 1, CountTotal: true}, + }, + expResp: &exchange.QueryGetAssetOrdersResponse{ + Orders: reverseSlice(denomBidOrders[denom1][6:9]), + Pagination: &query.PageResponse{NextKey: makeKey(denomBidOrders[denom1][5]), Total: 5}, + }, + }, + } + + for _, tc := range tests { + s.Run(tc.name, func() { + respRaw := s.doQueryTest(tc.setup, runner(tc.req), tc.expInErr, queryName) + if s.Assert().Equal(tc.expResp, respRaw, queryName+" result") { + return + } + resp, ok := respRaw.(*exchange.QueryGetAssetOrdersResponse) + s.Require().True(ok, queryName+" response is of type %T and could not be cast to %T", respRaw, tc.expResp) + if tc.expResp == nil || resp == nil { + return + } + s.assertEqualOrders(tc.expResp.Orders, resp.Orders, "%s Orders", queryName) + s.assertEqualPageResponse(tc.expResp.Pagination, resp.Pagination, "%s Pagination", queryName) + }) + } +} // TODO[1658]: func (s *TestSuite) TestQueryServer_GetAllOrders()