40
40
import mysql .connector
41
41
from mysql .connector import fabric , errorcode
42
42
from mysql .connector .fabric import connection , balancing
43
+ from mysql .connector .catch23 import UNICODE_TYPES , PY2
43
44
44
45
ERR_NO_FABRIC_CONFIG = "Fabric configuration not available"
45
46
@@ -148,7 +149,8 @@ def test_cnx_properties(self):
148
149
cnxprops = {
149
150
# name: (valid_types, description, default)
150
151
'group' : ((str ,), "Name of group of servers" , None ),
151
- 'key' : ((int , str , datetime .datetime , datetime .date ),
152
+ 'key' : (tuple ([int , str , datetime .datetime ,
153
+ datetime .date ] + list (UNICODE_TYPES )),
152
154
"Sharding key" , None ),
153
155
'tables' : ((tuple , list ), "List of tables in query" , None ),
154
156
'mode' : ((int ,), "Read-Only, Write-Only or Read-Write" ,
@@ -365,10 +367,25 @@ def test___eq__(self):
365
367
self .assertFalse (balancer1 == balancer3 )
366
368
367
369
@unittest .skipIf (not tests .FABRIC_CONFIG , ERR_NO_FABRIC_CONFIG )
368
- class FabricSharding (tests .MySQLConnectorTests ):
370
+ class FabricShardingTests (tests .MySQLConnectorTests ):
369
371
370
372
"""Test Fabric's sharding"""
371
373
374
+ emp_data = {
375
+ 1985 : [
376
+ (10001 , datetime .date (1953 , 9 , 2 ), u'Georgi' , u'Facello' , u'M' ,
377
+ datetime .date (1986 , 6 , 26 )),
378
+ (10002 , datetime .date (1964 , 6 , 2 ), u'Bezalel' , u'Simmel' , u'F' ,
379
+ datetime .date (1985 , 11 , 21 )),
380
+ ],
381
+ 2000 : [
382
+ (47291 , datetime .date (1960 , 9 , 9 ), u'Ulf' , u'Flexer' , u'M' ,
383
+ datetime .date (2000 , 1 , 12 )),
384
+ (60134 , datetime .date (1964 , 4 , 21 ), u'Seshu' , u'Rathonyi' , u'F' ,
385
+ datetime .date (2000 , 1 , 2 )),
386
+ ]
387
+ }
388
+
372
389
def setUp (self ):
373
390
self .cnx = mysql .connector .connect (
374
391
fabric = tests .FABRIC_CONFIG , user = 'root' , database = 'employees'
@@ -421,27 +438,11 @@ def test_range_datetime(self):
421
438
gtid_executed = self ._truncate (cur , tbl_name )
422
439
self .cnx .commit ()
423
440
424
- employee_data = {
425
- 1985 : [
426
- (10001 , datetime .date (1953 , 9 , 2 ), u'Georgi' , u'Facello' , u'M' ,
427
- datetime .date (1986 , 6 , 26 )),
428
- (10002 , datetime .date (1964 , 6 , 2 ), u'Bezalel' , u'Simmel' , u'F' ,
429
- datetime .date (1985 , 11 , 21 )),
430
- ],
431
- 2000 : [
432
- (47291 , datetime .date (1960 , 9 , 9 ), u'Ulf' , u'Flexer' , u'M' ,
433
- datetime .date (2000 , 1 , 12 )),
434
- (60134 , datetime .date (1964 , 4 , 21 ), u'Seshu' , u'Rathonyi' , u'F' ,
435
- datetime .date (2000 , 1 , 2 )),
436
- ]
437
- }
438
-
439
441
insert = ("INSERT INTO {0} "
440
442
"VALUES (%s, %s, %s, %s, %s, %s)" ).format (tbl_name )
441
443
442
444
self ._populate (self .cnx , gtid_executed , tbl_name , insert ,
443
- employee_data [1985 ] + employee_data [2000 ],
444
- 5 )
445
+ self .emp_data [1985 ] + self .emp_data [2000 ], 5 )
445
446
446
447
time .sleep (2 )
447
448
@@ -452,8 +453,60 @@ def test_range_datetime(self):
452
453
cur = self .cnx .cursor ()
453
454
cur .execute ("SELECT * FROM {0}" .format (tbl_name ))
454
455
rows = cur .fetchall ()
455
- self .assertEqual (rows , employee_data [hire_date .year ])
456
+ self .assertEqual (rows , self . emp_data [hire_date .year ])
456
457
457
458
self .cnx .set_property (tables = tables ,
458
459
key = '2014-01-02' , mode = fabric .MODE_READONLY )
459
460
self .assertRaises (ValueError , self .cnx .cursor )
461
+
462
+ def test_range_string (self ):
463
+ self .assertTrue (self ._check_table (
464
+ "employees.employees_range_string" , 'RANGE_STRING' ))
465
+ tbl_name = "employees_range_string"
466
+
467
+ tables = ["employees.{0}" .format (tbl_name )]
468
+
469
+ self .cnx .set_property (tables = tables ,
470
+ scope = fabric .SCOPE_GLOBAL ,
471
+ mode = fabric .MODE_READWRITE )
472
+ cur = self .cnx .cursor ()
473
+ gtid_executed = self ._truncate (cur , tbl_name )
474
+ self .cnx .commit ()
475
+
476
+ insert = ("INSERT INTO {0} "
477
+ "VALUES (%s, %s, %s, %s, %s, %s)" ).format (tbl_name )
478
+
479
+ self ._populate (self .cnx , gtid_executed , tbl_name , insert ,
480
+ self .emp_data [1985 ] + self .emp_data [2000 ], 3 )
481
+
482
+ time .sleep (2 )
483
+
484
+ emp_exp_range_string = {
485
+ 'A' : [self .emp_data [1985 ][0 ],
486
+ self .emp_data [2000 ][0 ]],
487
+ 'M' : [self .emp_data [1985 ][1 ],
488
+ self .emp_data [2000 ][1 ]],
489
+ }
490
+
491
+ str_keys = [u'A' , u'M' ]
492
+ for str_key in str_keys :
493
+ self .cnx .set_property (tables = tables ,
494
+ key = str_key , mode = fabric .MODE_READONLY )
495
+ cur = self .cnx .cursor ()
496
+ cur .execute ("SELECT * FROM {0}" .format (tbl_name ))
497
+ rows = cur .fetchall ()
498
+ self .assertEqual (rows , emp_exp_range_string [str_key ])
499
+
500
+ self .cnx .set_property (tables = tables ,
501
+ key = b'not unicode str' , mode = fabric .MODE_READONLY )
502
+ self .assertRaises (ValueError , self .cnx .cursor )
503
+
504
+ self .cnx .set_property (tables = tables ,
505
+ key = 12345 , mode = fabric .MODE_READONLY )
506
+ self .assertRaises (ValueError , self .cnx .cursor )
507
+
508
+ if PY2 :
509
+ self .cnx .set_property (tables = tables ,
510
+ key = 'not unicode str' ,
511
+ mode = fabric .MODE_READONLY )
512
+ self .assertRaises (ValueError , self .cnx .cursor )
0 commit comments