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_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_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_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"}
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)
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}")) @pytest.mark.parametrize("input,error", AMBIGUOUS_UNIONS)