def test_implements_to_string(self):
        @compat.implements_to_string
        class X(object):
            def __str__(self):
                return u'Pändõra'

        assert compat.text_type(X()) == u'Pändõra'
 def String(min_len=1, max_len=150):
     return Test([
         lambda v: text_type(v),
         validate(lambda v: len(v) >= min_len,
                  error='Not long enough.'),
         validate(lambda v: len(v) <= max_len,
                  error='Too long.'),
     ])
    def test_basics(self):
        assert compat.text_type(u'Pändõra') == u'Pändõra'
        assert type('text') in compat.string_types

        string_io = compat.StringIO()
        string_io.write('test')
        assert string_io.getvalue() == 'test'

        mapping = {'a': 12, 'b': 16}
        result = {r for r in compat.iteritems(mapping)}
        assert result == {('a', 12), ('b', 16)}
    def test_basics(self):
        test = Test([
            lambda v: text_type(v),
            validate(lambda v: len(v) >= 1, error='Not long enough.'),
            validate(lambda v: len(v) <= 10, error='Too long.'),
        ])

        assert test('Nils') == u'Nils'

        with raises(ValueError) as exc:
            test('')

        assert str(exc.value) == 'Not long enough.'
Beispiel #5
0
def String(min_len=1, max_len=150,
           min_error='Too short.', max_error='Too long.',
           strip_html=True):
    commands = list()
    commands.append(lambda v: text_type(v) if v is not None else '')

    if min_len is not None:
        commands.append(validate(lambda v: len(v) >= min_len, min_error))
    if max_len is not None:
        commands.append(validate(lambda v: len(v) <= max_len, max_error))

    if strip_html:
        commands.append(lambda v: bleach.clean(v, tags=list(), strip=True))

    return Test(commands)
Beispiel #6
0
 def json_default(self, value):
     if type(value) is Undefined:
         return None
     elif isinstance(value, (dt.datetime, dt.date)):
         return value.isoformat()
     elif isinstance(value, Decimal):
         return str(value)
     elif isinstance(value, FileStorage):
         return value.filename
     elif hasattr(value, 'to_dict'):
         return value.to_dict()
     elif hasattr(value, '__iter__'):
         return [v for v in value]
     elif type(value).__str__ is not object.__str__:
         return text_type(value)
     else:
         return repr(value)
Beispiel #7
0
def Boolean(false_values=None, true_values=None, true_unless_false=False,
            none_values=None, error='Not a boolean value.'):
    if false_values is None:
        false_values = BOOL_FIELD_FALSE_VALUES
    if true_values is None:
        true_values = BOOL_FIELD_TRUE_VALUES

    commands = list()

    if none_values is not None:
        commands.append(test_and_return(lambda v: v in none_values, None))

    if true_unless_false:
        commands.append(lambda v: v not in false_values)
    else:
        commands.append(test_and_return(lambda v: v in false_values, False))
        commands.append(test_and_return(lambda v: v in true_values, True))

    return Test([lambda v: text_type(v).lower(),
                 Test(commands, mode=Test.OR, error_or=error)])