def test_to_python():
class Foo(jst.JSONHasTraits):
_required_traits = ['a', 'b']
a = jst.JSONNumber()
b = jst.JSONString()
class Bar(jst.JSONHasTraits):
c = jst.JSONArray(jst.JSONNumber())
d = jst.JSONInstance(Foo)
e = jst.JSONArray(jst.JSONInstance(Foo))
D = {'c': [1, 2, 3], 'd': {'a': 5, 'b': 'blah'},
'e': [{'a': 3, 'b': 'foo'}, {'a': 4, 'b': 'bar'}]}
obj = Bar.from_dict(D)
obj2 = eval(obj.to_python())
assert obj2.to_dict() == obj.to_dict() == D
# Make sure there is an error if required traits are missing
foo = Foo(a=4)
with pytest.raises(T.TraitError):
foo.to_python()
评论列表
文章目录