Skip to content

Commit 1635f16

Browse files
committed
test: add a test for the playground script
1 parent 7750c79 commit 1635f16

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

test/doc/playground_test.lua

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
local yaml = require('yaml')
2+
local t = require('luatest')
3+
local g = t.group()
4+
5+
local popen_ok, popen = pcall(require, 'popen')
6+
7+
g.before_all(function()
8+
t.skip_if(not popen_ok, 'no built-in popen module')
9+
t.skip_if(jit.os == 'OSX', 'popen is broken on Mac OS: ' ..
10+
'https://github.com/tarantool/tarantool/issues/6674')
11+
end)
12+
13+
-- Run ./doc/playground.lua, execute a request and compare the
14+
-- output with reference return values.
15+
--
16+
-- The first arguments is the request string. All the following
17+
-- arguments are expected return values (as Lua values).
18+
--
19+
-- The function ignores trailing `null` values in the YAML
20+
-- output.
21+
local function check_request(request, ...)
22+
local ph, err = popen.new({'./doc/playground.lua'}, {
23+
stdin = popen.opts.PIPE,
24+
stdout = popen.opts.PIPE,
25+
stderr = popen.opts.DEVNULL,
26+
})
27+
if ph == nil then
28+
error('popen.new: ' .. tostring(err))
29+
end
30+
31+
local ok, err = ph:write(request, {timeout = 1})
32+
if not ok then
33+
ph:close()
34+
error('ph:write: ' .. tostring(err))
35+
end
36+
ph:shutdown({stdin = true})
37+
38+
-- Read everything until EOF.
39+
local chunks = {}
40+
while true do
41+
local chunk, err = ph:read()
42+
if chunk == nil then
43+
ph:close()
44+
error('ph:read: ' .. tostring(err))
45+
end
46+
if chunk == '' then break end -- EOF
47+
table.insert(chunks, chunk)
48+
end
49+
50+
local status = ph:wait()
51+
assert(status.state == popen.state.EXITED)
52+
53+
-- Glue all chunks, parse response.
54+
local stdout = table.concat(chunks)
55+
local response_yaml = string.match(stdout, '%-%-%-.-%.%.%.')
56+
local response = yaml.decode(response_yaml)
57+
58+
-- NB: This call does NOT differentiate `nil` and `box.NULL`.
59+
t.assert_equals(response, {...})
60+
end
61+
62+
local cases = {
63+
test_select_customers = {
64+
request = "crud.select('customers', {{'<=', 'age', 35}}, {first = 10})",
65+
retval_1 = {
66+
metadata = {
67+
{name = 'id', type = 'unsigned'},
68+
{name = 'bucket_id', type = 'unsigned'},
69+
{name = 'name', type = 'string'},
70+
{name = 'age', type = 'number'},
71+
},
72+
rows = {
73+
{5, 1172, 'Jack', 35},
74+
{3, 2804, 'David', 33},
75+
{6, 1064, 'William', 25},
76+
{7, 693, 'Elizabeth', 18},
77+
{1, 477, 'Elizabeth', 12},
78+
},
79+
}
80+
},
81+
test_select_developers = {
82+
request = "crud.select('developers', nil, {first = 6})",
83+
retval_1 = {
84+
metadata = {
85+
{name = 'id', type = 'unsigned'},
86+
{name = 'bucket_id', type = 'unsigned'},
87+
{name = 'name', type = 'string'},
88+
{name = 'surname', type = 'string'},
89+
{name = 'age', type = 'number'},
90+
},
91+
rows = {
92+
{1, 477, 'Alexey', 'Adams', 20},
93+
{2, 401, 'Sergey', 'Allred', 21},
94+
{3, 2804, 'Pavel', 'Adams', 27},
95+
{4, 1161, 'Mikhail', 'Liston', 51},
96+
{5, 1172, 'Dmitry', 'Jacobi', 16},
97+
{6, 1064, 'Alexey', 'Sidorov', 31},
98+
},
99+
},
100+
},
101+
test_insert = {
102+
request = ("crud.insert('developers', %s)"):format(
103+
"{100, nil, 'Alfred', 'Hitchcock', 123}"),
104+
retval_1 = {
105+
metadata = {
106+
{name = 'id', type = 'unsigned'},
107+
{name = 'bucket_id', type = 'unsigned'},
108+
{name = 'name', type = 'string'},
109+
{name = 'surname', type = 'string'},
110+
{name = 'age', type = 'number'},
111+
},
112+
rows = {
113+
{100, 2976, 'Alfred', 'Hitchcock', 123},
114+
},
115+
}
116+
},
117+
test_error = {
118+
request = [[
119+
do
120+
local res, err = crud.select('non_existent', nil, {first = 10})
121+
return res, err and err.err or nil
122+
end
123+
]],
124+
retval_1 = box.NULL,
125+
retval_2 = 'Space "non_existent" doesn\'t exist',
126+
},
127+
}
128+
129+
for case_name, case in pairs(cases) do
130+
g[case_name] = function()
131+
check_request(case.request, case.retval_1, case.retval_2)
132+
end
133+
end

0 commit comments

Comments
 (0)