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)
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)
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
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"}
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")
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")
def test_omit(): s = Class("foo", "docs", Field("foo", String(), default=OMIT)) assert s.load("test", "{}") == {}
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)