コード例 #1
0
def test_list_of_len_validation():
    with given:
        value = [1, 2]

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

    with given:
        assert result.get_errors() == []
コード例 #2
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() == []
コード例 #3
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)]
コード例 #4
0
def test_list_of_min_max_len_validation(min_length: int, max_length: int):
    with given:
        value = [1, 2]

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

    with then:
        assert result.get_errors() == []
コード例 #5
0
def test_list_of_len_validation_error(value: List[Any]):
    with given:
        length = 2

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

    with then:
        assert result.get_errors() == [
            LengthValidationError(PathHolder(), value, length)
        ]
コード例 #6
0
def test_list_of_min_len_validation_error():
    with given:
        value = [1]
        min_length = 2

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

    with then:
        assert result.get_errors() == [
            MinLengthValidationError(PathHolder(), value, min_length)
        ]
コード例 #7
0
def test_list_of_type_validation_kwargs():
    with given:
        value = 3.14
        path = PathHolder().items[0]["key"]

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

    with then:
        assert result.get_errors() == [
            TypeValidationError(path[1], value, int)
        ]
コード例 #8
0
def test_list_of_max_len_validation_error():
    with given:
        value = [1, 2, 3]
        max_length = 2

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

    with then:
        assert result.get_errors() == [
            MaxLengthValidationError(PathHolder(), value, max_length)
        ]
コード例 #9
0
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() == []
コード例 #10
0
def test_list_more_element_validation_error():
    with given:
        value = [1]

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

    with then:
        assert result.get_errors() == [
            ExtraElementValidationError(PathHolder(),
                                        actual_value=value,
                                        index=0),
        ]
コード例 #11
0
def test_list_heterogeneous_elements_validation():
    with given:
        value = [42, 3.14, "banana"]

    with when:
        result = validate(
            schema.list([
                schema.int(42),
                schema.float(3.14),
                schema.str("banana"),
            ]), value)

    with then:
        assert result.get_errors() == []
コード例 #12
0
def test_list_contains_validation_incorrect_head_element_error():
    with given:
        value = [3, 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=3,
                                 expected_value=1),
        ]
コード例 #13
0
def test_list_contains_validation_no_elements_error():
    with given:
        value = []
        sch = schema.list([..., schema.int, ...])

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            MissingElementValidationError(PathHolder(),
                                          actual_value=value,
                                          index=0),
        ]
コード例 #14
0
def test_list_less_elements_validation_error():
    with given:
        value = [1, 2]

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

    with then:
        assert result.get_errors() == [
            MissingElementValidationError(PathHolder(),
                                          actual_value=value,
                                          index=2),
        ]
コード例 #15
0
def test_list_contains_head_validation_missing_element_error():
    with given:
        value = [1]
        sch = schema.list([schema.int(1), schema.int(2), ...])

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            MissingElementValidationError(PathHolder(),
                                          actual_value=value,
                                          index=1)
        ]
コード例 #16
0
def test_list_contains_validation_incorrect_order_error():
    with given:
        value = [2, 1]
        sch = schema.list([..., schema.int(1), schema.int(2), ...])

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            MissingElementValidationError(PathHolder(),
                                          actual_value=value,
                                          index=2),
        ]
コード例 #17
0
def test_list_contains_validation_extra_body_element_error():
    with given:
        value = [1, 0, 2]
        sch = schema.list([..., schema.int(1), schema.int(2), ...])

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            ValueValidationError(PathHolder()[1],
                                 actual_value=0,
                                 expected_value=2),
        ]
コード例 #18
0
def test_list_of_type_elements_value_validation_error():
    with given:
        expected_value = 42
        actual_value = 43

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

    with then:
        path = PathHolder()[1]
        assert result.get_errors() == [
            ValueValidationError(path, actual_value, expected_value)
        ]
コード例 #19
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),
        ]
コード例 #20
0
def test_list_contains_tail_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),
        ]
コード例 #21
0
def test_list_of_max_len_validation(value: List[Any]):
    with when:
        result = validate(schema.list(schema.int).len(..., 2), value)

    with given:
        assert result.get_errors() == []
コード例 #22
0
def test_list_no_elements_validation():
    with when:
        result = validate(schema.list([]), [])

    with then:
        assert result.get_errors() == []
コード例 #23
0
def test_list_of_elements_validation(value: Any):
    with when:
        result = validate(schema.list(schema.int), value)

    with then:
        assert result.get_errors() == []