def test_conflicting_dict_name_fails():
    document = {
        "list_of_values": [
            "test1",
            600,
            ["test3", 1, 2, {
                "field1": "testfield",
                "field2": 5
            }],
        ],
        "list_of_values_2": {
            "1": "test1",
            "2": 501,
            "3": ["test2", 1, 2, 55]
        },
        "list_of_values_3": ["test10", 600, "test11"],
    }
    schema = get_extended_dict_schema()
    schema["list_of_values"]["name"] = "hello"

    v = Validator(schema)
    with pytest.raises(AttributeError):
        v.normalized_as_object(document)

    v.normalized_as_dict(document, allow_name_conflicts=True)
def test_readable_schema_wrong_naming():
    document = {"produce": ["Apple", 5, "High"]}
    schema = {
        "unknown": {
            "type":
            "list",
            "name":
            "fruits",
            "items": [
                {
                    "type": "string"
                },
                {
                    "type": "integer",
                    "min": 0
                },
                {
                    "type": "string"
                },
            ],
        }
    }
    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {"produce": ["unknown field"]}
    assert v.normalized_as_dict(document) == {
        "produce": {
            0: "Apple",
            1: 5,
            2: "High"
        }
    }
    assert v.errors == {}
예제 #3
0
def test_simple_list_validation_works():
    document = ["test1", 600, "test2"]
    schema = get_simple_list_schema()

    v = Validator(schema)
    assert v.normalized_as_dict(document) == {0: "test1", 1: 600, 2: "test2"}
    assert v.errors == {}
예제 #4
0
def test_extended_dict_validation_works():
    document = {
        "list_of_values": [
            "test1",
            600,
            ["test3", 1, 2, {
                "field1": "testfield",
                "field2": 5
            }],
        ],
        "list_of_values_2": {
            "1": "test1",
            "2": 501,
            "3": ["test2", 1, 2, 55]
        },
        "list_of_values_3": ["test10", 600, "test11"],
    }
    schema = get_extended_dict_schema()

    dict_schema = deepcopy(schema)
    v = Validator(dict_schema)
    assert v.normalized_as_dict(document) == {
        "first_val": {
            0: "test1",
            1: 600,
            2: {
                0: "test3",
                1: 1,
                "hi": 2,
                3: {
                    "field01231": "testfield",
                    "field2": 5
                }
            },
        },
        "list_of_values_2": {
            "1": "test1",
            "2": 501,
            "3": {
                0: "test2",
                "second_int": 1,
                2: 2,
                3: 55
            },
        },
        "hello": {
            0: "test10",
            1: 600,
            2: "test11"
        },
    }
    assert v.errors == {}
예제 #5
0
def test_simple_dict_validation_works():
    document = {"list_of_values": ["test1", 600, "test2"]}
    schema = get_simple_dict_schema()

    v = Validator(schema)
    assert v.normalized_as_dict(document) == {
        "hello": {
            0: "test1",
            1: 600,
            2: "test2"
        }
    }
    assert v.errors == {}
def test_list_custom_coerce_rules_work_in_list():
    def multiply(value):
        return value * 2

    schema = {"type": "list", "items": [{"type": "integer", "coerce": multiply}]}

    document = [2]

    v = Validator(schema)
    assert v.validate(document) is True
    assert v.errors == {}
    assert v.normalized(document) == [4]
    assert v.normalized_as_dict(document) == {0: 4}
def test_coerce_rules_work_in_list_thats_exceeded_len():
    schema = {
        "type": "list",
        "items": [
            {"type": "integer", "coerce": lambda x: x * 2},
            {"type": "integer", "coerce": lambda x: int(x * 6)},
        ],
    }

    document = [2]

    v = Validator(schema, allow_list_missing=True)
    assert v.validate(document) is True
    assert v.errors == {}
    assert v.normalized(document) == [4]
    assert v.normalized_as_dict(document) == {0: 4}
def test_list_lambda_coerce_rules_work_in_list():
    schema = {
        "type": "list",
        "items": [
            {"type": "integer", "coerce": lambda x: x * 2},
            {"type": "integer", "coerce": lambda x: int(x * 6)},
        ],
    }

    document = [2, 1.5]

    v = Validator(schema)
    assert v.validate(document) is True
    assert v.errors == {}
    assert v.normalized(document) == [4, 9]
    assert v.normalized_as_dict(document) == {0: 4, 1: 9}
def test_coerce_rules_work_in_list():

    schema = {
        "list_of_values": {
            "type": "list",
            "items": [
                {"type": "string", "coerce": str},
                {"type": "integer", "coerce": int},
            ],
        }
    }

    document = {"list_of_values": [123, "987"]}

    v = Validator()
    assert v.validate(document, schema) is True
    assert v.errors == {}
    assert v.normalized_as_dict(document, schema) == {
        "list_of_values": {0: "123", 1: 987}
    }
