Exemplo n.º 1
0
def test_validator_validate_pass():
    with when:
        result = validate(schema.int, 42)

    with then:
        assert isinstance(result, ValidationResult)
        assert not result.has_errors()
Exemplo n.º 2
0
def test_str_contains_validation(value: str):
    with given:
        substr = "banana"

    with when:
        result = validate(schema.str.contains(substr), value)

    with then:
        assert result.get_errors() == []
def test_int_value_validation():
    with given:
        value = 42

    with when:
        result = validate(schema.int(value), value)

    with then:
        assert result.get_errors() == []
def test_dict_relaxed_no_keys_validation(value: Dict[Any, Any]):
    with given:
        sch = schema.dict({...:...})

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
Exemplo n.º 5
0
def test_str_min_max_len_validation_error(min_length: int, max_length: int):
    with given:
        value = "banana"

    with when:
        result = validate(schema.str.len(min_length, max_length), value)

    with then:
        assert len(result.get_errors()) == 1
def test_dict_type_validation(value: Dict[Any, Any]):
    with given:
        sch = schema.dict

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
def test_list_len_validation():
    with given:
        value = [1, 2]

    with when:
        result = validate(schema.list.len(2), value)

    with then:
        assert result.get_errors() == []
Exemplo n.º 8
0
def test_bytes_value_validation():
    with given:
        value = b"banana"

    with when:
        result = validate(schema.bytes(value), value)

    with then:
        assert result.get_errors() == []
Exemplo n.º 9
0
def test_list_contains_head_validation(value: List[Any]):
    with given:
        sch = schema.list([schema.int(1), schema.int(2), ...])

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
def test_list_min_max_len_validation_error(min_length: int, max_length: int):
    with given:
        value = [1, 2]

    with when:
        result = validate(schema.list.len(min_length, max_length), value)

    with then:
        assert len(result.get_errors()) == 1
Exemplo n.º 11
0
def test_float_value_validation():
    with given:
        value = 3.14

    with when:
        result = validate(schema.float(value), value)

    with then:
        assert result.get_errors() == []
Exemplo n.º 12
0
def test_str_len_validation():
    with given:
        value = "banana"

    with when:
        result = validate(schema.str.len(6), value)

    with then:
        assert result.get_errors() == []
def test_dict_no_keys_validation():
    with given:
        sch = schema.dict({})
        value = {}

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
Exemplo n.º 14
0
def test_str_regex_validation():
    with given:
        pattern = "[a-z]+"
        value = "banana"

    with when:
        result = validate(schema.str.regex(pattern), value)

    with then:
        assert result.get_errors() == []
Exemplo n.º 15
0
def test_list_of_type_elements_validation_error():
    with given:
        value = 3.14

    with when:
        result = validate(schema.list(schema.int), [42, value])

    with then:
        path = PathHolder()[1]
        assert result.get_errors() == [TypeValidationError(path, value, int)]
Exemplo n.º 16
0
def test_none_validation_error():
    with given:
        value = False

    with when:
        result = validate(schema.none, value)

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder(), value, type(None))
        ]
def test_list_type_validation_error():
    with given:
        value = {}

    with when:
        result = validate(schema.list, value)

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder(), value, list),
        ]
Exemplo n.º 18
0
def test_bytes_type_validation_error():
    with given:
        value = "banana"

    with when:
        result = validate(schema.bytes, value)

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder(), value, bytes),
        ]
Exemplo n.º 19
0
def test_bool_type_validation_error():
    with given:
        value = "True"

    with when:
        result = validate(schema.bool, value)

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder(), value, bool)
        ]
def test_list_len_validation_error(value: List[Any]):
    with given:
        length = 2

    with when:
        result = validate(schema.list.len(length), value)

    with then:
        assert result.get_errors() == [
            LengthValidationError(PathHolder(), value, length)
        ]
Exemplo n.º 21
0
def test_str_contains_validation_error(value: str):
    with given:
        substr = "banana"

    with when:
        result = validate(schema.str.contains(substr), value)

    with then:
        assert result.get_errors() == [
            SubstrValidationError(PathHolder(), value, substr)
        ]
Exemplo n.º 22
0
def test_int_type_validation_error():
    with given:
        value = 3.14

    with when:
        result = validate(schema.int, value)

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder(), value, int)
        ]
Exemplo n.º 23
0
def test_str_type_validation_error():
    with given:
        value = ["b", "a", "n", "a", "n", "a"]

    with when:
        result = validate(schema.str, value)

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder(), value, str),
        ]
Exemplo n.º 24
0
def test_str_len_validation_error(value: str):
    with given:
        length = 7

    with when:
        result = validate(schema.str.len(length), value)

    with then:
        assert result.get_errors() == [
            LengthValidationError(PathHolder(), value, length),
        ]
Exemplo n.º 25
0
def test_float_type_validation_error():
    with given:
        value = 42

    with when:
        result = validate(schema.float, value)

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder(), value, float)
        ]
Exemplo n.º 26
0
def test_any_type_validation_kwargs():
    with given:
        actual_value = False
        path = PathHolder().items[0]["key"]

    with when:
        result = validate(schema.any(schema.none), actual_value, path=path)

    with then:
        assert result.get_errors() == [
            SchemaMismatchValidationError(path, actual_value, (schema.none, ))
        ]
Exemplo n.º 27
0
def test_any_types_validation_error():
    with given:
        value = None
        types = (schema.int, schema.str)

    with when:
        result = validate(schema.any(*types), value)

    with then:
        assert result.get_errors() == [
            SchemaMismatchValidationError(PathHolder(), value, types)
        ]
Exemplo n.º 28
0
def test_none_type_validation_kwargs():
    with given:
        actual_value = False
        path = PathHolder().items[0]["key"]

    with when:
        result = validate(schema.none, actual_value, path=path)

    with then:
        assert result.get_errors() == [
            TypeValidationError(path, actual_value, type(None))
        ]
def test_list_homogeneous_elements_validation():
    with given:
        value = [1, 2]

    with when:
        result = validate(schema.list([
            schema.int(1),
            schema.int(2),
        ]), value)

    with then:
        assert result.get_errors() == []
def test_list_max_len_validation_error():
    with given:
        value = [1, 2, 3]
        max_length = 2

    with when:
        result = validate(schema.list.len(..., max_length), value)

    with then:
        assert result.get_errors() == [
            MaxLengthValidationError(PathHolder(), value, max_length)
        ]