aproxy/tests/schema_validation.lua

50 lines
1.3 KiB
Lua
Raw Normal View History

2022-12-07 18:12:43 +00:00
TestSchemaValidator = {}
2022-12-07 17:57:07 +00:00
local config = require('config')
2022-12-07 18:12:43 +00:00
function TestSchemaValidator:testBasicFields()
2022-12-07 17:57:07 +00:00
local errors = config.validateSchema({a={type='string'}}, {a='test'})
2022-12-07 18:26:11 +00:00
lu.assertIs(table.len(errors), 0)
2022-12-07 17:57:07 +00:00
local errors = config.validateSchema({a={type='number'}}, {a=123})
2022-12-07 18:26:11 +00:00
lu.assertIs(table.len(errors), 0)
2022-12-07 17:57:07 +00:00
local errors = config.validateSchema({a={type='string'}}, {a=123})
2022-12-07 18:26:11 +00:00
lu.assertIs(table.len(errors), 1)
2022-12-07 17:57:07 +00:00
end
2022-12-07 18:12:43 +00:00
function TestSchemaValidator:testList()
2022-12-07 17:57:07 +00:00
local errors = config.validateSchema({a={type='list', schema={type='number'}}}, {a={1,2,3}})
2022-12-07 18:26:11 +00:00
lu.assertIs(table.len(errors), 0)
2022-12-07 17:57:07 +00:00
local errors = config.validateSchema({a={type='list', schema={type='number'}}}, {a={1,2,3,'asd'}})
2022-12-07 18:26:11 +00:00
lu.assertIs(table.len(errors), 1)
2022-12-07 17:57:07 +00:00
end
2022-12-07 18:12:43 +00:00
function TestSchemaValidator:testTable()
2022-12-07 18:13:33 +00:00
local TEST_SCHEMA = {
2022-12-07 18:12:43 +00:00
a={
type='table',
schema={
b={
type='number'
}
}
}
2022-12-07 18:13:33 +00:00
}
local errors = config.validateSchema(
TEST_SCHEMA,
2022-12-07 18:12:43 +00:00
{a=
{b=2}
}
)
2022-12-07 18:26:11 +00:00
lu.assertIs(table.len(errors), 0)
2022-12-07 18:13:33 +00:00
local errors = config.validateSchema(
TEST_SCHEMA,
{a=
{b='sex'}
}
)
2022-12-07 18:26:11 +00:00
lu.assertIs(table.len(errors), 1)
2022-12-07 18:12:43 +00:00
end