def test_float(self): res = t.ToFloat().check(1.0) assert res == 1.0 res = extract_error(t.ToFloat(), 1 + 3j) assert res == 'value is not float' res = extract_error(t.ToFloat(), 1) assert res == 1.0 res = t.ToFloat(gte=2).check(3.0) assert res == 3.0 res = extract_error(t.ToFloat(gte=2), 1.0) assert res == 'value is less than 2' res = t.ToFloat(lte=10).check(5.0) assert res == 5.0 res = extract_error(t.ToFloat(lte=3), 5.0) assert res == 'value is greater than 3' res = t.ToFloat().check("5.0") assert res == 5.0
def test_float_repr(self): res = t.ToFloat(gte=1) assert repr(res) == '<ToFloat(gte=1)>' res = t.ToFloat(lte=10) assert repr(res) == '<ToFloat(lte=10)>' res = t.ToFloat(gte=1, lte=10) assert repr(res) == '<ToFloat(gte=1, lte=10)>' res = t.ToFloat() > 10 assert repr(res) == '<ToFloat(gt=10)>' res = t.ToFloat() >= 10 assert repr(res) == '<ToFloat(gte=10)>' res = t.ToFloat() < 10 assert repr(res) == '<ToFloat(lt=10)>' res = t.ToFloat() <= 10 assert repr(res) == '<ToFloat(lte=10)>'
def construct(arg): ''' Shortcut syntax to define trafarets. - int, str, float and bool will return t.Int, t.String, t.Float and t.Bool - one element list will return t.List - tuple or list with several args will return t.Tuple - dict will return t.Dict. If key has '?' at the and it will be optional and '?' will be removed - any callable will be t.Call - otherwise it will be returned as is construct is recursive and will try construct all lists, tuples and dicts args ''' if isinstance(arg, t.Trafaret): return arg elif isinstance(arg, tuple) or (isinstance(arg, list) and len(arg) > 1): return t.Tuple(*(construct(a) for a in arg)) elif isinstance(arg, list): # if len(arg) == 1 return t.List(construct(arg[0])) elif isinstance(arg, dict): return t.Dict({ construct_key(key): construct(value) for key, value in arg.items() }) elif isinstance(arg, str): return t.Atom(arg) elif isinstance(arg, type): if arg is int: return t.ToInt() elif arg is float: return t.ToFloat() elif arg is str: return t.String() elif arg is bool: return t.Bool() else: return t.Type(arg) elif callable(arg): return t.Call(arg) else: return arg
def build_trafaret(sa_type, **kwargs): if isinstance(sa_type, sa.sql.sqltypes.Enum): trafaret = t.Enum(*sa_type.enums, **kwargs) # check for Text should be before String elif isinstance(sa_type, sa.sql.sqltypes.Text): trafaret = t.String(**kwargs) elif isinstance(sa_type, sa.sql.sqltypes.String): trafaret = t.String(max_length=sa_type.length, **kwargs) elif isinstance(sa_type, sa.sql.sqltypes.Integer): trafaret = t.ToInt(**kwargs) elif isinstance(sa_type, sa.sql.sqltypes.Float): trafaret = t.ToFloat(**kwargs) elif isinstance(sa_type, sa.sql.sqltypes.DateTime): trafaret = DateTime(**kwargs) # RFC3339 elif isinstance(sa_type, sa.sql.sqltypes.Date): trafaret = DateTime(**kwargs) # RFC3339 elif isinstance(sa_type, sa.sql.sqltypes.Boolean): trafaret = t.ToBool(**kwargs) # Add PG related JSON and ARRAY elif isinstance(sa_type, postgresql.JSON): trafaret = AnyDict | t.List(AnyDict) # Add PG related JSON and ARRAY elif isinstance(sa_type, postgresql.ARRAY): item_trafaret = build_trafaret(sa_type.item_type) trafaret = t.List(item_trafaret) else: type_ = str(sa_type) msg = 'Validator for type {} not implemented'.format(type_) raise NotImplementedError(msg) return trafaret
def test_float_meta_repr(self): assert repr(t.ToFloat() > 10) == '<ToFloat(gt=10)>' assert repr(t.ToFloat() >= 10) == '<ToFloat(gte=10)>' assert repr(t.ToFloat() < 10) == '<ToFloat(lt=10)>' assert repr(t.ToFloat() <= 10) == '<ToFloat(lte=10)>'
def to_number(value): return _check_return(value, t.ToFloat())