Exemple #1
0
def test_12_validate_many_errors_msg():
    requests = [
        {"first_name": "John", "last_name": "Doe", "age": 40},
        {"first_name": "", "last_name": "Doe", "age": 40},
        {"first_name": "John", "last_name": "", "age": 40},
        {"first_name": "John", "last_name": "Doe", "age": 10},
        {"first_name": "", "last_name": "", "age": 10},
    ]
    rule = {
        "first_name": R.Required,
        "last_name": R.Required,
        "age": [R.Integer, R.Min(18)],
    }

    result, _, errors = validate_many(requests, rule, True)

    assert not result
    assert len(errors) == 5

    assert len(errors[0]) == 0

    assert len(errors[1]) == 1
    assert "first_name" in errors[1]

    assert len(errors[2]) == 1
    assert "last_name" in errors[2]

    assert len(errors[3]) == 1
    assert "age" in errors[3]

    assert len(errors[4]) == 3
    assert "first_name" in errors[4]
    assert "last_name" in errors[4]
    assert "age" in errors[4]
Exemple #2
0
def test_14_validated_data_many():
    reqs = [
        {"first_name": "John", "last_name": "Doe", "age": 33},
        {
            "first_name": "Nick",
            "last_name": "Bush",
            "age": 23,
            "_token": "Ou1WQdWbyTuy4y8jazwA",
        },
        {
            "first_name": "John",
            "last_name": "Watson",
            "age": 30,
            "_token": "Ou1WQdWbyTuy4y8jazwA",
        },
        {
            "first_name": "May",
            "last_name": "Afferson",
            "age": 40,
            "_cookie": "7Juhm38k8eBgo3HVDFKC",
        },
    ]

    rule = {"first_name": "required", "last_name": "required", "age": "min:18"}

    result, validated_datas, _ = validate_many(reqs, rule, return_info=True)

    # result should be True
    assert result

    # all of validated data should contain only first_name, last_name and age
    for data in validated_datas:
        assert ("first_name" in data) and ("last_name" in data) and ("age" in data)
        assert ("_token" not in data) and ("_cookie" not in data)
Exemple #3
0
def test_11_validate_many_errors_msg():
    requests = [{"age": 11}, {"age": 12}, {"age": 13}, {"age": 14}]
    rule = {"age": [R.Integer, R.Min(18)]}
    result, _, errors = validate_many(requests, rule, True)
    assert not result
    assert 4 == len(errors)
    assert "age" in errors[0]
    assert "age" in errors[1]
    assert "age" in errors[2]
    assert "age" in errors[3]
    assert "Min" in errors[0]["age"]
    assert "Min" in errors[1]["age"]
    assert "Min" in errors[2]["age"]
    assert "Min" in errors[3]["age"]
    assert "Got: 11" in errors[0]["age"]["Min"]
    assert "Got: 12" in errors[1]["age"]["Min"]
    assert "Got: 13" in errors[2]["age"]["Min"]
    assert "Got: 14" in errors[3]["age"]["Min"]

    requests = [{"age": 11}, {"age": 12}, {"age": 23}, {"age": 14}]
    rule = {"age": [R.Integer, R.Max(18)]}
    result, _, errors = validate_many(requests, rule, True)
    assert not result
    assert 4 == len(errors)
    assert {} == errors[0]
    assert {} == errors[1]
    assert {} == errors[3]
    assert "age" in errors[2]
    assert "Max" in errors[2]["age"]
    assert "Got: 23" in errors[2]["age"]["Max"]

    requests = [{"name": "Jon"}, {"name": ""}, {"name": ""}, {"name": "Tom"}]
    rule = {"name": [R.Required()]}
    result, _, errors = validate_many(requests, rule, True)
    assert not result
    assert 4 == len(errors)
    assert {} == errors[0]
    assert "name" in errors[1]
    assert "name" in errors[2]
    assert {} == errors[3]
    assert "Required" in errors[1]["name"]
    assert "Required" in errors[2]["name"]
    assert "Field was empty" in errors[1]["name"]["Required"]
    assert "Field was empty" in errors[2]["name"]["Required"]
Exemple #4
0
def test_10_validate_many():
    requests = [{"age": 19}, {"age": 20}, {"age": 21}, {"age": 22}]
    rule = {"age": [R.Integer, R.Min(18)]}
    result = validate_many(requests, rule)
    assert result

    requests = [{"age": 11}, {"age": 12}, {"age": 13}, {"age": 14}]
    rule = {"age": [R.Integer, R.Min(18)]}
    result = validate_many(requests, rule)
    assert not result

    requests = [{"age": 11}, {"age": 12}, {"age": 13}, {"age": 14}]
    rule = {"age": [R.Integer, R.Max(18)]}
    result = validate_many(requests, rule)
    assert result

    requests = [{"age": 21}, {"age": 22}, {"age": 23}, {"age": 24}]
    rule = {"age": [R.Integer, R.Max(18)]}
    result = validate_many(requests, rule)
    assert not result

    requests = [{"name": "John"}, {"name": "Rob"}, {"name": "Greg"}, {"name": "Tom"}]
    rule = {"name": "required|min:3"}
    result = validate_many(requests, rule)
    assert result

    requests = [{"name": "John"}, {"name": ""}, {"name": ""}, {"name": "Tom"}]
    rule = {"name": [R.Required()]}
    result = validate_many(requests, rule)
    assert not result