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_types .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_MsgpackExtTypes (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', {
@@ -418,6 +420,85 @@ def test_decimal_tarantool_encode_with_precision_loss(self):
418
420
419
421
self .assertSequenceEqual (self .con .eval (lua_eval ), [True ])
420
422
423
+
424
+ UUID_cases = [
425
+ {
426
+ 'python' : uuid .UUID ('ae28d4f6-076c-49dd-8227-7f9fae9592d0' ),
427
+ 'msgpack' : (b'\xae \x28 \xd4 \xf6 \x07 \x6c \x49 \xdd \x82 \x27 \x7f \x9f \xae \x95 \x92 \xd0 ' ),
428
+ 'tarantool' : "uuid.fromstr('ae28d4f6-076c-49dd-8227-7f9fae9592d0')" ,
429
+ },
430
+ {
431
+ 'python' : uuid .UUID ('b3121301-9300-4038-a652-ead943fb9c39' ),
432
+ 'msgpack' : (b'\xb3 \x12 \x13 \x01 \x93 \x00 \x40 \x38 \xa6 \x52 \xea \xd9 \x43 \xfb \x9c \x39 ' ),
433
+ 'tarantool' : "uuid.fromstr('b3121301-9300-4038-a652-ead943fb9c39')" ,
434
+ },
435
+ {
436
+ 'python' : uuid .UUID ('dfa69f02-92e6-44a5-abb5-84b39292ff93' ),
437
+ 'msgpack' : (b'\xdf \xa6 \x9f \x02 \x92 \xe6 \x44 \xa5 \xab \xb5 \x84 \xb3 \x92 \x92 \xff \x93 ' ),
438
+ 'tarantool' : "uuid.fromstr('dfa69f02-92e6-44a5-abb5-84b39292ff93')" ,
439
+ },
440
+ {
441
+ 'python' : uuid .UUID ('8b69a1ce-094a-4e21-a5dc-4cdae7cd8960' ),
442
+ 'msgpack' : (b'\x8b \x69 \xa1 \xce \x09 \x4a \x4e \x21 \xa5 \xdc \x4c \xda \xe7 \xcd \x89 \x60 ' ),
443
+ 'tarantool' : "uuid.fromstr('8b69a1ce-094a-4e21-a5dc-4cdae7cd8960')" ,
444
+ },
445
+ {
446
+ 'python' : uuid .UUID ('25932334-1d42-4686-9299-ec1a7165227c' ),
447
+ 'msgpack' : (b'\x25 \x93 \x23 \x34 \x1d \x42 \x46 \x86 \x92 \x99 \xec \x1a \x71 \x65 \x22 \x7c ' ),
448
+ 'tarantool' : "uuid.fromstr('25932334-1d42-4686-9299-ec1a7165227c')" ,
449
+ },
450
+ ]
451
+
452
+ def test_UUID_msgpack_decode (self ):
453
+ for i in range (len (self .UUID_cases )):
454
+ with self .subTest (msg = str (i )):
455
+ UUID_case = self .UUID_cases [i ]
456
+
457
+ self .assertEqual (unpacker_ext_hook (2 , UUID_case ['msgpack' ]),
458
+ UUID_case ['python' ])
459
+
460
+ @skip_or_run_UUID_test
461
+ def test_UUID_tarantool_decode (self ):
462
+ for i in range (len (self .UUID_cases )):
463
+ with self .subTest (msg = str (i )):
464
+ UUID_case = self .UUID_cases [i ]
465
+
466
+ self .adm (f"box.space['test']:replace{{{ i } , { UUID_case ['tarantool' ]} }}" )
467
+
468
+ self .assertSequenceEqual (self .con .select ('test' , i ),
469
+ [[i , UUID_case ['python' ]]])
470
+
471
+ def test_UUID_msgpack_encode (self ):
472
+ for i in range (len (self .UUID_cases )):
473
+ with self .subTest (msg = str (i )):
474
+ UUID_case = self .UUID_cases [i ]
475
+
476
+ self .assertEqual (packer_default (UUID_case ['python' ]),
477
+ msgpack .ExtType (code = 2 , data = UUID_case ['msgpack' ]))
478
+
479
+ @skip_or_run_UUID_test
480
+ def test_UUID_tarantool_encode (self ):
481
+ for i in range (len (self .UUID_cases )):
482
+ with self .subTest (msg = str (i )):
483
+ UUID_case = self .UUID_cases [i ]
484
+
485
+ self .con .insert ('test' , [i , UUID_case ['python' ]])
486
+
487
+ lua_eval = f"""
488
+ local tuple = box.space['test']:get({ i } )
489
+ assert(tuple ~= nil)
490
+
491
+ local id = { UUID_case ['tarantool' ]}
492
+ if tuple[2] == id then
493
+ return true
494
+ else
495
+ return nil, ('%s is not equal to expected %s'):format(
496
+ tostring(tuple[2]), tostring(id))
497
+ end
498
+ """
499
+
500
+ self .assertSequenceEqual (self .con .eval (lua_eval ), [True ])
501
+
421
502
@classmethod
422
503
def tearDownClass (self ):
423
504
self .con .close ()
0 commit comments