|
| 1 | +import sys |
| 2 | +import unittest |
| 3 | + |
| 4 | +import decimal |
| 5 | +import msgpack |
| 6 | + |
| 7 | +import tarantool |
| 8 | +import tarantool.msgpack_ext.decimal as ext_decimal |
| 9 | + |
| 10 | +from .lib.skip import skip_or_run_decimal_test, skip_or_run_varbinary_test |
| 11 | +from .lib.tarantool_server import TarantoolServer |
| 12 | + |
| 13 | +class TestSuite_Connection(unittest.TestCase): |
| 14 | + @classmethod |
| 15 | + def setUpClass(self): |
| 16 | + print(' CONNECTION '.center(70, '='), file=sys.stderr) |
| 17 | + print('-' * 70, file=sys.stderr) |
| 18 | + self.srv = TarantoolServer() |
| 19 | + self.srv.script = 'test/suites/box.lua' |
| 20 | + self.srv.start() |
| 21 | + |
| 22 | + self.adm = self.srv.admin |
| 23 | + self.adm(r""" |
| 24 | + box.schema.user.create('test', {password = 'test', if_not_exists = true}) |
| 25 | + box.schema.user.grant('test', 'read,write,execute', 'universe') |
| 26 | +
|
| 27 | + box.schema.create_space('space_varbin') |
| 28 | +
|
| 29 | + box.space['space_varbin']:format({ |
| 30 | + { |
| 31 | + 'id', |
| 32 | + type = 'number', |
| 33 | + is_nullable = false |
| 34 | + }, |
| 35 | + { |
| 36 | + 'varbin', |
| 37 | + type = 'varbinary', |
| 38 | + is_nullable = false, |
| 39 | + } |
| 40 | + }) |
| 41 | +
|
| 42 | + box.space['space_varbin']:create_index('id', { |
| 43 | + type = 'tree', |
| 44 | + parts = {1, 'number'}, |
| 45 | + unique = true}) |
| 46 | +
|
| 47 | + box.space['space_varbin']:create_index('varbin', { |
| 48 | + type = 'tree', |
| 49 | + parts = {2, 'varbinary'}, |
| 50 | + unique = true}) |
| 51 | + """) |
| 52 | + |
| 53 | + def setUp(self): |
| 54 | + # prevent a remote tarantool from clean our session |
| 55 | + if self.srv.is_started(): |
| 56 | + self.srv.touch_lock() |
| 57 | + |
| 58 | + @skip_or_run_decimal_test |
| 59 | + def test_custom_packer(self): |
| 60 | + def my_ext_type_encoder(obj): |
| 61 | + if isinstance(obj, decimal.Decimal): |
| 62 | + obj = obj + 1 |
| 63 | + return msgpack.ExtType(ext_decimal.EXT_ID, ext_decimal.encode(obj, None)) |
| 64 | + raise TypeError("Unknown type: %r" % (obj,)) |
| 65 | + |
| 66 | + def my_packer_factory(_): |
| 67 | + return msgpack.Packer(default=my_ext_type_encoder) |
| 68 | + |
| 69 | + self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'], |
| 70 | + user='test', password='test', |
| 71 | + packer_factory=my_packer_factory) |
| 72 | + |
| 73 | + resp = self.con.eval("return ...", (decimal.Decimal('27756'),)) |
| 74 | + self.assertSequenceEqual(resp, [decimal.Decimal('27757')]) |
| 75 | + |
| 76 | + def test_custom_packer_supersedes_encoding(self): |
| 77 | + def my_packer_factory(_): |
| 78 | + return msgpack.Packer(use_bin_type=False) |
| 79 | + |
| 80 | + self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'], |
| 81 | + user='test', password='test', |
| 82 | + encoding='utf-8', |
| 83 | + packer_factory=my_packer_factory) |
| 84 | + |
| 85 | + # bytes -> mp_str (string) for encoding=None |
| 86 | + # bytes -> mp_bin (varbinary) for encoding='utf-8' |
| 87 | + resp = self.con.eval("return type(...)", (bytes(bytearray.fromhex('DEADBEAF0103')),)) |
| 88 | + self.assertSequenceEqual(resp, ['string']) |
| 89 | + |
| 90 | + @skip_or_run_decimal_test |
| 91 | + def test_custom_unpacker(self): |
| 92 | + def my_ext_type_decoder(code, data): |
| 93 | + if code == ext_decimal.EXT_ID: |
| 94 | + return ext_decimal.decode(data, None) - 1 |
| 95 | + raise NotImplementedError("Unknown msgpack extension type code %d" % (code,)) |
| 96 | + |
| 97 | + def my_unpacker_factory(_): |
| 98 | + if msgpack.version >= (1, 0, 0): |
| 99 | + return msgpack.Unpacker(ext_hook=my_ext_type_decoder, strict_map_key=False) |
| 100 | + return msgpack.Unpacker(ext_hook=my_ext_type_decoder) |
| 101 | + |
| 102 | + |
| 103 | + self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'], |
| 104 | + user='test', password='test', |
| 105 | + unpacker_factory=my_unpacker_factory) |
| 106 | + |
| 107 | + resp = self.con.eval("return require('decimal').new('27756')") |
| 108 | + self.assertSequenceEqual(resp, [decimal.Decimal('27755')]) |
| 109 | + |
| 110 | + @skip_or_run_varbinary_test |
| 111 | + def test_custom_unpacker_supersedes_encoding(self): |
| 112 | + def my_unpacker_factory(_): |
| 113 | + if msgpack.version >= (0, 5, 2): |
| 114 | + if msgpack.version >= (1, 0, 0): |
| 115 | + return msgpack.Unpacker(raw=True, strict_map_key=False) |
| 116 | + |
| 117 | + return msgpack.Unpacker(raw=True) |
| 118 | + return msgpack.Unpacker(encoding=None) |
| 119 | + |
| 120 | + self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'], |
| 121 | + user='test', password='test', |
| 122 | + encoding='utf-8', |
| 123 | + unpacker_factory=my_unpacker_factory) |
| 124 | + |
| 125 | + data_id = 1 |
| 126 | + data_hex = 'DEADBEAF' |
| 127 | + data = bytes(bytearray.fromhex(data_hex)) |
| 128 | + space = 'space_varbin' |
| 129 | + |
| 130 | + self.con.execute(""" |
| 131 | + INSERT INTO "%s" VALUES (%d, x'%s'); |
| 132 | + """ % (space, data_id, data_hex)) |
| 133 | + |
| 134 | + resp = self.con.execute(""" |
| 135 | + SELECT * FROM "%s" WHERE "varbin" == x'%s'; |
| 136 | + """ % (space, data_hex)) |
| 137 | + self.assertSequenceEqual(resp, [[data_id, data]]) |
| 138 | + |
| 139 | + def test_custom_unpacker_supersedes_use_list(self): |
| 140 | + def my_unpacker_factory(_): |
| 141 | + if msgpack.version >= (1, 0, 0): |
| 142 | + return msgpack.Unpacker(use_list=False, strict_map_key=False) |
| 143 | + return msgpack.Unpacker(use_list=False) |
| 144 | + |
| 145 | + self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'], |
| 146 | + user='test', password='test', |
| 147 | + use_list=True, |
| 148 | + unpacker_factory=my_unpacker_factory) |
| 149 | + |
| 150 | + resp = self.con.eval("return {1, 2, 3}") |
| 151 | + self.assertIsInstance(resp[0], tuple) |
| 152 | + |
| 153 | + @classmethod |
| 154 | + def tearDown(self): |
| 155 | + if hasattr(self, 'con'): |
| 156 | + self.con.close() |
| 157 | + |
| 158 | + @classmethod |
| 159 | + def tearDownClass(self): |
| 160 | + self.srv.stop() |
| 161 | + self.srv.clean() |
0 commit comments