diff --git a/spec/balances_spec.lua b/spec/balances_spec.lua index 38c5eea..788cade 100644 --- a/spec/balances_spec.lua +++ b/spec/balances_spec.lua @@ -44,4 +44,23 @@ describe("balances", function() assert.are.equal(0, balances.getBalance(testAddress2)) assert.are.equal(100, balances.getBalance(testAddress1)) end) + + describe("getPaginatedBalances", function() + it("should return paginated balances", function() + local balances = balances.getPaginatedBalances(nil, 10, "balance", "desc") + assert.are.same(balances, { + limit = 10, + sortBy = "balance", + sortOrder = "desc", + hasMore = false, + totalItems = 1, + items = { + { + address = testAddress1, + balance = 100, + }, + }, + }) + end) + end) end) diff --git a/spec/gar_spec.lua b/spec/gar_spec.lua index 0dd0738..e98cb11 100644 --- a/spec/gar_spec.lua +++ b/spec/gar_spec.lua @@ -1798,4 +1798,31 @@ describe("gar", function() }) end) end) + + describe("getPaginatedGateways", function() + it("should return paginated gateways sorted by startTimestamp in ascending order (oldest first)", function() + local gateway1 = utils.deepCopy(testGateway) + local gateway2 = utils.deepCopy(testGateway) + gateway1.startTimestamp = 1000 + gateway2.startTimestamp = 0 + _G.GatewayRegistry = { + [stubGatewayAddress] = gateway1, + [stubRandomAddress] = gateway2, + } + local gateways = gar.getPaginatedGateways(nil, 10, "startTimestamp", "asc") + gateway1.gatewayAddress = stubGatewayAddress + gateway2.gatewayAddress = stubRandomAddress + assert.are.same({ + limit = 10, + sortBy = "startTimestamp", + sortOrder = "asc", + hasMore = false, + totalItems = 2, + items = { + gateway2, -- should be first because it has a lower startTimestamp + gateway1, + }, + }, gateways) + end) + end) end) diff --git a/src/utils.lua b/src/utils.lua index 99230d7..c853f1d 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -89,15 +89,16 @@ end function utils.paginateTableWithCursor(tableArray, cursor, cursorField, limit, sortBy, sortOrder) local sortedTable = utils.sortTableByField(tableArray, sortBy, sortOrder) - if not sortedTable or #sortedTable == 0 then + + if not sortedTable or utils.lengthOfTable(sortedTable) == 0 then return { items = {}, limit = limit, totalItems = 0, - totalPages = 0, sortBy = sortBy, sortOrder = sortOrder, nextCursor = nil, + hasMore = false, } end @@ -113,25 +114,25 @@ function utils.paginateTableWithCursor(tableArray, cursor, cursorField, limit, s end local items = {} - local endIndex = math.min(startIndex + limit - 1, #sortedTable) + local endIndex = math.min(startIndex + limit - 1, utils.lengthOfTable(sortedTable)) for i = startIndex, endIndex do table.insert(items, sortedTable[i]) end local nextCursor = nil - if endIndex < #sortedTable then + if endIndex < utils.lengthOfTable(sortedTable) then nextCursor = sortedTable[endIndex][cursorField] end return { items = items, limit = limit, - totalItems = #sortedTable, + totalItems = utils.lengthOfTable(sortedTable), sortBy = sortBy, sortOrder = sortOrder, nextCursor = nextCursor, - hasMore = nextCursor ~= nil, + hasMore = nextCursor ~= nil and true or false, } end