def test_string_not_required(): """If the property is not required, None is a valid value""" test_string = String(required=False) test_string.validate(None) assert True
def test_string_invalid_3(): """'foobar" is too long""" test_string = String(validators=[max_length(5)]) with assert_raises(InvalidPropertyError) as cm: test_string.validate('foobar') assert cm.exception.error == 'invalid'
def test_property_required_context(): """Test context-aware property validators""" test_property = String(required=lambda c: c == 'foo') test_property.validate(None) with assert_raises(InvalidPropertyError): test_property.validate(None, context='foo')
def test_string_invalid_1(): """Integer is not a valid value""" test_string = String() with assert_raises(InvalidPropertyError) as cm: test_string.validate(5) assert cm.exception.error == 'invalid'
def test_string_required(): """By default, a value is required""" test_string = String() with assert_raises(InvalidPropertyError) as cm: test_string.validate(None) assert cm.exception.error == 'required'
def test_string_invalid_2(): """'fo' is too short""" test_string = String(validators=[min_length(3)]) with assert_raises(InvalidPropertyError) as cm: test_string.validate('fo') assert cm.exception.error == 'invalid'
def test_string_choices(): """Value can only be 'published' or 'draft'""" test_string = String(validators=[choices(['published', 'draft'])]) test_string.validate('published') with assert_raises(InvalidPropertyError) as cm: test_string.validate('not_a_status') assert cm.exception.error == 'invalid'
def test_string_regex(): """By default, a value is required""" test_string = String(validators=[regex(r'^([a-z]*)$')]) test_string.validate('abc') with assert_raises(InvalidPropertyError) as cm: test_string.validate('1236580002EEEEE') assert cm.exception.error == 'invalid'
def test_property_context(): """Test context-aware property validators""" test_property = String(validators=[min_length(2, context='foo')]) test_property.validate('a') test_property.validate('ab', context='foo') with assert_raises(InvalidPropertyError): test_property.validate('a', context='foo')
def test_string_valid_1(): """'foo' is a valid string""" test_string = String() test_string.validate('foo') assert True