예제 #1
0
def test_string():
    validator = validators.string()
    assert validator('1') == '1'
    assert validator(1) == '1'
    assert validator(None) == None
    assert validator([]) == []
    assert validator({}) == {}

    validator = validators.string(max_length=5)

    assert 'a' * 5 == validator('a' * 5)
    with pytest.raises(ValidationException):
        validator('a' * 6)
예제 #2
0
def test_validation_strings():
    a_field = field('a',
                    validators=[validators.required(),
                                validators.string()])
    f_field = field('f', validators=[validators.required()])
    g_field = field('g')

    a_serializer = serializer(
        a_field,
        f_field,
        g_field,
    )

    from_json = {
        'a': 1,
        'f': [],
    }

    with pytest.raises(ValidationException) as exc_info:
        a_serializer.deserialize(from_json)

    assert hasattr(exc_info.value, 'errors')
    assert exc_info.value.errors == {
        'f': ['This field is required'],
    }
예제 #3
0
def test_validation_strings():
    a_field = field('a',
                    validators=[validators.required(),
                                validators.string()])
    f_field = field('f', validators=[validators.required()])
    g_field = field('g')

    a_serializer = serializer(
        a_field,
        f_field,
        g_field,
    )

    from_json = {
        'a': 1,
        'f': [],
    }

    try:
        a_serializer.deserialize(from_json)
    except Exception as e:
        assert hasattr(e, 'errors')
        assert e.errors == {
            'f': ['This field is required'],
        }
예제 #4
0
def test_field_multiple_validation():
    serializer = multiple_field('e',
                                validators=[validators.string(max_length=1)])
    test_obj = {'e': ['a', 'bb']}
    target = {}
    errors = None
    try:
        serializer.deserialize(test_obj, target)
    except ValidationException as e:
        errors = e.errors

    assert errors == {'e': {1: ['This field is to long, max length is 1']}}