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()

    v = Validator(schema)
    o = v.normalized_as_object(document, callable_numbers=False)
    p = v.normalized_as_object(document, callable_numbers=True)
    assert o.list_of_values_2["3"].second_int == 1
    assert p.list_of_values_2._3.second_int == 1
    assert o.first_val[2][3].field01231 == "testfield"
    assert p.first_val._2._3.field01231 == "testfield"
    assert o.first_val[2][3].field2 == 5
    assert p.first_val._2._3.field2 == 5
    assert o.hello[1] == 600
    assert p.hello._1 == 600
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_simple_list_validation_works():
    document = ["test1", 600, "test2"]
    schema = get_simple_list_schema()

    v = Validator(schema)
    o = v.normalized_as_object(document, callable_numbers=False)
    p = v.normalized_as_object(document, callable_numbers=True)
    assert o[0] == "test1"
    assert o[1] == 600
    assert o[2] == "test2"
    assert p._0 == "test1"
    assert p._1 == 600
    assert p._2 == "test2"
def test_simple_dict_validation_works():
    document = {"list_of_values": ["test1", 600, "test2"]}
    schema = get_simple_dict_schema()

    v = Validator(schema)
    o = v.normalized_as_object(document, callable_numbers=False)
    p = v.normalized_as_object(document, callable_numbers=True)
    assert o.hello[0] == "test1"
    assert o.hello[1] == 600
    assert o.hello[2] == "test2"
    assert o["hello"][0] == "test1"
    assert o["hello"][1] == 600
    assert o["hello"][2] == "test2"
    assert p.hello._0 == "test1"
    assert p.hello._1 == 600
    assert p.hello._2 == "test2"
    assert p["hello"]["_0"] == "test1"
    assert p["hello"]["_1"] == 600
    assert p["hello"]["_2"] == "test2"
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)
    o = v.normalized_as_object(document, callable_numbers=False)
    p = v.normalized_as_object(document, callable_numbers=True)
    assert o[0][2].second_int == 1
    assert p._0._2.second_int == 1
    assert o[2][3].field01231 == "testfield"
    assert p._2._3.field01231 == "testfield"
    assert o[2][3].field2 == 5
    assert p._2._3.field2 == 5
    assert o.hello[1] == 600
    assert p.hello._1 == 600
Ejemplo n.º 6
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