当前位置: 首页>>代码示例>>Python>>正文


Python types.Enum方法代码示例

本文整理汇总了Python中sqlalchemy.types.Enum方法的典型用法代码示例。如果您正苦于以下问题:Python types.Enum方法的具体用法?Python types.Enum怎么用?Python types.Enum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlalchemy.types的用法示例。


在下文中一共展示了types.Enum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: print_create_table

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def print_create_table(tables):
    app.config.from_object('config.default')
    database.init_app(app)

    engine = database.session.get_bind()

    for class_name in tables:
        cls = get_class(class_name)

        for c in cls.__table__.columns:
            if not isinstance(c.type, Enum):
                continue
            t = c.type
            sql = str(CreateEnumType(t).compile(engine))
            click.echo(sql.strip() + ';')

        for index in cls.__table__.indexes:
            sql = str(CreateIndex(index).compile(engine))
            click.echo(sql.strip() + ';')

        sql = str(CreateTable(cls.__table__).compile(engine))
        click.echo(sql.strip() + ';') 
开发者ID:EdwardBetts,项目名称:osm-wikidata,代码行数:24,代码来源:cli.py

示例2: test_should_enum_convert_enum

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_should_enum_convert_enum():
    field = get_field(types.Enum(enum.Enum("TwoNumbers", ("one", "two"))))
    field_type = field.type()
    assert isinstance(field_type, graphene.Enum)
    assert field_type._meta.name == "TwoNumbers"
    assert hasattr(field_type, "ONE")
    assert not hasattr(field_type, "one")
    assert hasattr(field_type, "TWO")
    assert not hasattr(field_type, "two")

    field = get_field(types.Enum("one", "two", name="two_numbers"))
    field_type = field.type()
    assert isinstance(field_type, graphene.Enum)
    assert field_type._meta.name == "TwoNumbers"
    assert hasattr(field_type, "ONE")
    assert not hasattr(field_type, "one")
    assert hasattr(field_type, "TWO")
    assert not hasattr(field_type, "two") 
开发者ID:graphql-python,项目名称:graphene-sqlalchemy,代码行数:20,代码来源:test_converter.py

示例3: test_create_table

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_create_table(self, connection):
        metadata = self.metadata
        t1 = Table(
            "table",
            metadata,
            Column("id", Integer, primary_key=True),
            Column(
                "value", Enum("one", "two", "three", name="onetwothreetype")
            ),
        )
        t1.create(connection)
        t1.create(connection, checkfirst=True)  # check the create
        connection.execute(t1.insert(), value="two")
        connection.execute(t1.insert(), value="three")
        connection.execute(t1.insert(), value="three")
        eq_(
            connection.execute(t1.select().order_by(t1.c.id)).fetchall(),
            [(1, "two"), (2, "three"), (3, "three")],
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:21,代码来源:test_types.py

示例4: test_generate_multiple

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_generate_multiple(self):
        """Test that the same enum twice only generates once
        for the create_all() call, without using checkfirst.

        A 'memo' collection held by the DDL runner
        now handles this.

        """
        metadata = self.metadata

        e1 = Enum("one", "two", "three", name="myenum")
        Table("e1", metadata, Column("c1", e1))

        Table("e2", metadata, Column("c1", e1))

        metadata.create_all(checkfirst=False)
        metadata.drop_all(checkfirst=False)
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ] 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:22,代码来源:test_types.py

示例5: test_generate_alone_on_metadata

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_generate_alone_on_metadata(self):
        """Test that the same enum twice only generates once
        for the create_all() call, without using checkfirst.

        A 'memo' collection held by the DDL runner
        now handles this.

        """
        metadata = self.metadata

        Enum("one", "two", "three", name="myenum", metadata=self.metadata)

        metadata.create_all(checkfirst=False)
        assert "myenum" in [e["name"] for e in inspect(testing.db).get_enums()]
        metadata.drop_all(checkfirst=False)
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ] 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:20,代码来源:test_types.py

示例6: test_generate_multiple_on_metadata

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_generate_multiple_on_metadata(self):
        metadata = self.metadata

        e1 = Enum("one", "two", "three", name="myenum", metadata=metadata)

        t1 = Table("e1", metadata, Column("c1", e1))

        t2 = Table("e2", metadata, Column("c1", e1))

        metadata.create_all(checkfirst=False)
        assert "myenum" in [e["name"] for e in inspect(testing.db).get_enums()]
        metadata.drop_all(checkfirst=False)
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ]

        e1.create()  # creates ENUM
        t1.create()  # does not create ENUM
        t2.create()  # does not create ENUM 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:21,代码来源:test_types.py

示例7: test_remain_on_table_metadata_wide

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_remain_on_table_metadata_wide(self):
        metadata = self.metadata

        e1 = Enum("one", "two", "three", name="myenum", metadata=metadata)
        table = Table("e1", metadata, Column("c1", e1))

        # need checkfirst here, otherwise enum will not be created
        assert_raises_message(
            sa.exc.ProgrammingError,
            '.*type "myenum" does not exist',
            table.create,
        )
        table.create(checkfirst=True)
        table.drop()
        table.create(checkfirst=True)
        table.drop()
        assert "myenum" in [e["name"] for e in inspect(testing.db).get_enums()]
        metadata.drop_all()
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ] 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:23,代码来源:test_types.py

