| 326 | |
| 327 | class MySQLEncodingTest(TestCase): |
| 328 | |
| 329 | @unittest.skipUnless(connection.vendor == 'mysql', |
| 330 | "MySQL specific utf8_bin test.") |
| 331 | def test_utfbin_unicode(self): |
| 332 | """ |
| 333 | Ensure CharField is unicode with both utf_bin and default encoding. |
| 334 | """ |
| 335 | |
| 336 | models.Person.objects.create(first_name='Django', last_name='Reinhardt') |
| 337 | |
| 338 | cur = connections['default'].cursor() |
| 339 | cur.execute('ALTER TABLE backends_person ' + |
| 340 | 'MODIFY first_name VARCHAR(20) ' + |
| 341 | 'CHARACTER SET utf8 COLLATE utf8_bin;') |
| 342 | cur.close() |
| 343 | |
| 344 | person = models.Person.objects.get(last_name='Reinhardt') |
| 345 | |
| 346 | self.assertIsInstance(person.first_name, unicode) # utf_bin |
| 347 | self.assertIsInstance(person.last_name, unicode) # default |
| 348 | |
| 349 | @unittest.skipUnless(connection.vendor == 'mysql', |
| 350 | "MySQL specific utf8_bin test.") |
| 351 | def test_utfbin_unicode_needed(self): |
| 352 | """ |
| 353 | Ensure CharField is unicode with both utf_bin and default encoding, |
| 354 | with an actual non-ascii string. |
| 355 | """ |
| 356 | |
| 357 | models.Person.objects.create(first_name='汉语/漢語', last_name='Reinhardt') |
| 358 | |
| 359 | cur = connections['default'].cursor() |
| 360 | cur.execute('ALTER TABLE backends_person ' + |
| 361 | 'MODIFY first_name VARCHAR(20) ' + |
| 362 | 'CHARACTER SET utf8 COLLATE utf8_bin;') |
| 363 | cur.close() |
| 364 | |
| 365 | person = models.Person.objects.get(last_name='Reinhardt') |
| 366 | |
| 367 | self.assertIsInstance(person.first_name, unicode) # utf_bin |
| 368 | self.assertIsInstance(person.last_name, unicode) # default |
| 369 | |
| 370 | @unittest.skipUnless(connection.vendor == 'mysql', |
| 371 | "MySQL specific conversion test.") |
| 372 | def test_convert_var_string_unicode(self): |
| 373 | """Test the convert_var_string method with unicode data""" |
| 374 | from django.db.backends.mysql.base import convert_var_string |
| 375 | |
| 376 | result = convert_var_string('汉语/漢語') |
| 377 | self.assertIsInstance(result, unicode) |
| 378 | |
| 379 | @unittest.skipUnless(connection.vendor == 'mysql', |
| 380 | "MySQL specific conversion test.") |
| 381 | def test_convert_var_string_binary(self): |
| 382 | """Test the convert_var_string method with binary data""" |
| 383 | from django.db.backends.mysql.base import convert_var_string |
| 384 | |
| 385 | # Use the python executable as source of binary data |
| 386 | data = open(sys.executable, 'rb').read() |
| 387 | |
| 388 | result = convert_var_string(data) |
| 389 | self.assertIsInstance(result, str) |
| 390 | |