示例#1
0
def test_length_repr():
    assert (
        repr(validate.Length(min=None, max=None, error=None, equal=None))
        == "<Length(min=None, max=None, equal=None, error=None)>"
    )
    assert repr(
        validate.Length(min=1, max=3, error="foo", equal=None)
    ) == "<Length(min=1, max=3, equal=None, error={!r})>".format("foo")
    assert repr(
        validate.Length(min=None, max=None, error="foo", equal=5)
    ) == "<Length(min=None, max=None, equal=5, error={!r})>".format("foo")
示例#2
0
def test_length_equal():
    assert validate.Length(equal=3)("foo") == "foo"
    assert validate.Length(equal=3)([1, 2, 3]) == [1, 2, 3]
    assert validate.Length(equal=None)("") == ""
    assert validate.Length(equal=None)([]) == []

    with pytest.raises(ValidationError):
        validate.Length(equal=2)("foo")
    with pytest.raises(ValidationError):
        validate.Length(equal=2)([1, 2, 3])

    with pytest.raises(ValueError):
        validate.Length(1, None, equal=3)("foo")
    with pytest.raises(ValueError):
        validate.Length(None, 5, equal=3)("foo")
    with pytest.raises(ValueError):
        validate.Length(1, 5, equal=3)("foo")
示例#3
0
def test_length_custom_message():
    v = validate.Length(5, 6, error="{input} is not between {min} and {max}")
    with pytest.raises(ValidationError) as excinfo:
        v("foo")
    assert "foo is not between 5 and 6" in str(excinfo)

    v = validate.Length(5, None, error="{input} is shorter than {min}")
    with pytest.raises(ValidationError) as excinfo:
        v("foo")
    assert "foo is shorter than 5" in str(excinfo)

    v = validate.Length(None, 2, error="{input} is longer than {max}")
    with pytest.raises(ValidationError) as excinfo:
        v("foo")
    assert "foo is longer than 2" in str(excinfo)

    v = validate.Length(
        None, None, equal=4, error="{input} does not have {equal}"
    )
    with pytest.raises(ValidationError) as excinfo:
        v("foo")
    assert "foo does not have 4" in str(excinfo)
示例#4
0
class UserSchema(Schema):
    id = fields.Int(dump_only=True)
    email = fields.Str(
        required=True,
        validate=validate.Email(error="Not a valid email address"),
    )
    password = fields.Str(
        required=True,
        validate=[validate.Length(min=6, max=36)],
        load_only=True,
    )
    joined_on = fields.DateTime(dump_only=True)

    # Clean up data
    @pre_load
    def process_input(self, data):
        data["email"] = data["email"].lower().strip()
        return data

    # We add a post_dump hook to add an envelope to responses
    @post_dump(pass_many=True)
    def wrap(self, data, many):
        key = "users" if many else "user"
        return {key: data}
示例#5
0
def test_length_max():
    assert validate.Length(1, 3)("foo") == "foo"
    assert validate.Length(1, 3)([1, 2, 3]) == [1, 2, 3]
    assert validate.Length(None, 1)("a") == "a"
    assert validate.Length(None, 1)([1]) == [1]
    assert validate.Length()("") == ""
    assert validate.Length()([]) == []
    assert validate.Length(2, 2)("ab") == "ab"
    assert validate.Length(2, 2)([1, 2]) == [1, 2]

    with pytest.raises(ValidationError):
        validate.Length(1, 2)("foo")
    with pytest.raises(ValidationError):
        validate.Length(1, 2)([1, 2, 3])
    with pytest.raises(ValidationError):
        validate.Length(None, 2)("foo")
    with pytest.raises(ValidationError):
        validate.Length(None, 2)([1, 2, 3])
示例#6
0
def test_length_min():
    assert validate.Length(3, 5)("foo") == "foo"
    assert validate.Length(3, 5)([1, 2, 3]) == [1, 2, 3]
    assert validate.Length(0)("a") == "a"
    assert validate.Length(0)([1]) == [1]
    assert validate.Length()("") == ""
    assert validate.Length()([]) == []
    assert validate.Length(1, 1)("a") == "a"
    assert validate.Length(1, 1)([1]) == [1]

    with pytest.raises(ValidationError):
        validate.Length(4, 5)("foo")
    with pytest.raises(ValidationError):
        validate.Length(4, 5)([1, 2, 3])
    with pytest.raises(ValidationError):
        validate.Length(5)("foo")
    with pytest.raises(ValidationError):
        validate.Length(5)([1, 2, 3])