示例#1
0
def test_field_convert():
    f = Field()
    f.validate('whatever', convert=True)

    class MultiTypeField(Field):
        value_type = (bool, int)

    f = MultiTypeField()
    f.validate('True', convert=True)
    f.validate('1', convert=True)
    f.validate(False, convert=True)
    f.validate(0, convert=True)
示例#2
0
def test_field_choices():
    f0 = Field(null=False, choices=[1, '2', ''])

    # null=False priority is higher than choices
    with pytest.raises(ValueError):
        f0.validate('')

    f0.validate(1)
    f0.validate('2')

    with pytest.raises(ValueError):
        f0.validate('1')
    with pytest.raises(ValueError):
        f0.validate(2)
示例#3
0
def test_field_null_values():
    f0 = Field(null_values=(None, 0), null=True)

    assert None is f0.validate(None)
    assert None is f0.validate(0)
    assert '' == f0.validate('')

    f1 = f0.spawn(null=False)
    with pytest.raises(ValueError):
        f1.validate(None)
    with pytest.raises(ValueError):
        f1.validate(0)
    assert '' == f0.validate('')

    # though null is not allowed, value will never be determined as null, so `null=False` won't take any effect
    f2 = Field(null_values=(), null=False)
    assert None is f2.validate(None)
示例#4
0
def test_field_null():
    f0 = Field(null=True)

    assert None is f0.validate(None)
    assert None is f0.validate('')
    assert None is f0.validate(u_(''))
    # 0 is not null, only '' and None are
    assert 0 is f0.validate(0)

    f1 = Field(null=False)
    with pytest.raises(ValueError):
        f1.validate(None)
    with pytest.raises(ValueError):
        f1.validate('')
    with pytest.raises(ValueError):
        f1.validate(u_(''))
    assert 0 is f1.validate(0)