Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of a Mock for Lpdb #2047

Merged
merged 12 commits into from
Oct 19, 2022
4 changes: 3 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
std = {
globals = {
"mw",
},
read_globals = {
"arg",
"assert",
Expand All @@ -7,7 +10,6 @@ std = {
"getmetatable",
"ipairs",
"math",
"mw",
"next",
"os",
"package",
Expand Down
97 changes: 97 additions & 0 deletions standard/mock/mock_lpdb.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
-- @Liquipedia
-- wiki=commons
-- page=Module:Mock/Lpdb
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Array = require('Module:Array')
local Json = require('Module:Json')
local Lua = require('Module:Lua')
local String = require('Module:StringUtils')
local Table = require('Module:Table')

local lpdbData = Lua.import('Module:Mock/Lpdb/Data', {requireDevIfEnabled = true})

local mockLpdb = {}

local _lpdb = {
lpdb = mw.ext.LiquipediaDB.lpdb
}

function mockLpdb.setUp()
mw.ext.LiquipediaDB.lpdb = mockLpdb.query
end

function mockLpdb.tearDown()
Rathoz marked this conversation as resolved.
Show resolved Hide resolved
mw.ext.LiquipediaDB.lpdb = _lpdb.lpdb
end

--- Not yet support in Mock is:
Rathoz marked this conversation as resolved.
Show resolved Hide resolved
---- conditions with `OR` or `_`
---- query with `::`
---- order
---- groupby
function mockLpdb.query(table, parameters)
local data = lpdbData[table]

if not data then
return error(mw.message.new('liquipediadb-error-invalid-datatype'))
end

data = Array.map(data, function(entry)
return (Json.parseIfString(entry))
Rathoz marked this conversation as resolved.
Show resolved Hide resolved
end)
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

-- Conditions
if String.isNotEmpty(parameters.conditions) then
local condition = mockLpdb._parseConditions(parameters.conditions)
data = Array.filter(data, condition)
end
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

--Limit/Offset
parameters.limit = tonumber(parameters.limit) or 20
parameters.offset = tonumber(parameters.offset) or 0
data = Array.sub(data, parameters.offset + 1, (parameters.offset + parameters.limit))
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

--Query
if String.isNotEmpty(parameters.query) then
local fields = Table.mapValues(mw.text.split(parameters.query, ','), mw.text.trim)
data = Array.map(data, function(entry)
return Table.map(entry, function(field, value)
-- Use map as a filter since there's no applicable filter function atm
return field, Table.includes(fields, field) and value or nil
end)
end)
end
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

return data
end

function mockLpdb._parseConditions(conditions)
---@type {comparator:string, name:string, value:string}[]
local criterias = {}

for name, comparator, value in string.gmatch(conditions, '%[%[(%a+)::([!><]?)([%a%s%d]+)]]') do
Rathoz marked this conversation as resolved.
Show resolved Hide resolved
table.insert(criterias, {name = name, comparator = comparator, value = value})
end

return function (entry)
return Array.all(criterias, function (criteria)
if criteria.comparator == '' then
return entry[criteria.name] == criteria.value
elseif criteria.comparator == '!' then
Rathoz marked this conversation as resolved.
Show resolved Hide resolved
return entry[criteria.name] ~= criteria.value
elseif criteria.comparator == '>' then
return entry[criteria.name] > criteria.value
elseif criteria.comparator == '<' then
return entry[criteria.name] < criteria.value
else
error('Unknown comparator: '.. tostring(criteria.comparator))
end
end)
end
end

return mockLpdb
Loading