Esempio n. 1
0
def test_03_validator_error_msg():
    request = {"age": 100}
    rule = {"age": [R.Integer, R.Between(18, 90)]}
    val = Validator(request, rule)
    result = val.validate()
    errors = val.get_error_messages()
    assert not result
    assert "age" in errors.keys()
    assert "Between" in errors["age"].keys()

    request = {"age": 28, "name": "John", "surname": "Krasinski"}
    rule = {
        "age": [R.Integer, R.Between(18, 50)],
        "name": [R.Required()],
        "surname": [R.Required(), R.Mail()],
    }
    val = Validator(request, rule)
    result = val.validate()
    errors = val.get_error_messages()

    # Test General
    assert not result
    assert len(errors) == 1
    assert "surname" in errors.keys()

    # Test Error 1
    mail_err = errors["surname"]
    assert len(mail_err) == 1
    assert "Mail" in mail_err.keys()
Esempio n. 2
0
def test_08_validate_error_msg():
    request = {"age": 100}
    rule = {"age": [R.Integer, R.Between(18, 90)]}
    result, validated_data, errors = validate(request, rule, return_info=True)
    assert not result
    assert "age" in errors.keys()
    assert "Between" in errors["age"].keys()

    request = {"age": 28, "name": "John", "surname": "Krasinski"}
    rule = {
        "age": [R.Integer, R.Between(18, 50)],
        "name": [R.Required()],
        "surname": [R.Required(), R.Mail()],
    }
    result, validated_data, errors = validate(request, rule, return_info=True)

    # Test General
    assert not result
    assert len(errors) == 1
    assert "surname" in errors.keys()

    # Test Error 1
    mail_err = errors["surname"]
    assert len(mail_err) == 1
    assert "Mail" in mail_err.keys()
Esempio n. 3
0
def test_parser_mixed_01():
    # checking abstract university student object rules
    rules = {
        "name": R.Required(),
        "suranme": R.Required,
        "university_mail": [R.Required, "mail"],
        "faculty_name": [R.Required],
        "current_semester": ["required", R.Min(1)],
        "credits_amount": [R.Required, R.Between(0, 240)],
        "credits_current_semester": [R.Min(0), R.Max(75)],
        "debt": [R.Max(0)],
    }
    parsed_rules_goal = {
        "name": [R.Required()],
        "suranme": [R.Required()],
        "university_mail": [R.Required(), R.Mail()],
        "faculty_name": [R.Required()],
        "current_semester": [R.Required(), R.Min(1)],
        "credits_amount": [R.Required(), R.Between(0, 240)],
        "credits_current_semester": [R.Min(0), R.Max(75)],
        "debt": [R.Max(0)],
    }
    parsed_rules = Parser(rules).parse()

    assert instances_checker(parsed_rules, parsed_rules_goal)
    assert parsed_rules["current_semester"][1].min == 1
    assert parsed_rules["credits_amount"][1].min == 0
    assert parsed_rules["credits_amount"][1].max == 240
    assert parsed_rules["credits_current_semester"][0].min == 0
    assert parsed_rules["credits_current_semester"][1].max == 75
    assert parsed_rules["debt"][0].max == 0
Esempio n. 4
0
def test_parser_mixed_02():
    # checking abstract football match object rules
    rules = {
        "team1": "required",
        "team2": R.Required,
        "team1_goals": ["required", "mail"],
        "team2_goals": [R.Required],
        "offides": [R.Required()],
        "fouls": [R.Required()],
        "corner_kicks": "required|min:1",
        "penalties": ["max:0"],
        "yellow_cards": [R.Required(), R.Between(0, 240)],
        "red_cards": [R.Min(0), R.Max(75)],
    }
    parsed_rules_goal = {
        "team1": [R.Required()],
        "team2": [R.Required()],
        "team1_goals": [R.Required(), R.Mail()],
        "team2_goals": [R.Required()],
        "offides": [R.Required()],
        "fouls": [R.Required()],
        "corner_kicks": [R.Required(), R.Min(1)],
        "penalties": [R.Max(0)],
        "yellow_cards": [R.Required(), R.Between(0, 240)],
        "red_cards": [R.Min(0), R.Max(75)],
    }
    parsed_rules = Parser(rules).parse()

    assert instances_checker(parsed_rules, parsed_rules_goal)
    assert parsed_rules["corner_kicks"][1].min == 1
    assert parsed_rules["yellow_cards"][1].min == 0
    assert parsed_rules["yellow_cards"][1].max == 240
    assert parsed_rules["red_cards"][0].min == 0
    assert parsed_rules["red_cards"][1].max == 75
