Esempio n. 1
0
def test_nested_nested():

    arg = dict(c=3, n2=dict(b=2, n1=dict(a=1)))
    o = ExampleNestedNested(arg)
    assert o.c == 3
    assert o.n2.b == 2
    assert o.n2.n1.a == 1

    arg = dict(c=3, n2=dict(b=2, n1=dict(z=3)))
    errs = (
        exceptions.UnexpectedKeyError(
            "z",
            ExampleSingle,
            path=(
                "n2",
                "n1",
            ),
        ),
        exceptions.MissingValueError(
            "a",
            ExampleSingle,
            path=(
                "n2",
                "n1",
            ),
        ),
    )

    o = ExampleNestedNested(arg)
    assert o.get_errors() == errs
Esempio n. 2
0
def test_nested():

    arg = dict(b=1, n1=dict(a=2))
    o = ExampleNested(arg)
    assert o.b == 1
    assert o.n1.a == 2

    arg = dict(b=1, n1=dict(z=3))
    errs = (
        exceptions.UnexpectedKeyError("z", ExampleSingle, path=("n1", )),
        exceptions.MissingValueError("a", ExampleSingle, path=("n1", )),
    )

    o = ExampleNested(arg)
    assert o.get_errors() == errs
Esempio n. 3
0
def test_missing():

    o = Example(dict(a=1, c=2.0, d=True))
    assert o.a == 1
    assert o.c == 2.0
    assert o.d is True

    o = Example(dict(a=1, c=2.0, d=True))
    errs = o.get_errors()
    assert isinstance(errs, tuple)
    assert len(errs) == 1
    e = errs[0]
    assert e == exceptions.MissingValueError("b", Example)

    errs = o.get_errors(err_on_missing=False)
    assert isinstance(errs, tuple)
    assert len(errs) == 0
Esempio n. 4
0
def test_dict_int():

    arg = dict(a=1, n=dict(k=dict(a=2)))
    o = ExampleNestedDict(arg)
    assert o.a == 1
    assert o.n["k"].a == 2
    assert not o.get_errors()

    arg = dict(a=1, n=dict(k=dict(c=3)))
    o = ExampleNestedDict(arg)
    errs = (
        exceptions.UnexpectedKeyError(
            "c", ExampleSingle).with_index("k").with_parent("n"),
        exceptions.MissingValueError(
            "a", ExampleSingle).with_index("k").with_parent("n"),
    )
    assert o.get_errors() == errs
Esempio n. 5
0
def test_tuple_int():

    arg = dict(a=1, n=(dict(a=2), ))
    o = ExampleNestedTuple.from_dict(arg)
    assert o.a == 1
    assert o.n[0].a == 2
    assert not o.get_errors()

    arg = dict(a=1, n=(dict(c=3), ))
    o = ExampleNestedTuple(arg)
    errs = (
        exceptions.UnexpectedKeyError(
            "c", ExampleSingle).with_index(0).with_parent("n"),
        exceptions.MissingValueError(
            "a", ExampleSingle).with_index(0).with_parent("n"),
    )
    assert o.get_errors() == errs
Esempio n. 6
0
def test_dict_int_with_defk():

    with pytest.raises(ValueError):
        arg = dict(a=1, n=tuple(dict(a=2)))
        o = ExampleNestedTupleWithDefK(arg)

    arg = dict(a=1, n=dict(k=dict(a=2)))
    o = ExampleNestedDictWithDefK(arg)
    assert o.a == 1
    assert o.n["k"].a == 2
    assert o.n["k"].b == "k"
    assert not o.get_errors()

    arg = dict(a=1, n=dict(k=dict(c=3)))
    o = ExampleNestedDict(arg)
    errs = (
        exceptions.UnexpectedKeyError(
            "c", ExampleSingle).with_index("k").with_parent("n"),
        exceptions.MissingValueError(
            "a", ExampleSingle).with_index("k").with_parent("n"),
    )
    assert o.get_errors() == errs