|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import sys |
| 6 | +import unittest |
| 7 | +import msgpack |
| 8 | +import warnings |
| 9 | +import tarantool |
| 10 | +import pandas |
| 11 | + |
| 12 | +from tarantool.msgpack_ext.packer import default as packer_default |
| 13 | +from tarantool.msgpack_ext.unpacker import ext_hook as unpacker_ext_hook |
| 14 | + |
| 15 | +from .lib.tarantool_server import TarantoolServer |
| 16 | +from .lib.skip import skip_or_run_datetime_test |
| 17 | +from tarantool.error import MsgpackError, MsgpackWarning |
| 18 | + |
| 19 | +class TestSuite_Datetime(unittest.TestCase): |
| 20 | + @classmethod |
| 21 | + def setUpClass(self): |
| 22 | + print(' DATETIME EXT TYPE '.center(70, '='), file=sys.stderr) |
| 23 | + print('-' * 70, file=sys.stderr) |
| 24 | + self.srv = TarantoolServer() |
| 25 | + self.srv.script = 'test/suites/box.lua' |
| 26 | + self.srv.start() |
| 27 | + |
| 28 | + self.adm = self.srv.admin |
| 29 | + self.adm(r""" |
| 30 | + _, datetime = pcall(require, 'datetime') |
| 31 | +
|
| 32 | + box.schema.space.create('test') |
| 33 | + box.space['test']:create_index('primary', { |
| 34 | + type = 'tree', |
| 35 | + parts = {1, 'string'}, |
| 36 | + unique = true}) |
| 37 | +
|
| 38 | + box.schema.user.create('test', {password = 'test', if_not_exists = true}) |
| 39 | + box.schema.user.grant('test', 'read,write,execute', 'universe') |
| 40 | + """) |
| 41 | + |
| 42 | + self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'], |
| 43 | + user='test', password='test') |
| 44 | + |
| 45 | + def setUp(self): |
| 46 | + # prevent a remote tarantool from clean our session |
| 47 | + if self.srv.is_started(): |
| 48 | + self.srv.touch_lock() |
| 49 | + |
| 50 | + self.adm("box.space['test']:truncate()") |
| 51 | + |
| 52 | + |
| 53 | + cases = { |
| 54 | + 'date': { |
| 55 | + 'python': tarantool.Datetime(year=2022, month=8, day=31), |
| 56 | + 'msgpack': (b'\x80\xa4\x0e\x63\x00\x00\x00\x00'), |
| 57 | + 'tarantool': r"datetime.new({year=2022, month=8, day=31})", |
| 58 | + }, |
| 59 | + 'date_unix_start': { |
| 60 | + 'python': tarantool.Datetime(year=1970, month=1, day=1), |
| 61 | + 'msgpack': (b'\x00\x00\x00\x00\x00\x00\x00\x00'), |
| 62 | + 'tarantool': r"datetime.new({year=1970, month=1, day=1})", |
| 63 | + }, |
| 64 | + 'date_before_1970': { |
| 65 | + 'python': tarantool.Datetime(year=1900, month=1, day=1), |
| 66 | + 'msgpack': (b'\x80\x81\x55\x7c\xff\xff\xff\xff'), |
| 67 | + 'tarantool': r"datetime.new({year=1900, month=1, day=1})", |
| 68 | + }, |
| 69 | + 'datetime_with_minutes': { |
| 70 | + 'python': tarantool.Datetime(year=2022, month=8, day=31, hour=18, minute=7), |
| 71 | + 'msgpack': (b'\x44\xa3\x0f\x63\x00\x00\x00\x00'), |
| 72 | + 'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7})", |
| 73 | + }, |
| 74 | + 'datetime_with_seconds': { |
| 75 | + 'python': tarantool.Datetime(year=2022, month=8, day=31, hour=18, minute=7, second=54), |
| 76 | + 'msgpack': (b'\x7a\xa3\x0f\x63\x00\x00\x00\x00'), |
| 77 | + 'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7, sec=54})", |
| 78 | + }, |
| 79 | + 'datetime_with_microseconds': { |
| 80 | + 'python': tarantool.Datetime(year=2022, month=8, day=31, hour=18, minute=7, second=54, |
| 81 | + microsecond=308543), |
| 82 | + 'msgpack': (b'\x7a\xa3\x0f\x63\x00\x00\x00\x00\x18\xfe\x63\x12\x00\x00\x00\x00'), |
| 83 | + 'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7, sec=54, " + |
| 84 | + r"nsec=308543000})", |
| 85 | + }, |
| 86 | + 'datetime_with_nanoseconds': { |
| 87 | + 'python': tarantool.Datetime(year=2022, month=8, day=31, hour=18, minute=7, second=54, |
| 88 | + microsecond=308543, nanosecond=321), |
| 89 | + 'msgpack': (b'\x7a\xa3\x0f\x63\x00\x00\x00\x00\x59\xff\x63\x12\x00\x00\x00\x00'), |
| 90 | + 'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7, sec=54, " + |
| 91 | + r"nsec=308543321})", |
| 92 | + }, |
| 93 | + 'pandas_timestamp': { |
| 94 | + 'python': pandas.Timestamp(year=2022, month=8, day=31, hour=18, minute=7, second=54, |
| 95 | + microsecond=308543, nanosecond=321), |
| 96 | + 'msgpack': (b'\x7a\xa3\x0f\x63\x00\x00\x00\x00\x59\xff\x63\x12\x00\x00\x00\x00'), |
| 97 | + 'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7, sec=54, " + |
| 98 | + r"nsec=308543321})", |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + def test_msgpack_decode(self): |
| 103 | + for name in self.cases.keys(): |
| 104 | + with self.subTest(msg=name): |
| 105 | + case = self.cases[name] |
| 106 | + |
| 107 | + self.assertEqual(unpacker_ext_hook(4, case['msgpack']), |
| 108 | + case['python']) |
| 109 | + |
| 110 | + @skip_or_run_datetime_test |
| 111 | + def test_tarantool_decode(self): |
| 112 | + for name in self.cases.keys(): |
| 113 | + with self.subTest(msg=name): |
| 114 | + case = self.cases[name] |
| 115 | + |
| 116 | + self.adm(f"box.space['test']:replace{{'{name}', {case['tarantool']}}}") |
| 117 | + |
| 118 | + self.assertSequenceEqual(self.con.select('test', name), |
| 119 | + [[name, case['python']]]) |
| 120 | + |
| 121 | + def test_msgpack_encode(self): |
| 122 | + for name in self.cases.keys(): |
| 123 | + with self.subTest(msg=name): |
| 124 | + case = self.cases[name] |
| 125 | + |
| 126 | + self.assertEqual(packer_default(case['python']), |
| 127 | + msgpack.ExtType(code=4, data=case['msgpack'])) |
| 128 | + |
| 129 | + @skip_or_run_datetime_test |
| 130 | + def test_tarantool_encode(self): |
| 131 | + for name in self.cases.keys(): |
| 132 | + with self.subTest(msg=name): |
| 133 | + case = self.cases[name] |
| 134 | + |
| 135 | + self.con.insert('test', [name, case['python']]) |
| 136 | + |
| 137 | + lua_eval = f""" |
| 138 | + local dt = {case['tarantool']} |
| 139 | + |
| 140 | + local tuple = box.space['test']:get('{name}') |
| 141 | + assert(tuple ~= nil) |
| 142 | +
|
| 143 | + if tuple[2] == dt then |
| 144 | + return true |
| 145 | + else |
| 146 | + return nil, ('%s is not equal to expected %s'):format( |
| 147 | + tostring(tuple[2]), tostring(dt)) |
| 148 | + end |
| 149 | + """ |
| 150 | + |
| 151 | + self.assertSequenceEqual(self.adm(lua_eval), [True]) |
| 152 | + |
| 153 | + |
| 154 | + @classmethod |
| 155 | + def tearDownClass(self): |
| 156 | + self.con.close() |
| 157 | + self.srv.stop() |
| 158 | + self.srv.clean() |
0 commit comments