Skip to content

Commit

Permalink
chore(utils): add pagination tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Oct 19, 2024
1 parent ed7dfda commit 25af619
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
19 changes: 19 additions & 0 deletions spec/balances_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
27 changes: 27 additions & 0 deletions spec/gar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 7 additions & 6 deletions src/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 25af619

Please sign in to comment.