Esempio n. 5
0
def test_04_validator_error_msg():
    request = {
        "age": 53,
        "name": "Peter",
        "surname": "Griffin",
        "profession": "",
        "mail": "petergriffin.com",
    }
    rule = {
        "age": [R.Integer, R.Between(18, 50),
                R.Required()],
        "name": [R.Required()],
        "surname": [R.Required()],
        "profession": [R.Required, R.Mail],
        "mail": [R.Required(), R.Mail()],
    }
    val = Validator(request, rule)
    result = val.validate()
    errors = val.get_error_messages()

    # Test General
    assert not result
    assert len(errors) == 3
    assert "age" in errors
    assert "profession" in errors
    assert "mail" in errors

    # Test Error 1
    age_err = errors["age"]
    assert len(age_err) == 1
    assert "Between" in age_err

    # Test Error 2
    profession_err = errors["profession"]
    assert len(profession_err) == 2
    assert "Required" in profession_err
    assert "Mail" in profession_err

    # Test Error 3
    mail_err = errors["mail"]
    assert len(mail_err) == 1
    assert "Mail" in mail_err
Esempio n. 6
0
def test_09_validate_error_msg():
    request = {
        "age": 53,
        "name": "Peter",
        "surname": "Griffin",
        "profession": "",
        "mail": "petergriffin.com",
    }
    rule = {
        "age": [R.Integer, R.Between(18, 50),
                R.Required()],
        "name": [R.Required()],
        "surname": [R.Required()],
        "profession": [R.Required, R.Mail],
        "mail": [R.Required(), R.Mail()],
    }
    result, validated_data, errors = validate(request, rule, return_info=True)

    # Test General
    assert not result
    assert len(errors) == 3
    assert "age" in errors
    assert "profession" in errors
    assert "mail" in errors

    # Test Error 1
    age_err = errors["age"]
    assert len(age_err) == 1
    assert "Between" in age_err

    # Test Error 2
    profession_err = errors["profession"]
    assert len(profession_err) == 2
    assert "Required" in profession_err
    assert "Mail" in profession_err

    # Test Error 3
    mail_err = errors["mail"]
    assert len(mail_err) == 1
    assert "Mail" in mail_err
Esempio n. 7
0
def test_parser_arrays_simple():
    key1, key2 = "credits_amount", "semester"
    min_val, max_val = 10, 100

    rules = {key1: [f"between:{min_val},{max_val}"], key2: ["required"]}
    parsed_rules = Parser(rules).parse()

    assert len(parsed_rules) == 2
    assert key1 in parsed_rules.keys()
    assert key2 in parsed_rules.keys()
    assert isinstance(parsed_rules[key1][0], R.Between)
    assert isinstance(parsed_rules[key2][0], R.Required)
    assert parsed_rules[key1][0].min == min_val
    assert parsed_rules[key1][0].max == max_val

    rules = {key1: [R.Required, R.Mail], key2: R.Required}
    parsed_rules = Parser(rules).parse()

    assert isinstance(parsed_rules[key1][0], R.Required)
    assert isinstance(parsed_rules[key1][1], R.Mail)
    assert isinstance(parsed_rules[key2][0], R.Required)

    min_val, max_val = -31, 1928
    rules = {
        key1: [R.Required(), R.Max(max_val), R.Min(min_val)],
        key2: [R.Mail(), R.Between(min_val, max_val)],
    }
    parsed_rules = Parser(rules).parse()

    assert isinstance(parsed_rules[key1][0], R.Required)
    assert isinstance(parsed_rules[key1][1], R.Max)
    assert isinstance(parsed_rules[key1][2], R.Min)
    assert isinstance(parsed_rules[key2][0], R.Mail)
    assert isinstance(parsed_rules[key2][1], R.Between)

    assert parsed_rules[key1][1].max == max_val
    assert parsed_rules[key1][2].min == min_val
    assert parsed_rules[key2][1].min == min_val
    assert parsed_rules[key2][1].max == max_val