diff --git a/tests/include/api_sql_common.sh b/tests/include/api_sql_common.sh index a1915c7..577c8ab 100644 --- a/tests/include/api_sql_common.sh +++ b/tests/include/api_sql_common.sh @@ -19,14 +19,15 @@ end function event() local e, m + local inspect = require("inspect") print("drv:name() = " .. drv:name()) print("SQL types:") - for k,v in pairs(sysbench.sql.type) do print(k .. " = " .. v) end + print(inspect(sysbench.sql.type)) print('--') print("SQL error codes:") - for k,v in pairs(sysbench.sql.error) do print(k .. " = " .. v) end + print(inspect(sysbench.sql.error)) print('--') e, m = pcall(sysbench.sql.driver, "non-existing") @@ -146,7 +147,8 @@ sysbench $SB_ARGS --mysql-host="non-existing" --pgsql-host="non-existing" run cat >$CRAMTMP/api_sql.lua < '\\a', \0 => '\\0', 31 => '\31' +local shortControlCharEscapes = { + ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", + ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v" +} +local longControlCharEscapes = {} -- \a => nil, \0 => \000, 31 => \031 +for i=0, 31 do + local ch = string.char(i) + if not shortControlCharEscapes[ch] then + shortControlCharEscapes[ch] = "\\"..i + longControlCharEscapes[ch] = string.format("\\%03d", i) + end +end + +local function escape(str) + return (str:gsub("\\", "\\\\") + :gsub("(%c)%f[0-9]", longControlCharEscapes) + :gsub("%c", shortControlCharEscapes)) +end + +local function isIdentifier(str) + return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" ) +end + +local function isSequenceKey(k, sequenceLength) + return type(k) == 'number' + and 1 <= k + and k <= sequenceLength + and math.floor(k) == k +end + +local defaultTypeOrders = { + ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4, + ['function'] = 5, ['userdata'] = 6, ['thread'] = 7 +} + +local function sortKeys(a, b) + local ta, tb = type(a), type(b) + + -- strings and numbers are sorted numerically/alphabetically + if ta == tb and (ta == 'string' or ta == 'number') then return a < b end + + local dta, dtb = defaultTypeOrders[ta], defaultTypeOrders[tb] + -- Two default types are compared according to the defaultTypeOrders table + if dta and dtb then return defaultTypeOrders[ta] < defaultTypeOrders[tb] + elseif dta then return true -- default types before custom ones + elseif dtb then return false -- custom types after default ones + end + + -- custom types are sorted out alphabetically + return ta < tb +end + +-- For implementation reasons, the behavior of rawlen & # is "undefined" when +-- tables aren't pure sequences. So we implement our own # operator. +local function getSequenceLength(t) + local len = 1 + local v = rawget(t,len) + while v ~= nil do + len = len + 1 + v = rawget(t,len) + end + return len - 1 +end + +local function getNonSequentialKeys(t) + local keys = {} + local sequenceLength = getSequenceLength(t) + for k,_ in pairs(t) do + if not isSequenceKey(k, sequenceLength) then table.insert(keys, k) end + end + table.sort(keys, sortKeys) + return keys, sequenceLength +end + +local function getToStringResultSafely(t, mt) + local __tostring = type(mt) == 'table' and rawget(mt, '__tostring') + local str, ok + if type(__tostring) == 'function' then + ok, str = pcall(__tostring, t) + str = ok and str or 'error: ' .. tostring(str) + end + if type(str) == 'string' and #str > 0 then return str end +end + +local function countTableAppearances(t, tableAppearances) + tableAppearances = tableAppearances or {} + + if type(t) == 'table' then + if not tableAppearances[t] then + tableAppearances[t] = 1 + for k,v in pairs(t) do + countTableAppearances(k, tableAppearances) + countTableAppearances(v, tableAppearances) + end + countTableAppearances(getmetatable(t), tableAppearances) + else + tableAppearances[t] = tableAppearances[t] + 1 + end + end + + return tableAppearances +end + +local copySequence = function(s) + local copy, len = {}, #s + for i=1, len do copy[i] = s[i] end + return copy, len +end + +local function makePath(path, ...) + local keys = {...} + local newPath, len = copySequence(path) + for i=1, #keys do + newPath[len + i] = keys[i] + end + return newPath +end + +local function processRecursive(process, item, path, visited) + + if item == nil then return nil end + if visited[item] then return visited[item] end + + local processed = process(item, path) + if type(processed) == 'table' then + local processedCopy = {} + visited[item] = processedCopy + local processedKey + + for k,v in pairs(processed) do + processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited) + if processedKey ~= nil then + processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited) + end + end + + local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited) + setmetatable(processedCopy, mt) + processed = processedCopy + end + return processed +end + + + +------------------------------------------------------------------- + +local Inspector = {} +local Inspector_mt = {__index = Inspector} + +function Inspector:puts(...) + local args = {...} + local buffer = self.buffer + local len = #buffer + for i=1, #args do + len = len + 1 + buffer[len] = args[i] + end +end + +function Inspector:down(f) + self.level = self.level + 1 + f() + self.level = self.level - 1 +end + +function Inspector:tabify() + self:puts(self.newline, string.rep(self.indent, self.level)) +end + +function Inspector:alreadyVisited(v) + return self.ids[v] ~= nil +end + +function Inspector:getId(v) + local id = self.ids[v] + if not id then + local tv = type(v) + id = (self.maxIds[tv] or 0) + 1 + self.maxIds[tv] = id + self.ids[v] = id + end + return tostring(id) +end + +function Inspector:putKey(k) + if isIdentifier(k) then return self:puts(k) end + self:puts("[") + self:putValue(k) + self:puts("]") +end + +function Inspector:putTable(t) + if t == inspect.KEY or t == inspect.METATABLE then + self:puts(tostring(t)) + elseif self:alreadyVisited(t) then + self:puts('') + elseif self.level >= self.depth then + self:puts('{...}') + else + if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end + + local nonSequentialKeys, sequenceLength = getNonSequentialKeys(t) + local mt = getmetatable(t) + local toStringResult = getToStringResultSafely(t, mt) + + self:puts('{') + self:down(function() + if toStringResult then + self:puts(' -- ', escape(toStringResult)) + if sequenceLength >= 1 then self:tabify() end + end + + local count = 0 + for i=1, sequenceLength do + if count > 0 then self:puts(',') end + self:puts(' ') + self:putValue(t[i]) + count = count + 1 + end + + for _,k in ipairs(nonSequentialKeys) do + if count > 0 then self:puts(',') end + self:tabify() + self:putKey(k) + self:puts(' = ') + self:putValue(t[k]) + count = count + 1 + end + + if mt then + if count > 0 then self:puts(',') end + self:tabify() + self:puts(' = ') + self:putValue(mt) + end + end) + + if #nonSequentialKeys > 0 or mt then -- result is multi-lined. Justify closing } + self:tabify() + elseif sequenceLength > 0 then -- array tables have one extra space before closing } + self:puts(' ') + end + + self:puts('}') + end +end + +function Inspector:putValue(v) + local tv = type(v) + + if tv == 'string' then + self:puts(smartQuote(escape(v))) + elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or + tv == 'cdata' or tv == 'ctype' then + self:puts(tostring(v)) + elseif tv == 'table' then + self:putTable(v) + else + self:puts('<',tv,' ',self:getId(v),'>') + end +end + +------------------------------------------------------------------- + +function inspect.inspect(root, options) + options = options or {} + + local depth = options.depth or math.huge + local newline = options.newline or '\n' + local indent = options.indent or ' ' + local process = options.process + + if process then + root = processRecursive(process, root, {}, {}) + end + + local inspector = setmetatable({ + depth = depth, + level = 0, + buffer = {}, + ids = {}, + maxIds = {}, + newline = newline, + indent = indent, + tableAppearances = countTableAppearances(root) + }, Inspector_mt) + + inspector:putValue(root) + + return table.concat(inspector.buffer) +end + +setmetatable(inspect, { __call = function(_, ...) return inspect.inspect(...) end }) + +return inspect diff --git a/tests/t/api_sql_mysql.t b/tests/t/api_sql_mysql.t index d80b375..b06a502 100644 --- a/tests/t/api_sql_mysql.t +++ b/tests/t/api_sql_mysql.t @@ -6,24 +6,28 @@ SQL Lua API + MySQL tests $ . ${SBTEST_INCDIR}/api_sql_common.sh drv:name() = mysql SQL types: - NONE = 0 - INT = 3 - CHAR = 11 - VARCHAR = 12 - TIMESTAMP = 10 - TIME = 7 - FLOAT = 5 - TINYINT = 1 - BIGINT = 4 - SMALLINT = 2 - DATE = 8 - DATETIME = 9 - DOUBLE = 6 + { + BIGINT = 4, + CHAR = 11, + DATE = 8, + DATETIME = 9, + DOUBLE = 6, + FLOAT = 5, + INT = 3, + NONE = 0, + SMALLINT = 2, + TIME = 7, + TIMESTAMP = 10, + TINYINT = 1, + VARCHAR = 12 + } -- SQL error codes: - NONE = 0 - FATAL = 2 - IGNORABLE = 1 + { + FATAL = 2, + IGNORABLE = 1, + NONE = 0 + } -- FATAL: invalid database driver name: 'non-existing' failed to initialize the DB driver @@ -69,27 +73,33 @@ SQL Lua API + MySQL tests -- FATAL: mysql_drv_query() returned error 1048 (Column 'a' cannot be null) for query 'INSERT INTO t VALUES (NULL)' Got an error descriptor: - sql_errno = \t1048 (esc) - connection = \t (esc) - query = \tINSERT INTO t VALUES (NULL) (esc) - sql_state = \t23000 (esc) - sql_errmsg = \tColumn 'a' cannot be null (esc) + { + connection = , + query = "INSERT INTO t VALUES (NULL)", + sql_errmsg = "Column 'a' cannot be null", + sql_errno = 1048, + sql_state = "23000" + } */api_sql.lua:*: SQL error, errno = 1048, state = '23000': Column 'a' cannot be null (glob) FATAL: mysql_drv_query() returned error 1406 (Data too long for column 'a' at row 1) for query 'INSERT INTO t VALUES ('test')' Got an error descriptor: - sql_errno = \t1406 (esc) - connection = \t (esc) - query = \tINSERT INTO t VALUES ('test') (esc) - sql_state = \t22001 (esc) - sql_errmsg = \tData too long for column 'a' at row 1 (esc) + { + connection = , + query = "INSERT INTO t VALUES ('test')", + sql_errmsg = "Data too long for column 'a' at row 1", + sql_errno = 1406, + sql_state = "22001" + } */api_sql.lua:*: SQL error, errno = 1406, state = '22001': Data too long for column 'a' at row 1 (glob) FATAL: mysql_drv_query() returned error 1051 (Unknown table '*t') for query 'DROP TABLE t' (glob) Got an error descriptor: - sql_errno = \t1051 (esc) - connection = \t (esc) - query = \tDROP TABLE t (esc) - sql_state = \t42S02 (esc) - sql_errmsg = \tUnknown table '*t' (esc) (glob) + { + connection = , + query = "DROP TABLE t", + sql_errmsg = "Unknown table 'sbtest.t'", + sql_errno = 1051, + sql_state = "42S02" + } */api_sql.lua:*: SQL error, errno = 1051, state = '42S02': Unknown table '*t' (glob) -- 1 diff --git a/tests/t/api_sql_pgsql.t b/tests/t/api_sql_pgsql.t index 4c7a6e9..13b5b2c 100644 --- a/tests/t/api_sql_pgsql.t +++ b/tests/t/api_sql_pgsql.t @@ -6,24 +6,28 @@ SQL Lua API + PostgreSQL tests $ . ${SBTEST_INCDIR}/api_sql_common.sh drv:name() = pgsql SQL types: - NONE = 0 - INT = 3 - CHAR = 11 - VARCHAR = 12 - TIMESTAMP = 10 - TIME = 7 - FLOAT = 5 - TINYINT = 1 - BIGINT = 4 - SMALLINT = 2 - DATE = 8 - DATETIME = 9 - DOUBLE = 6 + { + BIGINT = 4, + CHAR = 11, + DATE = 8, + DATETIME = 9, + DOUBLE = 6, + FLOAT = 5, + INT = 3, + NONE = 0, + SMALLINT = 2, + TIME = 7, + TIMESTAMP = 10, + TINYINT = 1, + VARCHAR = 12 + } -- SQL error codes: - NONE = 0 - FATAL = 2 - IGNORABLE = 1 + { + FATAL = 2, + IGNORABLE = 1, + NONE = 0 + } -- FATAL: invalid database driver name: 'non-existing' failed to initialize the DB driver @@ -73,29 +77,35 @@ SQL Lua API + PostgreSQL tests FATAL: PQexec() failed: 7 null value in column "a" violates not-null constraint FATAL: failed query was: INSERT INTO t VALUES (NULL) Got an error descriptor: - sql_errno = \t0 (esc) - connection = \t (esc) - query = \tINSERT INTO t VALUES (NULL) (esc) - sql_state = \t23502 (esc) - sql_errmsg = \tnull value in column "a" violates not-null constraint (esc) + { + connection = , + query = "INSERT INTO t VALUES (NULL)", + sql_errmsg = 'null value in column "a" violates not-null constraint', + sql_errno = 0, + sql_state = "23502" + } */api_sql.lua:*: SQL error, errno = 0, state = '23502': null value in column "a" violates not-null constraint (glob) FATAL: PQexec() failed: 7 value too long for type character(1) FATAL: failed query was: INSERT INTO t VALUES ('test') Got an error descriptor: - sql_errno = \t0 (esc) - connection = \t (esc) - query = \tINSERT INTO t VALUES ('test') (esc) - sql_state = \t22001 (esc) - sql_errmsg = \tvalue too long for type character(1) (esc) + { + connection = , + query = "INSERT INTO t VALUES ('test')", + sql_errmsg = "value too long for type character(1)", + sql_errno = 0, + sql_state = "22001" + } */api_sql.lua:*: SQL error, errno = 0, state = '22001': value too long for type character(1) (glob) FATAL: PQexec() failed: 7 table "t" does not exist FATAL: failed query was: DROP TABLE t Got an error descriptor: - sql_errno = \t0 (esc) - connection = \t (esc) - query = \tDROP TABLE t (esc) - sql_state = \t42P01 (esc) - sql_errmsg = \ttable "t" does not exist (esc) + { + connection = , + query = "DROP TABLE t", + sql_errmsg = 'table "t" does not exist', + sql_errno = 0, + sql_state = "42P01" + } */api_sql.lua:*: SQL error, errno = 0, state = '42P01': table "t" does not exist (glob) -- 1 diff --git a/tests/t/cmdline.t b/tests/t/cmdline.t index 4e2e339..277e735 100644 --- a/tests/t/cmdline.t +++ b/tests/t/cmdline.t @@ -415,10 +415,8 @@ Command line options tests $ cat >cmdline.lua < function print_cmd() - > for k, v in pairs(sysbench.cmdline.argv) do - > print(string.format("argv[%u] = %s", k, v)) - > end - > print(string.format("sysbench.cmdline.command = %s", sysbench.cmdline.command)) + > print("argv = " .. require("inspect")(sysbench.cmdline.argv)) + > print(string.format("sysbench.cmdline.command = %s",sysbench.cmdline.command)) > end > function prepare() > print_cmd() @@ -428,25 +426,20 @@ Command line options tests $ sysbench --opt1 --opt2=val cmdline.lua sysbench * (glob) - argv[0] = sysbench - argv[1] = --opt1 - argv[2] = --opt2=val - argv[3] = cmdline.lua + argv = { "--opt1", "--opt2=val", "cmdline.lua", + [0] = "sysbench" + } sysbench.cmdline.command = nil $ sysbench --opt1 --opt2=val cmdline.lua prepare sysbench * (glob) - argv[0] = sysbench - argv[1] = --opt1 - argv[2] = --opt2=val - argv[3] = cmdline.lua - argv[4] = prepare + argv = { "--opt1", "--opt2=val", "cmdline.lua", "prepare", + [0] = "sysbench" + } sysbench.cmdline.command = prepare - argv[0] = sysbench - argv[1] = --opt1 - argv[2] = --opt2=val - argv[3] = cmdline.lua - argv[4] = prepare + argv = { "--opt1", "--opt2=val", "cmdline.lua", "prepare", + [0] = "sysbench" + } sysbench.cmdline.command = prepare $ sysbench - <