Skip to content

Commit

Permalink
chore(test): add test for getting paginated primary names
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Nov 16, 2024
1 parent 7c3edf3 commit a0ceb1d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
25 changes: 25 additions & 0 deletions spec/primary_names_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,31 @@ describe("Primary Names", function()
end)
end)

describe("getPaginatedPrimaryNames", function()
it("should return all primary names", function()
_G.PrimaryNames = {
owners = {
["owner"] = { name = "test", startTimestamp = 1234567890 },
},
names = {
["test"] = "owner",
},
requests = {},
}
local paginatedPrimaryNames = primaryNames.getPaginatedPrimaryNames(nil, 10, "startTimestamp", "asc")
assert.are.same({
items = {
{ name = "test", owner = "owner", startTimestamp = 1234567890 },
},
totalItems = 1,
limit = 10,
hasMore = false,
sortBy = "startTimestamp",
sortOrder = "asc",
}, paginatedPrimaryNames)
end)
end)

describe("getPaginatedPrimaryNameRequests", function()
it("should return all primary name requests", function()
_G.PrimaryNames.requests = {
Expand Down
12 changes: 6 additions & 6 deletions src/primary_names.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end

--- Creates a transient request for a primary name. This is done by a user and must be approved by the name owner of the base name.
--- @param name string -- the name being requested, this could be an undername provided by the ant
--- @param initiator string -- the address that is creating the primary name request, e.g. the ANT process id
--- @param initiator WalletAddress -- the address that is creating the primary name request, e.g. the ANT process id
--- @param timestamp number -- the timestamp of the request
--- @param msgId string -- the message id of the request
--- @param fundFrom "balance"|"stakes"|"any"|nil -- the address to fund the request from. Default is "balance"
Expand Down Expand Up @@ -101,14 +101,14 @@ function primaryNames.createPrimaryNameRequest(name, initiator, timestamp, msgId
end

--- Get a primary name request, safely deep copying the request
--- @param address string
--- @param address WalletAddress
--- @return PrimaryNameRequest|nil primaryNameClaim - the request found, or nil if it does not exist
function primaryNames.getPrimaryNameRequest(address)
return utils.deepCopy(primaryNames.getUnsafePrimaryNameRequests()[address])
end

--- Unsafe access to the primary name requests
--- @return table<string, PrimaryNameRequest> primaryNameClaims - the primary name requests
--- @return table<WalletAddress, PrimaryNameRequest> primaryNameClaims - the primary name requests
function primaryNames.getUnsafePrimaryNameRequests()
return PrimaryNames.requests or {}
end
Expand All @@ -118,7 +118,7 @@ function primaryNames.getUnsafePrimaryNames()
end

--- Unsafe access to the primary name owners
--- @return table<string, PrimaryName> primaryNames - the primary names
--- @return table<WalletAddress, PrimaryName> primaryNames - the primary names
function primaryNames.getUnsafePrimaryNameOwners()
return PrimaryNames.owners or {}
end
Expand Down Expand Up @@ -300,10 +300,10 @@ end
function primaryNames.getPaginatedPrimaryNames(cursor, limit, sortBy, sortOrder)
local primaryNamesArray = {}
local cursorField = "name"
for address, primaryName in ipairs(primaryNames.getUnsafePrimaryNameOwners()) do
for owner, primaryName in pairs(primaryNames.getUnsafePrimaryNameOwners()) do
table.insert(primaryNamesArray, {
name = primaryName.name,
address = address,
owner = owner,
startTimestamp = primaryName.startTimestamp,
})
end
Expand Down

0 comments on commit a0ceb1d

Please sign in to comment.