Python sqlalchemy_utils 模块,ChoiceType() 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用sqlalchemy_utils.ChoiceType()

项目:deb-python-sqlalchemy-utils    作者:openstack    | 项目源码 | 文件源码
def User(self, Base):

        class User(Base):
            TYPES = [
                ('admin', 'Admin'),
                ('regular-user', 'Regular user')
            ]

            __tablename__ = 'user'
            id = sa.Column(sa.Integer, primary_key=True)
            type = sa.Column(ChoiceType(TYPES))

            def __repr__(self):
                return 'User(%r)' % self.id

        return User
项目:deb-python-sqlalchemy-utils    作者:openstack    | 项目源码 | 文件源码
def test_string_processing(self, session, User):
        flexmock(ChoiceType).should_receive('_coerce').and_return(
            u'admin'
        )
        user = User(
            type=u'admin'
        )

        session.add(user)
        session.commit()

        user = session.query(User).first()
        assert user.type.value == u'Admin'
项目:deb-python-sqlalchemy-utils    作者:openstack    | 项目源码 | 文件源码
def test_throws_exception_if_no_choices_given(self):
        with pytest.raises(ImproperlyConfigured):
            ChoiceType([])
项目:deb-python-sqlalchemy-utils    作者:openstack    | 项目源码 | 文件源码
def test_init_type(self):
        type_ = ChoiceType([(1, u'something')], impl=sa.Integer)
        assert type_.impl == sa.Integer
项目:deb-python-sqlalchemy-utils    作者:openstack    | 项目源码 | 文件源码
def Order(self, Base, OrderStatus):

        class Order(Base):
            __tablename__ = 'order'
            id_ = sa.Column(sa.Integer, primary_key=True)
            status = sa.Column(
                ChoiceType(OrderStatus, impl=sa.Integer()),
                default=OrderStatus.unpaid,
            )

            def __repr__(self):
                return 'Order(%r, %r)' % (self.id_, self.status)

        return Order