#!/usr/bin/python3.4 # Note: Many of those tests fail in MySQL_python version 1.2.3 and earlier # because byte strings are returned instead of unicode for columns having # a _bin collation, see https://sourceforge.net/p/mysql-python/bugs/289/ # This has been fixed in MySQL_python version 1.2.4. import MySQLdb import unittest def exec_sql(sql): db = MySQLdb.connect(db='philipp_winterrodeln_wiki', charset='utf8', use_unicode=True) # db = MySQLdb.connect(db='philipp_winterrodeln_wiki', charset='utf8', use_unicode=False) cursor = db.cursor() cursor.execute(sql) row = cursor.fetchone() return row class TestMySqlPython(unittest.TestCase): def test_datatype_page(self): result = exec_sql('select page_title, page_restrictions, page_touched from page where page_id = 1321') assert type(result[0]) == str # varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL assert type(result[1]) == bytes # tinyblob NOT NULL assert type(result[2]) == bytes # binary(14) NOT NULL def test_datatype_revision(self): result = exec_sql('select rev_comment, rev_user_text, rev_timestamp from revision where rev_id = 7586') assert type(result[0]) == bytes # tinyblob NOT NULL assert type(result[1]) == str # varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL assert type(result[2]) == bytes # binary(14) NOT NULL def test_datatypes_text(self): result = exec_sql('select old_text, old_flags from text where old_id = 7438') assert type(result[0]) == bytes # mediumblob NOT NULL assert type(result[1]) == bytes # tinyblob NOT NULL def test_datatype_user(self): result = exec_sql('select user_name, user_real_name, user_email from user where user_id = 1') assert type(result[0]) == str # varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL assert type(result[1]) == str # varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL assert type(result[2]) == str # tinytext NOT NULL assert result[0] == 'Philipp' def test_datatype_categorylinks(self): result = exec_sql('select cl_to, cl_sortkey from categorylinks where cl_from = 609') assert type(result[0]) == str # varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL assert type(result[1]) == bytes # varbinary(230) NOT NULL