sqlalchemy_utils.ChoiceType

Here are the examples of the python api sqlalchemy_utils.ChoiceType taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

7 Examples 7

Example 1

Project: sqlalchemy-utils Source File: test_choice.py
Function: user
    @pytest.fixture
    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

Example 2

Project: sqlalchemy-utils Source File: test_choice.py
Function: order
    @pytest.fixture
    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

Example 3

Project: sqlalchemy-utils Source File: test_choice.py
    @pytest.fixture
    def OrderNullable(self, Base, OrderStatus):

        class OrderNullable(Base):
            __tablename__ = 'order_nullable'
            id_ = sa.Column(sa.Integer, primary_key=True)
            status = sa.Column(
                ChoiceType(OrderStatus, impl=sa.Integer()),
                nullable=True,
            )

        return OrderNullable

Example 4

Project: wtforms-alchemy Source File: test_types.py
    def test_choice_type_uses_custom_coerce_func(self):
        choices = [(u'1', u'choice 1'), (u'2', u'choice 2')]
        self.init(type_=ChoiceType(choices))
        self.assert_type('test_column', SelectField)
        model = self.ModelTest(test_column=u'2')
        form = self.form_class(obj=model)
        assert '<option selected value="2">' in str(form.test_column)

Example 5

Project: sqlalchemy-utils Source File: test_choice.py
    def test_throws_exception_if_no_choices_given(self):
        with pytest.raises(ImproperlyConfigured):
            ChoiceType([])

Example 6

Project: sqlalchemy-utils Source File: test_choice.py
Function: test_init_type
    def test_init_type(self):
        type_ = ChoiceType([(1, u'something')], impl=sa.Integer)
        assert type_.impl == sa.Integer

Example 7

Project: wtforms-alchemy Source File: test_types.py
    def test_choice_type_converts_to_select_field(self):
        choices = [(u'1', u'choice 1'), (u'2', u'choice 2')]
        self.init(type_=ChoiceType(choices))
        self.assert_type('test_column', SelectField)
        assert self.form_class().test_column.choices == choices