Ejemplo n.º 1
0
def test_missing_field():
    s = Class("foo", Klass, Field("foo", String()), Field("bar", String()))

    obj = {"bar": "asdf"}
    try:
        s.load("test", yaml.dump(obj))
    except SchemaError, e:
        assert "required field 'foo' is missing" in str(e)
Ejemplo n.º 2
0
def test_union():
    s = Union(
        String(), Sequence(String()),
        Class("type-a", lambda **kw: Klass(a=True, **kw),
              Field("type", Constant("a"))),
        Class("type-b", lambda **kw: Klass(b=True, **kw),
              Field("type", Constant("b"))),
        Class("foo", Klass, Field("foo", String())),
        Class("foobar", Klass, Field("foobar", String())), Map(String()))
    assert s.load("test", "asdf") == "asdf"
    assert s.load("test", "[a, b, c]") == ["a", "b", "c"]
    assert s.load("test", "type: a") == Klass(type="a", a=True)
    assert s.load("test", "type: b") == Klass(type="b", b=True)
    assert s.load("test", "foo: bar") == Klass(foo="bar")
    assert s.load("test", "foobar: bar") == Klass(foobar="bar")
    assert s.load("test", "bar: foo") == {"bar": "foo"}
Ejemplo n.º 3
0
def test_scalars():
    s = Class("scalars", Klass, Field("string", String()),
              Field("integer", Integer()), Field("float", Float()))

    obj = {"string": "asdf", "integer": 3, "float": 3.14159}
    k1 = s.load("test", yaml.dump(obj))
    k2 = Klass(**obj)
    assert k1 == k2
Ejemplo n.º 4
0
def test_unknown_field():
    s = Class("foo", Klass, Field("foo", String()))

    obj = {"foo": "asdf", "bar": "fdsa"}
    try:
        s.load("test", yaml.dump(obj))
        assert False, "should have errored"
    except SchemaError, e:
        assert "no such field: bar" in str(e)
Ejemplo n.º 5
0
def test_map():
    s = Map(String())
    assert s.load("test", "{a: b, c: d}") == {"a": "b", "c": "d"}
Ejemplo n.º 6
0
def test_sequence():
    s = Sequence(String())
    assert s.load("test", "[a, b, c]") == ["a", "b", "c"]
Ejemplo n.º 7
0
def test_alias():
    s = Class("foobar", Klass, Field("foo-bar", String(), "foo_bar"))
    k = s.load("test", "{foo-bar: foobar}")
    assert k.fields == {"foo_bar": "foobar"}
Ejemplo n.º 8
0
def test_default_field():
    s = Class("foo", Klass, Field("foo", String(), default=None),
              Field("bar", String(), default="asdf"))

    k = s.load("test", "{}")
    assert k == Klass(foo=None, bar="asdf")
Ejemplo n.º 9
0
def test_null():
    for s in (String(), Integer(), Float(), Constant("asdf")):
        assert s.load("test", "null") == None
Ejemplo n.º 10
0
def test_lax():
    s = Class("foo", "docs", Field("foo", String()), strict=False)
    assert s.load("test", "{foo: bar, baz: moo}") == OrderedDict(foo="bar",
                                                                 baz="moo")
Ejemplo n.º 11
0
def test_omit():
    s = Class("foo", "docs", Field("foo", String(), default=OMIT))
    assert s.load("test", "{}") == {}
Ejemplo n.º 12
0
def test_generic_class():
    s = Class("foo", "docs", Field("foo", String(), default=OMIT))
    obj = s.load("test", "foo: bar")
    assert obj == OrderedDict(foo="bar")
    assert isinstance(obj, OrderedDict)
Ejemplo n.º 13
0
    assert isinstance(obj, OrderedDict)


def test_omit():
    s = Class("foo", "docs", Field("foo", String(), default=OMIT))
    assert s.load("test", "{}") == {}


def test_lax():
    s = Class("foo", "docs", Field("foo", String()), strict=False)
    assert s.load("test", "{foo: bar, baz: moo}") == OrderedDict(foo="bar",
                                                                 baz="moo")


AMBIGUOUS_UNIONS = (
    (lambda: Union(String(), String()),
     "ambiguous union: string appears multiple times"),
    (lambda: Union(Class("a", "a docs", Field("type", Constant("a"))),
                   Class("b", "b docs", Field("type", Constant("a")))),
     "ambiguous union: a:map{type=a}, b:map{type=a}"),
    (lambda: Union(Class("a", "a docs", Field("type", Constant("a"))),
                   Class("b", "b docs", Field("type", String()))),
     "ambiguous union: a:map{type=a}, b:map"),
    (lambda: Union(
        Class("a", "a docs", Field("type", Constant("a"))),
        Class("b", "b docs", Field("c", Constant("x")), Field(
            "type", String()))),
     "ambiguous union: 'type' both constant and unconstrained"),
    (lambda: Union(Class("a", "a docs", Field("type", Constant("a"))),
                   Map(Any())), "ambiguous union: map and a:map{type=a}"))