13
13
from tarantool .error import MsgpackError
14
14
from tarantool .msgpack_ext .packer import default as packer_default
15
15
from tarantool .msgpack_ext .unpacker import ext_hook as unpacker_ext_hook
16
+ from tarantool .msgpack_ext .types .datetime import IS_DATETIME_SUPPORTED
16
17
17
18
from .lib .tarantool_server import TarantoolServer
18
19
from .lib .skip import skip_or_run_datetime_test
19
20
21
+ # Workaround for matrix test cases build.
22
+ if not IS_DATETIME_SUPPORTED :
23
+ ORIGINAL_DATETIME_IMPL = tarantool .Datetime
24
+ tarantool .Datetime = lambda * args , ** kwars : None
25
+
20
26
21
27
class TestSuiteDatetime (unittest .TestCase ):
22
28
@classmethod
@@ -69,6 +75,7 @@ def setUp(self):
69
75
70
76
self .adm ("box.space['test']:truncate()" )
71
77
78
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
72
79
def test_datetime_class_api (self ):
73
80
datetime = tarantool .Datetime (year = 2022 , month = 8 , day = 31 , hour = 18 , minute = 7 , sec = 54 ,
74
81
nsec = 308543321 , tzoffset = 180 )
@@ -86,6 +93,7 @@ def test_datetime_class_api(self):
86
93
self .assertEqual (datetime .tz , '' )
87
94
self .assertEqual (datetime .value , 1661958474308543321 )
88
95
96
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
89
97
def test_datetime_class_api_wth_tz (self ):
90
98
datetime = tarantool .Datetime (year = 2022 , month = 8 , day = 31 , hour = 18 , minute = 7 , sec = 54 ,
91
99
nsec = 308543321 , tzoffset = 123 , tz = 'Europe/Moscow' )
@@ -142,6 +150,7 @@ def test_datetime_class_api_wth_tz(self):
142
150
},
143
151
}
144
152
153
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
145
154
def test_datetime_class_invalid_init (self ):
146
155
# pylint: disable=cell-var-from-loop
147
156
@@ -282,12 +291,14 @@ def test_datetime_class_invalid_init(self):
282
291
},
283
292
}
284
293
294
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
285
295
def test_msgpack_decode (self ):
286
296
for name , case in self .integration_cases .items ():
287
297
with self .subTest (msg = name ):
288
298
self .assertEqual (unpacker_ext_hook (4 , case ['msgpack' ]),
289
299
case ['python' ])
290
300
301
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
291
302
@skip_or_run_datetime_test
292
303
def test_tarantool_decode (self ):
293
304
for name , case in self .integration_cases .items ():
@@ -297,12 +308,14 @@ def test_tarantool_decode(self):
297
308
self .assertSequenceEqual (self .con .select ('test' , name ),
298
309
[[name , case ['python' ], 'field' ]])
299
310
311
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
300
312
def test_msgpack_encode (self ):
301
313
for name , case in self .integration_cases .items ():
302
314
with self .subTest (msg = name ):
303
315
self .assertEqual (packer_default (case ['python' ]),
304
316
msgpack .ExtType (code = 4 , data = case ['msgpack' ]))
305
317
318
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
306
319
@skip_or_run_datetime_test
307
320
def test_tarantool_encode (self ):
308
321
for name , case in self .integration_cases .items ():
@@ -325,12 +338,14 @@ def test_tarantool_encode(self):
325
338
326
339
self .assertSequenceEqual (self .adm (lua_eval ), [True ])
327
340
341
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
328
342
def test_msgpack_decode_unknown_tzindex (self ):
329
343
case = b'\x4a \x79 \x0f \x63 \x00 \x00 \x00 \x00 \x59 \xff \x63 \x12 \xb4 \x00 \xff \xff '
330
344
self .assertRaisesRegex (
331
345
MsgpackError , 'Failed to decode datetime with unknown tzindex "-1"' ,
332
346
lambda : unpacker_ext_hook (4 , case ))
333
347
348
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
334
349
def test_msgpack_decode_ambiguous_tzindex (self ):
335
350
case = b'\x4a \x79 \x0f \x63 \x00 \x00 \x00 \x00 \x59 \xff \x63 \x12 \x00 \x00 \x82 \x00 '
336
351
self .assertRaisesRegex (
@@ -364,11 +379,13 @@ def test_msgpack_decode_ambiguous_tzindex(self):
364
379
},
365
380
}
366
381
382
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
367
383
def test_python_datetime_subtraction (self ):
368
384
for name , case in self .datetime_subtraction_cases .items ():
369
385
with self .subTest (msg = name ):
370
386
self .assertEqual (case ['arg_1' ] - case ['arg_2' ], case ['res' ])
371
387
388
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
372
389
@skip_or_run_datetime_test
373
390
def test_tarantool_datetime_subtraction (self ):
374
391
for name , case in self .datetime_subtraction_cases .items ():
@@ -382,11 +399,13 @@ def test_tarantool_datetime_subtraction(self):
382
399
'res' : tarantool .Interval (day = 1 , hour = - 21 ),
383
400
}
384
401
402
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
385
403
def test_python_datetime_subtraction_different_timezones (self ):
386
404
case = self .datetime_subtraction_different_timezones_case
387
405
388
406
self .assertEqual (case ['arg_1' ] - case ['arg_2' ], case ['res' ])
389
407
408
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
390
409
@skip_or_run_datetime_test
391
410
@unittest .skip ('See https://github.com/tarantool/tarantool/issues/7698' )
392
411
def test_tarantool_datetime_subtraction_different_timezones (self ):
@@ -478,23 +497,27 @@ def test_tarantool_datetime_subtraction_different_timezones(self):
478
497
},
479
498
}
480
499
500
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
481
501
def test_python_interval_addition (self ):
482
502
for name , case in self .interval_arithmetic_cases .items ():
483
503
with self .subTest (msg = name ):
484
504
self .assertEqual (case ['arg_1' ] + case ['arg_2' ], case ['res_add' ])
485
505
506
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
486
507
def test_python_interval_subtraction (self ):
487
508
for name , case in self .interval_arithmetic_cases .items ():
488
509
with self .subTest (msg = name ):
489
510
self .assertEqual (case ['arg_1' ] - case ['arg_2' ], case ['res_sub' ])
490
511
512
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
491
513
@skip_or_run_datetime_test
492
514
def test_tarantool_interval_addition (self ):
493
515
for name , case in self .interval_arithmetic_cases .items ():
494
516
with self .subTest (msg = name ):
495
517
self .assertSequenceEqual (self .con .call ('add' , case ['arg_1' ], case ['arg_2' ]),
496
518
[case ['res_add' ]])
497
519
520
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
498
521
@skip_or_run_datetime_test
499
522
def test_tarantool_interval_subtraction (self ):
500
523
for name , case in self .interval_arithmetic_cases .items ():
@@ -508,11 +531,13 @@ def test_tarantool_interval_subtraction(self):
508
531
'res' : tarantool .Datetime (year = 2008 , month = 7 , day = 1 , hour = 12 , tz = 'Europe/Moscow' ),
509
532
}
510
533
534
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
511
535
def test_python_datetime_addition_winter_time_switch (self ):
512
536
case = self .datetime_addition_winter_time_switch_case
513
537
514
538
self .assertEqual (case ['arg_1' ] + case ['arg_2' ], case ['res' ])
515
539
540
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
516
541
@skip_or_run_datetime_test
517
542
@unittest .skip ('See https://github.com/tarantool/tarantool/issues/7700' )
518
543
def test_tarantool_datetime_addition_winter_time_switch (self ):
@@ -521,13 +546,71 @@ def test_tarantool_datetime_addition_winter_time_switch(self):
521
546
self .assertSequenceEqual (self .con .call ('add' , case ['arg_1' ], case ['arg_2' ]),
522
547
[case ['res' ]])
523
548
549
+ @unittest .skipIf (not IS_DATETIME_SUPPORTED , "pandas or pytz not installed" )
524
550
@skip_or_run_datetime_test
525
551
def test_primary_key (self ):
526
552
data = [tarantool .Datetime (year = 1970 , month = 1 , day = 1 ), 'content' ]
527
553
528
554
self .assertSequenceEqual (self .con .insert ('test_pk' , data ), [data ])
529
555
self .assertSequenceEqual (self .con .select ('test_pk' , data [0 ]), [data ])
530
556
557
+ integration_raw_case = {
558
+ 'python' : tarantool .DatetimeRaw (b'\x44 \xa3 \x0f \x63 \x00 \x00 \x00 \x00 ' ),
559
+ 'msgpack' : (b'\x44 \xa3 \x0f \x63 \x00 \x00 \x00 \x00 ' ),
560
+ 'tarantool' : r"datetime.new({year=2022, month=8, day=31, hour=18, min=7})" ,
561
+ }
562
+
563
+ @unittest .skipIf (IS_DATETIME_SUPPORTED , "pandas or pytz are installed" )
564
+ def test_msgpack_decode_raw (self ):
565
+ case = self .integration_raw_case
566
+ self .assertEqual (unpacker_ext_hook (4 , case ['msgpack' ]),
567
+ case ['python' ])
568
+
569
+ @unittest .skipIf (IS_DATETIME_SUPPORTED , "pandas or pytz are installed" )
570
+ @skip_or_run_datetime_test
571
+ def test_tarantool_decode_raw (self ):
572
+ name = 'decode_raw'
573
+ case = self .integration_raw_case
574
+ self .adm (f"box.space['test']:replace{{'{ name } ', { case ['tarantool' ]} , 'field'}}" )
575
+
576
+ self .assertSequenceEqual (self .con .select ('test' , name ),
577
+ [[name , case ['python' ], 'field' ]])
578
+
579
+ @unittest .skipIf (IS_DATETIME_SUPPORTED , "pandas or pytz are installed" )
580
+ def test_msgpack_encode_raw (self ):
581
+ case = self .integration_raw_case
582
+ self .assertEqual (packer_default (case ['python' ]),
583
+ msgpack .ExtType (code = 4 , data = case ['msgpack' ]))
584
+
585
+ @unittest .skipIf (IS_DATETIME_SUPPORTED , "pandas or pytz are installed" )
586
+ @skip_or_run_datetime_test
587
+ def test_tarantool_encode_raw (self ):
588
+ name = 'encode_raw'
589
+ case = self .integration_raw_case
590
+ self .con .insert ('test' , [name , case ['python' ], 'field' ])
591
+
592
+ lua_eval = f"""
593
+ local dt = { case ['tarantool' ]}
594
+
595
+ local tuple = box.space['test']:get('{ name } ')
596
+ assert(tuple ~= nil)
597
+
598
+ if tuple[2] == dt then
599
+ return true
600
+ else
601
+ return nil, ('%s is not equal to expected %s'):format(
602
+ tostring(tuple[2]), tostring(dt))
603
+ end
604
+ """
605
+
606
+ self .assertSequenceEqual (self .adm (lua_eval ), [True ])
607
+
608
+ @unittest .skipIf (IS_DATETIME_SUPPORTED , "pandas or pytz are installed" )
609
+ def test_datetime_if_not_supported (self ):
610
+ self .assertRaisesRegex (
611
+ AssertionError , "pandas and pytz are required" ,
612
+ lambda : ORIGINAL_DATETIME_IMPL (year = 1970 , month = 1 , day = 1 ))
613
+
531
614
@classmethod
532
615
def tearDownClass (cls ):
533
616
cls .con .close ()
0 commit comments