Beispiel #1
0
def test_str_len_alphabet_validation_error():
    with given:
        value = "banana"
        length = 7
        alphabet = "1234567890"

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

    with then:
        assert result.get_errors() == [
            LengthValidationError(PathHolder(), value, length),
            AlphabetValidationError(PathHolder(), value, alphabet),
        ]
def test_list_more_elements_validation_error():
    with given:
        value = [1, 2, 3]

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

    with then:
        assert result.get_errors() == [
            ExtraElementValidationError(PathHolder(),
                                        actual_value=value,
                                        index=1),
            ExtraElementValidationError(PathHolder(),
                                        actual_value=value,
                                        index=2),
        ]
Beispiel #3
0
def test_list_contains_head_validation_error_extra_element():
    with given:
        value = [0, 1, 2]
        sch = schema.list([schema.int(1), schema.int(2), ...])

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            ValueValidationError(PathHolder()[0],
                                 actual_value=value[0],
                                 expected_value=1),
            ValueValidationError(PathHolder()[1],
                                 actual_value=value[1],
                                 expected_value=2),
        ]
Beispiel #4
0
def test_list_contains_head_validation_incorrect_element_error():
    with given:
        value = [2]
        sch = schema.list([schema.int(1), schema.int(2), ...])

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            ValueValidationError(PathHolder()[0],
                                 actual_value=value[0],
                                 expected_value=1),
            MissingElementValidationError(PathHolder(),
                                          actual_value=value,
                                          index=1)
        ]
Beispiel #5
0
def test_validation_value_error():
    with given:
        actual = "banana"
        expected = "cucumber"

    with when:
        res = ValueValidationError(PathHolder(), actual, expected)

    with then:
        assert repr(res) == "ValueValidationError(PathHolder(), 'banana', 'cucumber')"
Beispiel #6
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)]
Beispiel #7
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_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)
        ]
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)
        ]
Beispiel #10
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),
        ]
Beispiel #11
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)
        ]
Beispiel #12
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))
        ]
Beispiel #13
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),
        ]
Beispiel #14
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),
        ]
Beispiel #15
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)
        ]
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),
        ]
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)
        ]
def test_list_min_len_validation_error():
    with given:
        value = [1]
        min_length = 2

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

    with then:
        assert result.get_errors() == [
            MinLengthValidationError(PathHolder(), value, min_length)
        ]
def test_int_max_value_validation_error():
    with given:
        max_value = 1
        actual_value = 2

    with when:
        result = validate(schema.int.max(max_value), actual_value)

    with then:
        assert result.get_errors() == [
            MaxValueValidationError(PathHolder(), actual_value, max_value)
        ]
def test_int_min_value_validation_error():
    with given:
        min_value = 1
        actual_value = 0

    with when:
        result = validate(schema.int.min(min_value), actual_value)

    with then:
        assert result.get_errors() == [
            MinValueValidationError(PathHolder(), actual_value, min_value)
        ]
Beispiel #21
0
def test_str_min_len_validation_error():
    with given:
        value = "banana"
        min_length = 7

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

    with then:
        assert result.get_errors() == [
            MinLengthValidationError(PathHolder(), value, min_length),
        ]
Beispiel #22
0
def test_str_value_validation_error():
    with given:
        expected_value = "banana"
        actual_value = "cucumber"

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

    with then:
        assert result.get_errors() == [
            ValueValidationError(PathHolder(), actual_value, expected_value),
        ]
Beispiel #23
0
def test_str_regex_validation_error():
    with given:
        pattern = "[0-9]+"
        value = "banana"

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

    with then:
        assert result.get_errors() == [
            RegexValidationError(PathHolder(), value, pattern)
        ]
Beispiel #24
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)
        ]
Beispiel #25
0
def test_const_validation_error():
    with given:
        expected_value = "banana"
        actual_value = "apple"

    with when:
        result = validate(schema.const(expected_value), actual_value)

    with then:
        assert result.get_errors() == [
            ValueValidationError(PathHolder(), actual_value, expected_value)
        ]
Beispiel #26
0
def test_str_max_len_validation_error():
    with given:
        value = "banana"
        max_length = 5

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

    with then:
        assert result.get_errors() == [
            MaxLengthValidationError(PathHolder(), value, max_length)
        ]
Beispiel #27
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_int_value_validation_error():
    with given:
        expected_value = 42
        actual_value = 43

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

    with then:
        assert result.get_errors() == [
            ValueValidationError(PathHolder(), actual_value, expected_value)
        ]
Beispiel #29
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, ))
        ]
def test_dict_extra_key_validation_error():
    with given:
        sch = schema.dict({})
        value = {"id": 1}

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            ExtraKeyValidationError(PathHolder(), value, "id")
        ]