def test_type(): t = Type() assert t == Type() assert t != 1 assert is_hashable(t) assert_raises(NotImplementedError, t.check, 1) assert_raises(NotImplementedError, t.coerce, 1) assert_raises(NotImplementedError, t.display) assert_raises(NotImplementedError, t.enumeration_value, 1) assert_raises(NotImplementedError, t.generate) assert_raises(NotImplementedError, t.rst) assert_raises(NotImplementedError, t.validate, 1)
def test_dispatch_type(): t = Type.dispatch(None) assert isinstance(t, AnyType) t = Type.dispatch(int) assert isinstance(t, TypeType) assert t.type is int t = Type.dispatch((int, float)) assert isinstance(t, MultiType) assert t.typelist == (int, float) t = Type.dispatch([1, 2]) assert isinstance(t, ValuesType) assert t.values == [1, 2] t = Type.dispatch(six.string_types) assert isinstance(t, TypeType) t.validate('abc') t.validate(u'abc') assert_raises(TypeError, t.validate, 1) te = TypeExtension() assert Type.dispatch(te) is te assert Type.dispatch(TypeExtension) is not TypeExtension assert isinstance(Type.dispatch(TypeExtension), TypeExtension) assert_raises(TypeError, Type.dispatch, 1) assert_raises(TypeError, Type.dispatch, b'abc') assert_raises(TypeError, Type.dispatch, u'abc')
def test_schema(): from syn.schema.b.sequence import Sequence from syn.type.a import List t = Schema(Sequence(1, 2, 3)) assert t == Schema(Sequence(1, 2, 3)) assert t != Schema(Sequence(1, 3, 2)) assert Type.dispatch(t) is t assert t.query([1, 2, 3]) assert not t.query([1, 3, 2]) t.validate([1, 2, 3]) assert_raises(TypeError, t.validate, [1, 3, 2]) assert t.generate() == [1, 2, 3] assert t.display() == t.rst() == '<Schema>' assert t.coerce(1) == 1 t = Schema(Sequence(int, float)) assert t.query([1, 2.3]) assert not t.query([1, 2]) val = t.generate() assert t.query(val) t = Schema(Sequence(int, List(float))) assert not t.query([1, 1.2]) assert not t.query([1, [1, 2]]) assert t.query([1, [1.2, 3.4]]) assert t.query([1, []]) val = t.generate() assert t.query(val)
def __init__(self, typ=None, default=None, doc='', optional=False, init=None): self.type = Type.dispatch(typ) self.default = default self.doc = doc self.optional = optional self.init = init
def test_coerce_classmethod(): t1 = Type.dispatch(CT1) assert t1.coerce(1) == CT1(1) assert t1.coerce({'a': 1}) == CT1(1) assert t1.coerce({'a': 1.2}) == CT1(1) assert_raises(TypeError, t1.coerce, [1, 2]) t2 = Type.dispatch(CT2) assert t2.coerce(dict(a=1, b=2)) == CT2(a=1, b=CT1(2)) assert t2.coerce(dict(a=1, b=CT1(2))) == CT2(a=1, b=CT1(2)) assert_raises(TypeError, t2.coerce, dict(a=1, b=2.3)) assert t2.coerce(dict(a=1, b=dict(a=2.3))) == CT2(a=1, b=CT1(2)) t3 = Type.dispatch(CT3) assert t3.coerce(dict(a=1, b=dict(a=2, b=3), c=4)) == \ CT3(a=1, b=CT2(a=2, b=CT1(3)), c=CT1(4)) assert t3.coerce(dict(a=1, b=dict(a=2, b=CT1(3)), c=4)) == \ CT3(a=1, b=CT2(a=2, b=CT1(3)), c=CT1(4)) assert_raises(TypeError, t3.coerce, dict(a=1, b=dict(a=2, b=3.1), c=4)) assert t3.coerce(dict(a=1, b=dict(a=2, b=dict(a=3.1)), c=4)) == \ CT3(a=1, b=CT2(a=2, b=CT1(3)), c=CT1(4)) obj = t3.coerce(dict(a=1, b=dict(a=2, b=3), c=4)) check_idempotence(obj) t4 = Type.dispatch(CT4) assert t4.coerce(dict(a = 1)) == CT4(a = 2) t5 = Type.dispatch(CT5) assert t5.coerce(dict(a = 1)) == CT5(a = 4) t6 = Type.dispatch(CT6) assert t6.coerce(dict(a = 1)) == CT6(a = 5)
def test_coerce_classmethod(): t1 = Type.dispatch(CT1) assert t1.coerce(1) == CT1(1) assert t1.coerce({'a': 1}) == CT1(1) assert t1.coerce({'a': 1.2}) == CT1(1) assert_raises(TypeError, t1.coerce, [1, 2]) t2 = Type.dispatch(CT2) assert t2.coerce(dict(a=1, b=2)) == CT2(a=1, b=CT1(2)) assert t2.coerce(dict(a=1, b=CT1(2))) == CT2(a=1, b=CT1(2)) assert_raises(TypeError, t2.coerce, dict(a=1, b=2.3)) assert t2.coerce(dict(a=1, b=dict(a=2.3))) == CT2(a=1, b=CT1(2)) t3 = Type.dispatch(CT3) assert t3.coerce(dict(a=1, b=dict(a=2, b=3), c=4)) == \ CT3(a=1, b=CT2(a=2, b=CT1(3)), c=CT1(4)) assert t3.coerce(dict(a=1, b=dict(a=2, b=CT1(3)), c=4)) == \ CT3(a=1, b=CT2(a=2, b=CT1(3)), c=CT1(4)) assert_raises(TypeError, t3.coerce, dict(a=1, b=dict(a=2, b=3.1), c=4)) assert t3.coerce(dict(a=1, b=dict(a=2, b=dict(a=3.1)), c=4)) == \ CT3(a=1, b=CT2(a=2, b=CT1(3)), c=CT1(4)) obj = t3.coerce(dict(a=1, b=dict(a=2, b=3), c=4)) check_idempotence(obj) t4 = Type.dispatch(CT4) assert t4.coerce(dict(a=1)) == CT4(a=2) t5 = Type.dispatch(CT5) assert t5.coerce(dict(a=1)) == CT5(a=4) t6 = Type.dispatch(CT6) assert t6.coerce(dict(a=1)) == CT6(a=5)
def test_set(): from syn.sets.b import Range t = Set(Range(1, 5)) assert t == Set(Range(1, 5)) assert t != Set(Range(0, 5)) assert Type.dispatch(t) is t assert t.query(1) assert not t.query(0) t.validate(1) assert_raises(TypeError, t.validate, 0) assert t.coerce(1) == 1 assert_raises(TypeError, t.coerce, 0) s = set(xrange(1, 6)) for k in xrange(SAMPLES): val = t.generate() with on_error(elog, s.__contains__, (val,)): assert val in s assert t.display() == t.rst() == '<Set>'
def test_set(): from syn.sets.b import Range t = Set(Range(1, 5)) assert t == Set(Range(1, 5)) assert t != Set(Range(0, 5)) assert Type.dispatch(t) is t assert t.query(1) assert not t.query(0) t.validate(1) assert_raises(TypeError, t.validate, 0) assert t.coerce(1) == 1 assert_raises(TypeError, t.coerce, 0) s = set(xrange(1, 6)) for k in xrange(SAMPLES): val = t.generate() with on_error(elog, s.__contains__, (val, )): assert val in s assert t.display() == t.rst() == '<Set>'
def _resolve_this(self): attrs = getattr(self, '_attrs', {}).values() for attr in attrs: if isinstance(attr.type, This): attr.type = Type.dispatch(self)
def _dispatch_type(self): if not isinstance(self.type, Type_): self.type = Type_.dispatch(self.type)
def __init__(self, *args, **kwargs): super(Attr, self).__init__(*args, **kwargs) self.type = Type.dispatch(self.type) self.validate()