|
| 1 | +""" |
| 2 | +Original Tarantool's test box-py/call.test.py had a problem when output with |
| 3 | +running under Python 2 was not the same as with running under Python 3. |
| 4 | +
|
| 5 | +Fixed in commit e97425e46a77a4ac9d102c5f88062cb37e5a8da4 |
| 6 | +"test: make box-py/call.test.py compatible with Python 2 and 3". |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import print_function |
| 10 | + |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import json |
| 14 | + |
| 15 | +def call(name, *args): |
| 16 | + return iproto.call(name, *args) |
| 17 | + |
| 18 | +admin("box.schema.user.create('test', { password = 'test' })") |
| 19 | +admin("box.schema.user.grant('test', 'execute,read,write', 'universe')") |
| 20 | +iproto.authenticate("test", "test") |
| 21 | +admin("exp_notation = 1e123") |
| 22 | +admin("function f1() return 'testing', 1, false, -1, 1.123, math.abs(exp_notation - 1e123) < 0.1, nil end") |
| 23 | +admin("f1()") |
| 24 | +call("f1") |
| 25 | +admin("f1=nil") |
| 26 | +call("f1") |
| 27 | +admin("function f1() return f1 end") |
| 28 | +call("f1") |
| 29 | + |
| 30 | +admin("space = box.schema.space.create('tweedledum')") |
| 31 | +admin("index = space:create_index('primary', { type = 'tree' })") |
| 32 | + |
| 33 | +json_dumps_kwargs=dict(sort_keys=True, separators=(', ', ': ')) |
| 34 | + |
| 35 | +def dump_args(*args): |
| 36 | + return json.dumps(args, **json_dumps_kwargs)[1:-1] |
| 37 | + |
| 38 | +def dump_response(response): |
| 39 | + if response.return_code: |
| 40 | + return str(response) |
| 41 | + if not response.data: |
| 42 | + return '' |
| 43 | + res = [] |
| 44 | + for item in response.data: |
| 45 | + res.append(json.dumps(item, **json_dumps_kwargs)) |
| 46 | + return '- ' + '\n- '.join(res) |
| 47 | + |
| 48 | +def lua_eval(name, *args): |
| 49 | + print("eval ({})({})".format(name, dump_args(*args))) |
| 50 | + print("---") |
| 51 | + print(dump_response(iproto.py_con.eval(name, args))) |
| 52 | + |
| 53 | +def lua_call(name, *args): |
| 54 | + print("call {}({})".format(name, dump_args(*args))) |
| 55 | + print("---") |
| 56 | + print(dump_response(iproto.py_con.call(name, args))) |
| 57 | + |
| 58 | +def test(expr, *args): |
| 59 | + lua_eval("return " + expr, *args) |
| 60 | + admin("function f(...) return " + expr + " end") |
| 61 | + lua_call("f", *args) |
| 62 | + |
| 63 | +test("{k1 = 'v1', k2 = 'v2'}") |
| 64 | +test("{k1 = 'v1', k2 = 'v2'}") |
| 65 | + |
| 66 | +admin("space:drop()") |
| 67 | +admin("box.schema.user.drop('test')") |
| 68 | + |
| 69 | +iproto.py_con.close() |
0 commit comments