示例8: test_standalone_enum

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_standalone_enum(self):
        metadata = MetaData(testing.db)
        etype = Enum(
            "four", "five", "six", name="fourfivesixtype", metadata=metadata
        )
        etype.create()
        try:
            assert testing.db.dialect.has_type(testing.db, "fourfivesixtype")
        finally:
            etype.drop()
            assert not testing.db.dialect.has_type(
                testing.db, "fourfivesixtype"
            )
        metadata.create_all()
        try:
            assert testing.db.dialect.has_type(testing.db, "fourfivesixtype")
        finally:
            metadata.drop_all()
            assert not testing.db.dialect.has_type(
                testing.db, "fourfivesixtype"
            ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:23,代码来源:test_types.py

示例9: test_reflection

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_reflection(self):
        metadata = self.metadata
        etype = Enum(
            "four", "five", "six", name="fourfivesixtype", metadata=metadata
        )
        Table(
            "table",
            metadata,
            Column("id", Integer, primary_key=True),
            Column(
                "value", Enum("one", "two", "three", name="onetwothreetype")
            ),
            Column("value2", etype),
        )
        metadata.create_all()
        m2 = MetaData(testing.db)
        t2 = Table("table", m2, autoload=True)
        eq_(t2.c.value.type.enums, ["one", "two", "three"])
        eq_(t2.c.value.type.name, "onetwothreetype")
        eq_(t2.c.value2.type.enums, ["four", "five", "six"])
        eq_(t2.c.value2.type.name, "fourfivesixtype") 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:23,代码来源:test_types.py

示例10: test_array_plus_native_enum_create

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_array_plus_native_enum_create(self):
        m = MetaData()
        t = Table(
            "t",
            m,
            Column(
                "data_1",
                self.ARRAY(postgresql.ENUM("a", "b", "c", name="my_enum_1")),
            ),
            Column(
                "data_2",
                self.ARRAY(types.Enum("a", "b", "c", name="my_enum_2")),
            ),
        )

        t.create(testing.db)
        eq_(
            set(e["name"] for e in inspect(testing.db).get_enums()),
            set(["my_enum_1", "my_enum_2"]),
        )
        t.drop(testing.db)
        eq_(inspect(testing.db).get_enums(), []) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:24,代码来源:test_types.py

示例11: test_enum_compile

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_enum_compile(self):
        e1 = Enum("x", "y", "z", name="somename")
        t1 = Table("sometable", MetaData(), Column("somecolumn", e1))
        self.assert_compile(
            schema.CreateTable(t1),
            "CREATE TABLE sometable (somecolumn " "ENUM('x','y','z'))",
        )
        t1 = Table(
            "sometable",
            MetaData(),
            Column(
                "somecolumn",
                Enum("x", "y", "z", native_enum=False, create_constraint=True),
            ),
        )
        self.assert_compile(
            schema.CreateTable(t1),
            "CREATE TABLE sometable (somecolumn "
            "VARCHAR(1), CHECK (somecolumn IN ('x', "
            "'y', 'z')))",
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:23,代码来源:test_types.py

示例12: test_broken_enum_returns_blanks

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_broken_enum_returns_blanks(self):
        t = Table(
            "enum_missing",
            self.metadata,
            Column("id", Integer, primary_key=True),
            Column("e1", sqltypes.Enum("one", "two", "three")),
            Column("e2", mysql.ENUM("one", "two", "three")),
        )
        t.create()

        with testing.db.connect() as conn:
            conn.execute(
                t.insert(), {"e1": "nonexistent", "e2": "nonexistent"}
            )
            conn.execute(t.insert(), {"e1": "", "e2": ""})
            conn.execute(t.insert(), {"e1": "two", "e2": "two"})
            conn.execute(t.insert(), {"e1": None, "e2": None})

            eq_(
                conn.execute(
                    select([t.c.e1, t.c.e2]).order_by(t.c.id)
                ).fetchall(),
                [("", ""), ("", ""), ("two", "two"), (None, None)],
            ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:26,代码来源:test_types.py

示例13: test_python_type

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_python_type(self):
        eq_(types.Integer().python_type, int)
        eq_(types.Numeric().python_type, decimal.Decimal)
        eq_(types.Numeric(asdecimal=False).python_type, float)
        eq_(types.LargeBinary().python_type, util.binary_type)
        eq_(types.Float().python_type, float)
        eq_(types.Interval().python_type, datetime.timedelta)
        eq_(types.Date().python_type, datetime.date)
        eq_(types.DateTime().python_type, datetime.datetime)
        eq_(types.String().python_type, str)
        eq_(types.Unicode().python_type, util.text_type)
        eq_(types.Enum("one", "two", "three").python_type, str)

        assert_raises(
            NotImplementedError, lambda: types.TypeEngine().python_type
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:18,代码来源:test_types.py

示例14: test_pep435_custom_sort_key

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_pep435_custom_sort_key(self):
        one, two, a_member, b_member = (
            self.one,
            self.two,
            self.a_member,
            self.b_member,
        )

        def sort_enum_key_value(value):
            return str(value.value)

        typ = Enum(self.SomeEnum, sort_key_function=sort_enum_key_value)
        is_(typ.sort_key_function, sort_enum_key_value)

        eq_(
            sorted([two, one, a_member, b_member], key=typ.sort_key_function),
            [one, two, a_member, b_member],
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:20,代码来源:test_types.py

示例15: test_enum_constraint_type_doesnt_double

# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import Enum [as 别名]
def test_enum_constraint_type_doesnt_double(self):
        m1 = MetaData()

        t1 = Table(
            "x",
            m1,
            Column("flag", Enum("a", "b", "c", create_constraint=True)),
        )
        eq_(
            len([c for c in t1.constraints if isinstance(c, CheckConstraint)]),
            1,
        )
        m2 = MetaData()
        t2 = t1.to_metadata(m2)

        eq_(
            len([c for c in t2.constraints if isinstance(c, CheckConstraint)]),
            1,
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:21,代码来源:test_metadata.py


注:本文中的sqlalchemy.types.Enum方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。