This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail.git


The following commit(s) were added to refs/heads/master by this push:
     new fd7f714  Simple CLI tester for stats.lua query parsing
fd7f714 is described below

commit fd7f7146fa52f10a0059d8cf5a82c5272be6532f
Author: Sebb <[email protected]>
AuthorDate: Thu Sep 30 00:06:17 2021 +0100

    Simple CLI tester for stats.lua query parsing
---
 test/stats_cli.lua | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/test/stats_cli.lua b/test/stats_cli.lua
new file mode 100755
index 0000000..8c285da
--- /dev/null
+++ b/test/stats_cli.lua
@@ -0,0 +1,97 @@
+#!/usr/bin/env lua
+
+-- Allow CLI testing of stats.lua query parameters
+-- Invoke with pairs of parameters, being the key and value, e.g.
+-- lua test_stats.lua list dev domain ponymail.apache.org q 'a/b' d lte=1y
+-- set MODE=inspect to print output query from inspect, otherwise print as 
JSON suitable for further processing
+
+-- Update path so we can always find the api/*.lua scripts
+local self=arg[0] or './dummy' -- get path to self
+local pfx=self:match("^.*/") or "" -- extract the path
+-- finally update package path (also adding current dir)
+package.path = package.path .. ";" .. pfx .. "../site/api/?.lua"   -- path to 
api dir
+package.path = package.path .. ";" .. pfx .. "/?.lua" -- current dir
+
+local inspect = require 'inspect'
+local http = require 'socket.http'
+local mock = require 'mock_r'
+local JSON = require 'cjson'
+require 'stats' -- local makes no difference here
+
+local _CACHE = {} -- capture output
+
+-- override http request so can capture the query
+http.request = function(url, data)
+  -- capture HTTP parameters (assume only called once)
+  _CACHE.url = url
+  _CACHE.query = JSON.decode(data)
+  -- return simplest result that satisfies stats.lua
+  result = [[
+{
+  "hits" : {
+  "total" : 0,
+  "hits" : [ ]
+  }
+}
+]]
+  return result, 200
+end
+
+
+local r = mock.r
+
+-- disable years active check
+r.ivm_get = function(r, key)
+  return JSON.encode({ pubfirst = 0, publast = 0})
+end
+
+-- collect output (assume only one call to puts)
+r.puts = function(r, ...) _CACHE.puts = JSON.decode(...) end
+
+-- TODO
+r.escape_html = function(r, val) 
+    return val
+end
+
+-- override the parse-args function so it returns our test data
+r.parseargs = function(r)
+  return _CACHE.args
+end
+
+
+local function test(args)
+  local output = {
+    quick = true, -- disable most queries
+  }
+  -- merge in user data
+  for k,v in pairs(args) do output[k] = v end  
+  _CACHE.args = output -- save the args
+  _CACHE.status = handle(r)
+  return _CACHE
+end
+
+local argc = #arg
+if argc % 2 == 0
+then
+  local data = {}
+  for i = 1,argc,2 do
+    data[arg[i]] = arg[i+1]
+  end
+  res = test(data)
+  if os.getenv("MODE") == "inspect" then
+    print(inspect(res["query"]))
+  else
+    print(JSON.encode(res))
+  end
+else
+  print("Need even arg count")
+end
+-- for k,v in pairs(arg) do
+--   if k > 0
+--   then
+--    print(v) 
+--    test(JSON.decode(v))
+--   end
+-- end
+-- test({d='lte=123d'},{gte = "now-123d", lte ='now+1d'})
+-- test({d='gte=123d', header_from='a/b/c'},{lte = "now-123d"})
\ No newline at end of file

Reply via email to