⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysql.py

📁 SQLAlchemy. 经典的Python ORM框架。学习必看。
💻 PY
📖 第 1 页 / 共 3 页
字号:
import testenv; testenv.configure_for_tests()import setsfrom sqlalchemy import *from sqlalchemy import sql, exceptionsfrom sqlalchemy.databases import mysqlfrom testlib import *class TypesTest(TestBase, AssertsExecutionResults):    "Test MySQL column types"    __only_on__ = 'mysql'    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('enum1', mysql.MSEnum("'black'", "'white'")),            )        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.enum1.type, mysql.MSEnum)            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(10, 2)'),            (mysql.MSNumeric, [None], {},             'NUMERIC'),            (mysql.MSNumeric, [12], {},             'NUMERIC(12, 2)'),            (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(10, 2)'),            (mysql.MSDecimal, [None], {},             'DECIMAL'),            (mysql.MSDecimal, [12], {},             'DECIMAL(12, 2)'),            (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.MSTinyInteger, [], {},             'TINYINT'),            (mysql.MSTinyInteger, [1], {},             'TINYINT(1)'),            (mysql.MSTinyInteger, [1], {'unsigned':True},             'TINYINT(1) UNSIGNED'),            (mysql.MSTinyInteger, [1], {'zerofill':True},             'TINYINT(1) ZEROFILL'),            (mysql.MSTinyInteger, [1], {'zerofill':True, 'unsigned':True},             'TINYINT(1) UNSIGNED ZEROFILL'),            (mysql.MSSmallInteger, [], {},             'SMALLINT'),            (mysql.MSSmallInteger, [4], {},             'SMALLINT(4)'),            (mysql.MSSmallInteger, [4], {'unsigned':True},             'SMALLINT(4) UNSIGNED'),            (mysql.MSSmallInteger, [4], {'zerofill':True},             'SMALLINT(4) ZEROFILL'),            (mysql.MSSmallInteger, [4], {'zerofill':True, 'unsigned':True},             'SMALLINT(4) UNSIGNED ZEROFILL'),           ]        table_args = ['test_mysql_numeric', MetaData(testing.db)]        for index, spec in enumerate(columns):            type_, args, kw, res = spec            table_args.append(Column('c%s' % index, type_(*args, **kw)))        numeric_table = Table(*table_args)        gen = testing.db.dialect.schemagenerator(testing.db.dialect, testing.db, None, None)        for col in numeric_table.c:            index = int(col.name[1:])            self.assert_eq(gen.get_column_specification(col),                           "%s %s" % (col.name, columns[index][3]))            self.assert_(repr(col))        try:            numeric_table.create(checkfirst=True)            assert True        except:            raise        numeric_table.drop()    @testing.exclude('mysql', '<', (4, 1, 1))    def test_charset(self):        """Exercise CHARACTER SET and COLLATE-ish options on string types."""        columns = [            (mysql.MSChar, [1], {},             'CHAR(1)'),            (mysql.MSChar, [1], {'binary':True},             'CHAR(1) BINARY'),            (mysql.MSChar, [1], {'ascii':True},             'CHAR(1) ASCII'),            (mysql.MSChar, [1], {'unicode':True},             'CHAR(1) UNICODE'),            (mysql.MSChar, [1], {'ascii':True, 'binary':True},             'CHAR(1) ASCII BINARY'),            (mysql.MSChar, [1], {'unicode':True, 'binary':True},             'CHAR(1) UNICODE BINARY'),            (mysql.MSChar, [1], {'charset':'utf8'},             'CHAR(1) CHARACTER SET utf8'),            (mysql.MSChar, [1], {'charset':'utf8', 'binary':True},             'CHAR(1) CHARACTER SET utf8 BINARY'),            (mysql.MSChar, [1], {'charset':'utf8', 'unicode':True},             'CHAR(1) CHARACTER SET utf8'),            (mysql.MSChar, [1], {'charset':'utf8', 'ascii':True},             'CHAR(1) CHARACTER SET utf8'),            (mysql.MSChar, [1], {'collation': 'utf8_bin'},             'CHAR(1) COLLATE utf8_bin'),            (mysql.MSChar, [1], {'charset': 'utf8', 'collation': 'utf8_bin'},             'CHAR(1) CHARACTER SET utf8 COLLATE utf8_bin'),            (mysql.MSChar, [1], {'charset': 'utf8', 'binary': True},             'CHAR(1) CHARACTER SET utf8 BINARY'),            (mysql.MSChar, [1], {'charset': 'utf8', 'collation': 'utf8_bin',                              'binary': True},             'CHAR(1) CHARACTER SET utf8 COLLATE utf8_bin'),            (mysql.MSChar, [1], {'national':True},             'NATIONAL CHAR(1)'),            (mysql.MSChar, [1], {'national':True, 'charset':'utf8'},             'NATIONAL CHAR(1)'),            (mysql.MSChar, [1], {'national':True, 'charset':'utf8', 'binary':True},             'NATIONAL CHAR(1) BINARY'),            (mysql.MSChar, [1], {'national':True, 'binary':True, 'unicode':True},             'NATIONAL CHAR(1) BINARY'),            (mysql.MSChar, [1], {'national':True, 'collation':'utf8_bin'},             'NATIONAL CHAR(1) COLLATE utf8_bin'),            (mysql.MSString, [1], {'charset':'utf8', 'collation':'utf8_bin'},             'VARCHAR(1) CHARACTER SET utf8 COLLATE utf8_bin'),            (mysql.MSString, [1], {'national':True, 'collation':'utf8_bin'},             'NATIONAL VARCHAR(1) COLLATE utf8_bin'),            (mysql.MSTinyText, [], {'charset':'utf8', 'collation':'utf8_bin'},             'TINYTEXT CHARACTER SET utf8 COLLATE utf8_bin'),            (mysql.MSMediumText, [], {'charset':'utf8', 'binary':True},             'MEDIUMTEXT CHARACTER SET utf8 BINARY'),            (mysql.MSLongText, [], {'ascii':True},             'LONGTEXT ASCII'),            (mysql.MSEnum, ["'foo'", "'bar'"], {'unicode':True},             '''ENUM('foo','bar') UNICODE''')           ]        table_args = ['test_mysql_charset', MetaData(testing.db)]        for index, spec in enumerate(columns):            type_, args, kw, res = spec            table_args.append(Column('c%s' % index, type_(*args, **kw)))        charset_table = Table(*table_args)        gen = testing.db.dialect.schemagenerator(testing.db.dialect, testing.db, None, None)        for col in charset_table.c:            index = int(col.name[1:])            self.assert_eq(gen.get_column_specification(col),                           "%s %s" % (col.name, columns[index][3]))            self.assert_(repr(col))        try:            charset_table.create(checkfirst=True)            assert True        except:            raise        charset_table.drop()    @testing.exclude('mysql', '<', (5, 0, 5))    def test_bit_50(self):        """Exercise BIT types on 5.0+ (not valid for all engine types)"""        meta = MetaData(testing.db)        bit_table = Table('mysql_bits', meta,                          Column('b1', mysql.MSBit),                          Column('b2', mysql.MSBit()),                          Column('b3', mysql.MSBit(), nullable=False),                          Column('b4', mysql.MSBit(1)),                          Column('b5', mysql.MSBit(8)),                          Column('b6', mysql.MSBit(32)),                          Column('b7', mysql.MSBit(63)),                          Column('b8', mysql.MSBit(64)))        self.assert_eq(colspec(bit_table.c.b1), 'b1 BIT')        self.assert_eq(colspec(bit_table.c.b2), 'b2 BIT')        self.assert_eq(colspec(bit_table.c.b3), 'b3 BIT NOT NULL')        self.assert_eq(colspec(bit_table.c.b4), 'b4 BIT(1)')        self.assert_eq(colspec(bit_table.c.b5), 'b5 BIT(8)')        self.assert_eq(colspec(bit_table.c.b6), 'b6 BIT(32)')        self.assert_eq(colspec(bit_table.c.b7), 'b7 BIT(63)')        self.assert_eq(colspec(bit_table.c.b8), 'b8 BIT(64)')        for col in bit_table.c:            self.assert_(repr(col))        try:            meta.create_all()            meta2 = MetaData(testing.db)            reflected = Table('mysql_bits', meta2, autoload=True)            for table in bit_table, reflected:                def roundtrip(store, expected=None):                    expected = expected or store                    table.insert(store).execute()                    row = list(table.select().execute())[0]                    try:                        self.assert_(list(row) == expected)                    except:                        print "Storing %s" % store                        print "Expected %s" % expected                        print "Found %s" % list(row)                        raise                    table.delete().execute()                roundtrip([0] * 8)                roundtrip([None, None, 0, None, None, None, None, None])

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -