|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import sys |
| 6 | +import unittest |
| 7 | +import decimal |
| 8 | +import warnings |
| 9 | +import tarantool |
| 10 | + |
| 11 | +from .lib.tarantool_server import TarantoolServer |
| 12 | +from .lib.skip import skip_or_run_decimal_test |
| 13 | +from tarantool.error import MsgpackError, MsgpackWarning |
| 14 | + |
| 15 | +class TestSuite_ExtTypes(unittest.TestCase): |
| 16 | + @classmethod |
| 17 | + def setUpClass(self): |
| 18 | + print(' EXT TYPES '.center(70, '='), file=sys.stderr) |
| 19 | + print('-' * 70, file=sys.stderr) |
| 20 | + self.srv = TarantoolServer() |
| 21 | + self.srv.script = 'test/suites/box.lua' |
| 22 | + self.srv.start() |
| 23 | + |
| 24 | + self.adm = self.srv.admin |
| 25 | + self.adm(r""" |
| 26 | + _, decimal = pcall(require, 'decimal') |
| 27 | +
|
| 28 | + box.schema.space.create('test') |
| 29 | + box.space['test']:create_index('primary', { |
| 30 | + type = 'tree', |
| 31 | + parts = {1, 'num'}, |
| 32 | + unique = true}) |
| 33 | +
|
| 34 | + box.schema.user.create('test', {password = 'test', if_not_exists = true}) |
| 35 | + box.schema.user.grant('test', 'read,write,execute', 'universe') |
| 36 | + """) |
| 37 | + |
| 38 | + self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'], |
| 39 | + user='test', password='test') |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + # prevent a remote tarantool from clean our session |
| 43 | + if self.srv.is_started(): |
| 44 | + self.srv.touch_lock() |
| 45 | + |
| 46 | + self.adm("box.space['test']:truncate()") |
| 47 | + |
| 48 | + |
| 49 | + valid_decimal_cases = [ |
| 50 | + "0.7", |
| 51 | + "0.3", |
| 52 | + "0.00000000000000000000000000000000000001", |
| 53 | + "0.00000000000000000000000000000000000009", |
| 54 | + "-18.34", |
| 55 | + "-108.123456789", |
| 56 | + "-11111111111111111111111111111111111111", |
| 57 | + "100", |
| 58 | + "0.1", |
| 59 | + "-0.1", |
| 60 | + "0.0000000000000000000000000000000000001", |
| 61 | + "-0.0000000000000000000000000000000000001", |
| 62 | + "0.00000000000000000000000000000000000001", |
| 63 | + "-0.00000000000000000000000000000000000001", |
| 64 | + "1", |
| 65 | + "-1", |
| 66 | + "0", |
| 67 | + "-0", |
| 68 | + "0.01", |
| 69 | + "0.001", |
| 70 | + "99999999999999999999999999999999999999", |
| 71 | + "-99999999999999999999999999999999999999", |
| 72 | + "-12.34", |
| 73 | + "12.34", |
| 74 | + "1.4", |
| 75 | + "2.718281828459045", |
| 76 | + "-2.718281828459045", |
| 77 | + "3.141592653589793", |
| 78 | + "-3.141592653589793", |
| 79 | + "1234567891234567890.0987654321987654321", |
| 80 | + "-1234567891234567890.0987654321987654321", |
| 81 | + ] |
| 82 | + |
| 83 | + @skip_or_run_decimal_test |
| 84 | + def test_decimal_decode(self): |
| 85 | + for i in range(len(self.valid_decimal_cases)): |
| 86 | + with self.subTest(msg=str(i)): |
| 87 | + decimal_case = self.valid_decimal_cases[i] |
| 88 | + |
| 89 | + self.adm("box.space['test']:replace{%d, decimal.new('%s')}" % (i, decimal_case)) |
| 90 | + |
| 91 | + self.assertSequenceEqual( |
| 92 | + self.con.select('test', i), |
| 93 | + [[i, decimal.Decimal(decimal_case)]]) |
| 94 | + |
| 95 | + @skip_or_run_decimal_test |
| 96 | + def test_decimal_encode(self): |
| 97 | + for i in range(len(self.valid_decimal_cases)): |
| 98 | + with self.subTest(msg=str(i)): |
| 99 | + decimal_case = self.valid_decimal_cases[i] |
| 100 | + |
| 101 | + self.con.insert('test', [i, decimal.Decimal(decimal_case)]) |
| 102 | + |
| 103 | + lua_eval = f""" |
| 104 | + local tuple = box.space['test']:get({i}) |
| 105 | + assert(tuple ~= nil) |
| 106 | +
|
| 107 | + local dec = decimal.new('{decimal_case}') |
| 108 | + if tuple[2] == dec then |
| 109 | + return true |
| 110 | + else |
| 111 | + return nil, ('%s is not equal to expected %s'):format( |
| 112 | + tostring(tuple[2]), tostring(dec)) |
| 113 | + end |
| 114 | + """ |
| 115 | + |
| 116 | + self.assertSequenceEqual(self.con.eval(lua_eval), [True]) |
| 117 | + |
| 118 | + |
| 119 | + error_decimal_cases = [ |
| 120 | + "999999999999999999999999999999999999999", |
| 121 | + "-999999999999999999999999999999999999999", |
| 122 | + "999999999999999999900000099999999999999999999", |
| 123 | + "-999999999999999999900000099999999999999999999", |
| 124 | + "100000000000000000000000000000000000000.1", |
| 125 | + "-100000000000000000000000000000000000000.1", |
| 126 | + "1000000000000000000011110000000000000000000.1", |
| 127 | + "-1000000000000000000011110000000000000000000.1", |
| 128 | + ] |
| 129 | + |
| 130 | + @skip_or_run_decimal_test |
| 131 | + def test_decimal_encode_error(self): |
| 132 | + for i in range(len(self.error_decimal_cases)): |
| 133 | + with self.subTest(msg=str(i)): |
| 134 | + decimal_case = self.error_decimal_cases[i] |
| 135 | + |
| 136 | + msg = 'Decimal cannot be encoded: Tarantool decimal ' + \ |
| 137 | + 'supports a maximum of 38 digits.' |
| 138 | + self.assertRaisesRegex( |
| 139 | + MsgpackError, msg, |
| 140 | + lambda: self.con.insert('test', |
| 141 | + [i, decimal.Decimal(decimal_case)])) |
| 142 | + |
| 143 | + |
| 144 | + precision_loss_decimal_cases = [ |
| 145 | + { |
| 146 | + 'input': "1.00000000000000000000000000000000000001", |
| 147 | + 'result': "1", |
| 148 | + }, |
| 149 | + { |
| 150 | + 'input': "-1.00000000000000000000000000000000000001", |
| 151 | + 'result': "-1", |
| 152 | + }, |
| 153 | + { |
| 154 | + 'input': "0.000000000000000000000000000000000000001", |
| 155 | + 'result': "0", |
| 156 | + }, |
| 157 | + { |
| 158 | + 'input': "-0.000000000000000000000000000000000000001", |
| 159 | + 'result': "-0", |
| 160 | + }, |
| 161 | + { |
| 162 | + 'input': "9999999.99999900000000000000000000000000000000000001", |
| 163 | + 'result': "9999999.999999", |
| 164 | + }, |
| 165 | + { |
| 166 | + 'input': "-9999999.99999900000000000000000000000000000000000001", |
| 167 | + 'result': "-9999999.999999", |
| 168 | + }, |
| 169 | + { |
| 170 | + 'input': "99999999999999999999999999999999999999.1", |
| 171 | + 'result': "99999999999999999999999999999999999999", |
| 172 | + }, |
| 173 | + { |
| 174 | + 'input': "-99999999999999999999999999999999999999.1", |
| 175 | + 'result': "-99999999999999999999999999999999999999", |
| 176 | + }, |
| 177 | + { |
| 178 | + 'input': "99999999999999999999999999999999999999.1111111111111111111111111", |
| 179 | + 'result': "99999999999999999999999999999999999999", |
| 180 | + }, |
| 181 | + { |
| 182 | + 'input': "-99999999999999999999999999999999999999.1111111111111111111111111", |
| 183 | + 'result': "-99999999999999999999999999999999999999", |
| 184 | + }, |
| 185 | + ] |
| 186 | + |
| 187 | + @skip_or_run_decimal_test |
| 188 | + def test_decimal_encode_with_precision_loss(self): |
| 189 | + for i in range(len(self.precision_loss_decimal_cases)): |
| 190 | + with self.subTest(msg=str(i)): |
| 191 | + decimal_case = self.precision_loss_decimal_cases[i] |
| 192 | + |
| 193 | + msg = 'Decimal encoded with loss of precision: ' + \ |
| 194 | + 'Tarantool decimal supports a maximum of 38 digits.' |
| 195 | + input_decimal = decimal.Decimal(decimal_case['input']) |
| 196 | + self.assertWarnsRegex( |
| 197 | + MsgpackWarning, msg, |
| 198 | + lambda: self.con.insert('test', [i, input_decimal])) |
| 199 | + |
| 200 | + lua_eval = f""" |
| 201 | + local tuple = box.space['test']:get({i}) |
| 202 | + assert(tuple ~= nil) |
| 203 | +
|
| 204 | + local dec = decimal.new('{decimal_case['result']}') |
| 205 | + if tuple[2] == dec then |
| 206 | + return true |
| 207 | + else |
| 208 | + return nil, ('%s is not equal to expected %s'):format( |
| 209 | + tostring(tuple[2]), tostring(dec)) |
| 210 | + end |
| 211 | + """ |
| 212 | + |
| 213 | + self.assertSequenceEqual(self.con.eval(lua_eval), [True]) |
| 214 | + |
| 215 | + @classmethod |
| 216 | + def tearDownClass(self): |
| 217 | + self.con.close() |
| 218 | + self.srv.stop() |
| 219 | + self.srv.clean() |
0 commit comments