/SQLAlchemy-0.7.8/test/dialect/test_mysql.py
Python | 1668 lines | 1628 code | 25 blank | 15 comment | 12 complexity | 3976419ae171c99a680c2eef9e5df809 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- # coding: utf-8
- from test.lib.testing import eq_, assert_raises, assert_raises_message
- # Py2K
- import sets
- # end Py2K
- from sqlalchemy import *
- from sqlalchemy import sql, exc, schema, types as sqltypes, event
- from sqlalchemy.dialects.mysql import base as mysql
- from sqlalchemy.engine.url import make_url
- from test.lib.testing import eq_
- from test.lib import *
- from test.lib.engines import utf8_engine
- import datetime
- class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
- __dialect__ = mysql.dialect()
- def test_reserved_words(self):
- table = Table("mysql_table", MetaData(),
- Column("col1", Integer),
- Column("master_ssl_verify_server_cert", Integer))
- x = select([table.c.col1, table.c.master_ssl_verify_server_cert])
- self.assert_compile(x,
- '''SELECT mysql_table.col1, mysql_table.`master_ssl_verify_server_cert` FROM mysql_table''')
- def test_create_index_simple(self):
- m = MetaData()
- tbl = Table('testtbl', m, Column('data', String(255)))
- idx = Index('test_idx1', tbl.c.data)
- self.assert_compile(schema.CreateIndex(idx),
- 'CREATE INDEX test_idx1 ON testtbl (data)',
- dialect=mysql.dialect())
- def test_create_index_with_length(self):
- m = MetaData()
- tbl = Table('testtbl', m, Column('data', String(255)))
- idx1 = Index('test_idx1', tbl.c.data, mysql_length=10)
- idx2 = Index('test_idx2', tbl.c.data, mysql_length=5)
- self.assert_compile(schema.CreateIndex(idx1),
- 'CREATE INDEX test_idx1 ON testtbl (data(10))',
- dialect=mysql.dialect())
- self.assert_compile(schema.CreateIndex(idx2),
- 'CREATE INDEX test_idx2 ON testtbl (data(5))',
- dialect=mysql.dialect())
- def test_create_index_with_using(self):
- m = MetaData()
- tbl = Table('testtbl', m, Column('data', String(255)))
- idx1 = Index('test_idx1', tbl.c.data, mysql_using='btree')
- idx2 = Index('test_idx2', tbl.c.data, mysql_using='hash')
- self.assert_compile(schema.CreateIndex(idx1),
- 'CREATE INDEX test_idx1 ON testtbl (data) USING btree',
- dialect=mysql.dialect())
- self.assert_compile(schema.CreateIndex(idx2),
- 'CREATE INDEX test_idx2 ON testtbl (data) USING hash',
- dialect=mysql.dialect())
- def test_create_pk_plain(self):
- m = MetaData()
- tbl = Table('testtbl', m, Column('data', String(255)),
- PrimaryKeyConstraint('data'))
- self.assert_compile(schema.CreateTable(tbl),
- "CREATE TABLE testtbl (data VARCHAR(255), PRIMARY KEY (data))",
- dialect=mysql.dialect())
- def test_create_pk_with_using(self):
- m = MetaData()
- tbl = Table('testtbl', m, Column('data', String(255)),
- PrimaryKeyConstraint('data', mysql_using='btree'))
- self.assert_compile(schema.CreateTable(tbl),
- "CREATE TABLE testtbl (data VARCHAR(255), "
- "PRIMARY KEY (data) USING btree)",
- dialect=mysql.dialect())
- class DialectTest(fixtures.TestBase):
- __only_on__ = 'mysql'
- @testing.only_on(['mysql+mysqldb', 'mysql+oursql'],
- 'requires particular SSL arguments')
- def test_ssl_arguments(self):
- dialect = testing.db.dialect
- kwarg = dialect.create_connect_args(
- make_url("mysql://scott:tiger@localhost:3306/test"
- "?ssl_ca=/ca.pem&ssl_cert=/cert.pem&ssl_key=/key.pem")
- )[1]
- # args that differ among mysqldb and oursql
- for k in ('use_unicode', 'found_rows', 'client_flag'):
- kwarg.pop(k, None)
- eq_(
- kwarg,
- {
- 'passwd': 'tiger', 'db': 'test',
- 'ssl': {'ca': '/ca.pem', 'cert': '/cert.pem',
- 'key': '/key.pem'},
- 'host': 'localhost', 'user': 'scott',
- 'port': 3306
- }
- )
- class TypesTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
- "Test MySQL column types"
- __only_on__ = 'mysql'
- __dialect__ = mysql.dialect()
- @testing.uses_deprecated('Manually quoting ENUM value literals')
- def test_basic(self):
- meta1 = MetaData(testing.db)
- table = Table(
- 'mysql_types', meta1,
- Column('id', Integer, primary_key=True),
- Column('num1', mysql.MSInteger(unsigned=True)),
- Column('text1', mysql.MSLongText),
- Column('text2', mysql.MSLongText()),
- Column('num2', mysql.MSBigInteger),
- Column('num3', mysql.MSBigInteger()),
- Column('num4', mysql.MSDouble),
- Column('num5', mysql.MSDouble()),
- Column('num6', mysql.MSMediumInteger),
- Column('enum1', mysql.ENUM("'black'", "'white'")),
- Column('enum2', mysql.ENUM("dog", "cat")),
- )
- try:
- table.drop(checkfirst=True)
- table.create()
- meta2 = MetaData(testing.db)
- t2 = Table('mysql_types', meta2, autoload=True)
- assert isinstance(t2.c.num1.type, mysql.MSInteger)
- assert t2.c.num1.type.unsigned
- assert isinstance(t2.c.text1.type, mysql.MSLongText)
- assert isinstance(t2.c.text2.type, mysql.MSLongText)
- assert isinstance(t2.c.num2.type, mysql.MSBigInteger)
- assert isinstance(t2.c.num3.type, mysql.MSBigInteger)
- assert isinstance(t2.c.num4.type, mysql.MSDouble)
- assert isinstance(t2.c.num5.type, mysql.MSDouble)
- assert isinstance(t2.c.num6.type, mysql.MSMediumInteger)
- assert isinstance(t2.c.enum1.type, mysql.ENUM)
- assert isinstance(t2.c.enum2.type, mysql.ENUM)
- t2.drop()
- t2.create()
- finally:
- meta1.drop_all()
- def test_numeric(self):
- "Exercise type specification and options for numeric types."
- columns = [
- # column type, args, kwargs, expected ddl
- # e.g. Column(Integer(10, unsigned=True)) ==
- # 'INTEGER(10) UNSIGNED'
- (mysql.MSNumeric, [], {},
- 'NUMERIC'),
- (mysql.MSNumeric, [None], {},
- 'NUMERIC'),
- (mysql.MSNumeric, [12], {},
- 'NUMERIC(12)'),
- (mysql.MSNumeric, [12, 4], {'unsigned':True},
- 'NUMERIC(12, 4) UNSIGNED'),
- (mysql.MSNumeric, [12, 4], {'zerofill':True},
- 'NUMERIC(12, 4) ZEROFILL'),
- (mysql.MSNumeric, [12, 4], {'zerofill':True, 'unsigned':True},
- 'NUMERIC(12, 4) UNSIGNED ZEROFILL'),
- (mysql.MSDecimal, [], {},
- 'DECIMAL'),
- (mysql.MSDecimal, [None], {},
- 'DECIMAL'),
- (mysql.MSDecimal, [12], {},
- 'DECIMAL(12)'),
- (mysql.MSDecimal, [12, None], {},
- 'DECIMAL(12)'),
- (mysql.MSDecimal, [12, 4], {'unsigned':True},
- 'DECIMAL(12, 4) UNSIGNED'),
- (mysql.MSDecimal, [12, 4], {'zerofill':True},
- 'DECIMAL(12, 4) ZEROFILL'),
- (mysql.MSDecimal, [12, 4], {'zerofill':True, 'unsigned':True},
- 'DECIMAL(12, 4) UNSIGNED ZEROFILL'),
- (mysql.MSDouble, [None, None], {},
- 'DOUBLE'),
- (mysql.MSDouble, [12, 4], {'unsigned':True},
- 'DOUBLE(12, 4) UNSIGNED'),
- (mysql.MSDouble, [12, 4], {'zerofill':True},
- 'DOUBLE(12, 4) ZEROFILL'),
- (mysql.MSDouble, [12, 4], {'zerofill':True, 'unsigned':True},
- 'DOUBLE(12, 4) UNSIGNED ZEROFILL'),
- (mysql.MSReal, [None, None], {},
- 'REAL'),
- (mysql.MSReal, [12, 4], {'unsigned':True},
- 'REAL(12, 4) UNSIGNED'),
- (mysql.MSReal, [12, 4], {'zerofill':True},
- 'REAL(12, 4) ZEROFILL'),
- (mysql.MSReal, [12, 4], {'zerofill':True, 'unsigned':True},
- 'REAL(12, 4) UNSIGNED ZEROFILL'),
- (mysql.MSFloat, [], {},
- 'FLOAT'),
- (mysql.MSFloat, [None], {},
- 'FLOAT'),
- (mysql.MSFloat, [12], {},
- 'FLOAT(12)'),
- (mysql.MSFloat, [12, 4], {},
- 'FLOAT(12, 4)'),
- (mysql.MSFloat, [12, 4], {'unsigned':True},
- 'FLOAT(12, 4) UNSIGNED'),
- (mysql.MSFloat, [12, 4], {'zerofill':True},
- 'FLOAT(12, 4) ZEROFILL'),
- (mysql.MSFloat, [12, 4], {'zerofill':True, 'unsigned':True},
- 'FLOAT(12, 4) UNSIGNED ZEROFILL'),
- (mysql.MSInteger, [], {},
- 'INTEGER'),
- (mysql.MSInteger, [4], {},
- 'INTEGER(4)'),
- (mysql.MSInteger, [4], {'unsigned':True},
- 'INTEGER(4) UNSIGNED'),
- (mysql.MSInteger, [4], {'zerofill':True},
- 'INTEGER(4) ZEROFILL'),
- (mysql.MSInteger, [4], {'zerofill':True, 'unsigned':True},
- 'INTEGER(4) UNSIGNED ZEROFILL'),
- (mysql.MSBigInteger, [], {},
- 'BIGINT'),
- (mysql.MSBigInteger, [4], {},
- 'BIGINT(4)'),
- (mysql.MSBigInteger, [4], {'unsigned':True},
- 'BIGINT(4) UNSIGNED'),
- (mysql.MSBigInteger, [4], {'zerofill':True},
- 'BIGINT(4) ZEROFILL'),
- (mysql.MSBigInteger, [4], {'zerofill':True, 'unsigned':True},
- 'BIGINT(4) UNSIGNED ZEROFILL'),
- (mysql.MSMediumInteger, [], {},
- 'MEDIUMINT'),
-