Exemple #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)
Exemple #2
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)
Exemple #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
Exemple #4
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"}
Exemple #5
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")
Exemple #6
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")
Exemple #7
0
def test_omit():
    s = Class("foo", "docs", Field("foo", String(), default=OMIT))
    assert s.load("test", "{}") == {}
Exemple #8
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)