4
4
5
5
import sys
6
6
import unittest
7
+ import uuid
7
8
import decimal
8
9
import msgpack
9
10
import warnings
13
14
from tarantool .msgpack_ext .unpacker import ext_hook as unpacker_ext_hook
14
15
15
16
from .lib .tarantool_server import TarantoolServer
16
- from .lib .skip import skip_or_run_decimal_test
17
+ from .lib .skip import skip_or_run_decimal_test , skip_or_run_UUID_test
17
18
from tarantool .error import MsgpackError , MsgpackWarning
18
19
19
20
class TestSuite_MsgpackExt (unittest .TestCase ):
@@ -28,6 +29,7 @@ def setUpClass(self):
28
29
self .adm = self .srv .admin
29
30
self .adm (r"""
30
31
_, decimal = pcall(require, 'decimal')
32
+ _, uuid = pcall(require, 'uuid')
31
33
32
34
box.schema.space.create('test')
33
35
box.space['test']:create_index('primary', {
@@ -424,6 +426,85 @@ def test_decimal_tarantool_encode_with_precision_loss(self):
424
426
425
427
self .assertSequenceEqual (self .con .eval (lua_eval ), [True ])
426
428
429
+
430
+ UUID_cases = {
431
+ 'uuid_1' : {
432
+ 'python' : uuid .UUID ('ae28d4f6-076c-49dd-8227-7f9fae9592d0' ),
433
+ 'msgpack' : (b'\xae \x28 \xd4 \xf6 \x07 \x6c \x49 \xdd \x82 \x27 \x7f \x9f \xae \x95 \x92 \xd0 ' ),
434
+ 'tarantool' : "uuid.fromstr('ae28d4f6-076c-49dd-8227-7f9fae9592d0')" ,
435
+ },
436
+ 'uuid_2' : {
437
+ 'python' : uuid .UUID ('b3121301-9300-4038-a652-ead943fb9c39' ),
438
+ 'msgpack' : (b'\xb3 \x12 \x13 \x01 \x93 \x00 \x40 \x38 \xa6 \x52 \xea \xd9 \x43 \xfb \x9c \x39 ' ),
439
+ 'tarantool' : "uuid.fromstr('b3121301-9300-4038-a652-ead943fb9c39')" ,
440
+ },
441
+ 'uuid_3' : {
442
+ 'python' : uuid .UUID ('dfa69f02-92e6-44a5-abb5-84b39292ff93' ),
443
+ 'msgpack' : (b'\xdf \xa6 \x9f \x02 \x92 \xe6 \x44 \xa5 \xab \xb5 \x84 \xb3 \x92 \x92 \xff \x93 ' ),
444
+ 'tarantool' : "uuid.fromstr('dfa69f02-92e6-44a5-abb5-84b39292ff93')" ,
445
+ },
446
+ 'uuid_4' : {
447
+ 'python' : uuid .UUID ('8b69a1ce-094a-4e21-a5dc-4cdae7cd8960' ),
448
+ 'msgpack' : (b'\x8b \x69 \xa1 \xce \x09 \x4a \x4e \x21 \xa5 \xdc \x4c \xda \xe7 \xcd \x89 \x60 ' ),
449
+ 'tarantool' : "uuid.fromstr('8b69a1ce-094a-4e21-a5dc-4cdae7cd8960')" ,
450
+ },
451
+ 'uuid_5' : {
452
+ 'python' : uuid .UUID ('25932334-1d42-4686-9299-ec1a7165227c' ),
453
+ 'msgpack' : (b'\x25 \x93 \x23 \x34 \x1d \x42 \x46 \x86 \x92 \x99 \xec \x1a \x71 \x65 \x22 \x7c ' ),
454
+ 'tarantool' : "uuid.fromstr('25932334-1d42-4686-9299-ec1a7165227c')" ,
455
+ },
456
+ }
457
+
458
+ def test_UUID_msgpack_decode (self ):
459
+ for name in self .UUID_cases .keys ():
460
+ with self .subTest (msg = name ):
461
+ UUID_case = self .UUID_cases [name ]
462
+
463
+ self .assertEqual (unpacker_ext_hook (2 , UUID_case ['msgpack' ]),
464
+ UUID_case ['python' ])
465
+
466
+ @skip_or_run_UUID_test
467
+ def test_UUID_tarantool_decode (self ):
468
+ for name in self .UUID_cases .keys ():
469
+ with self .subTest (msg = name ):
470
+ UUID_case = self .UUID_cases [name ]
471
+
472
+ self .adm (f"box.space['test']:replace{{'{ name } ', { UUID_case ['tarantool' ]} }}" )
473
+
474
+ self .assertSequenceEqual (self .con .select ('test' , name ),
475
+ [[name , UUID_case ['python' ]]])
476
+
477
+ def test_UUID_msgpack_encode (self ):
478
+ for name in self .UUID_cases .keys ():
479
+ with self .subTest (msg = name ):
480
+ UUID_case = self .UUID_cases [name ]
481
+
482
+ self .assertEqual (packer_default (UUID_case ['python' ]),
483
+ msgpack .ExtType (code = 2 , data = UUID_case ['msgpack' ]))
484
+
485
+ @skip_or_run_UUID_test
486
+ def test_UUID_tarantool_encode (self ):
487
+ for name in self .UUID_cases .keys ():
488
+ with self .subTest (msg = name ):
489
+ UUID_case = self .UUID_cases [name ]
490
+
491
+ self .con .insert ('test' , [name , UUID_case ['python' ]])
492
+
493
+ lua_eval = f"""
494
+ local tuple = box.space['test']:get('{ name } ')
495
+ assert(tuple ~= nil)
496
+
497
+ local id = { UUID_case ['tarantool' ]}
498
+ if tuple[2] == id then
499
+ return true
500
+ else
501
+ return nil, ('%s is not equal to expected %s'):format(
502
+ tostring(tuple[2]), tostring(id))
503
+ end
504
+ """
505
+
506
+ self .assertSequenceEqual (self .con .eval (lua_eval ), [True ])
507
+
427
508
@classmethod
428
509
def tearDownClass (self ):
429
510
self .con .close ()
0 commit comments