예제 #10
0
def test_extended_list_validation_works():
    document = [
        ["test1", 501, ["test2", 1, 2, 55]],
        600,
        ["test3", 1, 2, {
            "field1": "testfield",
            "field2": 5
        }],
        ["test10", 600, "test11"],
    ]
    schema = get_extended_list_schema()
    v = Validator(schema)
    assert v.normalized_as_dict(document) == {
        0: {
            0: "test1",
            1: 501,
            2: {
                0: "test2",
                "second_int": 1,
                2: 2,
                3: 55
            }
        },
        1: 600,
        2: {
            0: "test3",
            1: 1,
            "hi": 2,
            3: {
                "field01231": "testfield",
                "field2": 5
            }
        },
        "hello": {
            0: "test10",
            1: 600,
            2: "test11"
        },
    }
    assert v.errors == {}
예제 #11
0
def test_conflicting_dict_name_fails():
    document = {
        "list_of_values": [
            "test1",
            600,
            ["test3", 1, 2, {
                "field1": "testfield",
                "field2": 5
            }],
        ],
        "list_of_values_2": {
            "1": "test1",
            "2": 501,
            "3": ["test2", 1, 2, 55]
        },
        "list_of_values_3": ["test10", 600, "test11"],
    }
    schema = get_extended_dict_schema()

    dict_schema = deepcopy(schema)
    dict_schema["list_of_values"]["name"] = "hello"

    v = Validator(dict_schema)
    with pytest.raises(AttributeError):
        v.normalized_as_dict(document)

    try:
        assert v.normalized_as_dict(document, allow_name_conflicts=True) == {
            "list_of_values_2": {
                "1": "test1",
                "2": 501,
                "3": {
                    0: "test2",
                    "second_int": 1,
                    2: 2,
                    3: 55
                },
            },
            "hello": {
                0: "test10",
                1: 600,
                2: "test11"
            },
        }
    except AssertionError:
        if sys.version_info[0] == 3 and sys.version_info[1] < 6:
            # Workaround for earlier versions of python 3 not having sortable dicts
            assert v.normalized_as_dict(document,
                                        allow_name_conflicts=True) == {
                                            "list_of_values_2": {
                                                "1": "test1",
                                                "2": 501,
                                                "3": {
                                                    0: "test2",
                                                    "second_int": 1,
                                                    2: 2,
                                                    3: 55
                                                },
                                            },
                                            "hello": {
                                                0: "test1",
                                                1: 600,
                                                2: {
                                                    0: "test3",
                                                    1: 1,
                                                    "hi": 2,
                                                    3: {
                                                        "field01231":
                                                        "testfield",
                                                        "field2": 5
                                                    },
                                                },
                                            },
                                        }
        else:
            raise

    assert v.errors == {}
예제 #12
0
def test_multiuse_validation_works():
    document = {
        "list_of_values": [
            "rawr",
            1000,
            [
                "sfsdf", 1, 4, {
                    "field1": "dasdsa",
                    "field2": 3,
                    "field3": "sfd"
                }
            ],
        ],
        "list_of_values_2": ["fd", 1000, ["sfsdf", 1, 5434, 4, 4]],
    }
    schema = get_multiuse_schema()

    v = Validator(schema, allow_list_missing=True)

    for i in range(3):
        # -------------------------------------------------------------------------------------------------------
        expected_value = {
            "list_of_values": [{
                2: [{
                    3: [{
                        "field3": ["unknown field"]
                    }]
                }]
            }],
            "list_of_values_2": [{
                2: [{
                    3: ["min value is 50"],
                    4: ["unknown field"]
                }]
            }],
        }
        assert v.validate(document) is False
        assert v.errors == expected_value
        assert v.validate(document, schema) is False
        assert v.errors == expected_value

        # -------------------------------------------------------------------------------------------------------
        expected_value = {
            "list_of_values": [
                "rawr",
                1000,
                [
                    "sfsdf", 1, 4, {
                        "field1": "dasdsa",
                        "field2": 3,
                        "field3": "sfd"
                    }
                ],
            ],
            "list_of_values_2": ["fd", 1000, ["sfsdf", 1, 5434, 4, 4]],
        }
        assert v.normalized(document) == expected_value
        assert v.normalized(document, schema) == expected_value

        # -------------------------------------------------------------------------------------------------------
        expected_value = {
            "hello": {
                0: "rawr",
                1: 1000,
                2: {
                    0: "sfsdf",
                    1: 1,
                    "hi": 4,
                    3: {
                        "field01231": "dasdsa",
                        "field2": 3,
                        "field3": "sfd"
                    },
                },
            },
            "list_of_values_2": {
                0: "fd",
                1: 1000,
                2: {
                    0: "sfsdf",
                    "nd_int": 1,
                    2: 5434,
                    3: 4,
                    4: 4
                },
            },
        }
        assert v.normalized_as_dict(document) == expected_value
        assert v.normalized_as_dict(document, schema) == expected_value

        a = v.normalized_as_object(document)
        b = v.normalized_as_object(document, schema)

        assert a.hello[2].hi == 4
        assert b.hello[2].hi == 4

        assert a.list_of_values_2[2].nd_int == 1
        assert a.list_of_values_2[2].nd_int == 1

        assert a.list_of_values_2[2][2] == 5434
        assert a.list_of_values_2[2][2] == 5434