Exemplo n.º 1
0
 def test_fieldblock_readonly(self):
     class _Form(Form):
         fields=[
             FieldBlock('field block',
                        fields=[
                                 Field('number',
                                       convs.Int()),
                                 Field('title',
                                       convs.Char()),
                               ],
                        permissions='r'),
         ]
     env = AppEnvironment.create()
     form = _Form(env)
     self.assertEqual(form.raw_data, MultiDict([('number', ''),
                                                ('title', '')]))
     self.assertEqual(form.python_data, {'number': None,
                                         'title': None})
     self.assert_(form.accept({'number': '4', 'title': 'Hello'}),
                  form.errors)
     self.assertEqual(form.python_data, {'number': None,
                                         'title': None})
     self.assertEqual(form.get_field('number').raw_value, '')
     self.assertEqual(form.get_field('title').raw_value, '')
     self.assertEqual(form.errors, {})
Exemplo n.º 2
0
    def test_by_field_fieldset(self):
        class f(Form):
            fields = [FieldSet('name', fields=[Field('subfield')])]

        env = AppEnvironment.create()
        form = f(env)

        ve = ValidationError(
            by_field={
                '.subfield': u'Ошибка-1',
                'field': u'Ошибка-3',
                # XXX what does this mean?
                '': u'Ошибка-4'
            })

        ve.fill_errors(form.get_field('name'))
        self.assertEqual(form.errors, {
            'name.subfield': u'Ошибка-1',
            'field': u'Ошибка-3',
            '': u'Ошибка-4'
        })

        ve = ValidationError(by_field={'.absent': u'1'})
        self.assertRaises(
            AttributeError,  # XXX is AttributeError good?
            lambda: ve.fill_errors(form.get_field('name')))
Exemplo n.º 3
0
    def test_fieldlist_with_initial_delete(self):
        'Fieldlist element deletion'

        class _Form(Form):
            fields = [
                FieldList('list', field=Field('number', convs.Int())),
            ]

        env = AppEnvironment.create()
        form = _Form(env, initial={'list': [1, 2, 3]})
        self.assertEqual(
            form.raw_data,
            MultiDict((('list-indices', '1'), ('list-indices', '2'),
                       ('list-indices', '3')), **{
                           'list.1': '1',
                           'list.2': '2',
                           'list.3': '3'
                       }))
        self.assertEqual(form.python_data, {'list': [1, 2, 3]})
        self.assert_(
            form.accept(
                MultiDict((('list-indices', '1'), ('list-indices', '3')), **{
                    'list.1': '1',
                    'list.3': '3'
                })))
        self.assertEqual(form.python_data, {'list': [1, 3]})
Exemplo n.º 4
0
    def test_get_field(self):
        class F(Form):
            fields = [
                FieldSet('fieldset', fields=[
                    FieldList('fieldlist',
                        field=FieldSet(None, fields=[
                            Field('subfield')
                        ])
                    )
                ]),
                FieldBlock('field block', [
                    Field('blocksubfield')
                ], name='block')
            ]

        env = AppEnvironment.create()
        form = F(env)
        for nm, cls in [('fieldset', FieldSet),
                        ('fieldset.fieldlist', FieldList),
                        ('fieldset.fieldlist.1', FieldSet),
                        ('fieldset.fieldlist.1.subfield', Field),
                        ('block', FieldBlock),
                        ('blocksubfield', Field),
                        ]:
            self.assert_(isinstance(form.get_field(nm), cls),
                         '%s is not instance of %s' % (nm, cls))
            self.assertEqual(form.get_field(nm).input_name, nm)

        nm, cls = ('block.blocksubfield', Field)
        self.assert_(isinstance(form.get_field(nm), cls),
                     '%s is not instance of %s' % (nm, cls))
        self.assertEqual(form.get_field(nm).input_name,
                         'blocksubfield')
Exemplo n.º 5
0
    def test_get_field(self):
        class F(Form):
            fields = [
                FieldSet('fieldset',
                         fields=[
                             FieldList('fieldlist',
                                       field=FieldSet(
                                           None, fields=[Field('subfield')]))
                         ]),
                FieldBlock('field block', [Field('blocksubfield')],
                           name='block')
            ]

        env = AppEnvironment.create()
        form = F(env)
        for nm, cls in [
            ('fieldset', FieldSet),
            ('fieldset.fieldlist', FieldList),
            ('fieldset.fieldlist.1', FieldSet),
            ('fieldset.fieldlist.1.subfield', Field),
            ('block', FieldBlock),
            ('blocksubfield', Field),
        ]:
            self.assert_(isinstance(form.get_field(nm), cls),
                         '%s is not instance of %s' % (nm, cls))
            self.assertEqual(form.get_field(nm).input_name, nm)

        self.assertEqual(None, form.get_field('fieldset.fieldlist.asd'))

        nm, cls = ('block.blocksubfield', Field)
        self.assert_(isinstance(form.get_field(nm), cls),
                     '%s is not instance of %s' % (nm, cls))
        self.assertEqual(form.get_field(nm).input_name, 'blocksubfield')
Exemplo n.º 6
0
    def test_required(self):
        Form = self.get_form(required=True)

        env = AppEnvironment.create()
        form = Form(env)
        form.accept(MultiDict({'pass.pass': '', 'pass.conf': ''}))
        self.assertEqual(list(form.errors.keys()), ['pass'])
Exemplo n.º 7
0
    def test_clean_value(self):
        class AssertConv(convs.Int):
            def to_python(conv, value):
                value = convs.Int.to_python(conv, value)
                if value is not None:
                    field = conv.field.form.get_field('num')
                    self.assertEqual(field.clean_value, value)
                return value

        class F(Form):
            fields = [FieldBlock('', fields=[
                          Field('num',
                                conv=convs.Int()),
                          Field('f2',
                                conv=AssertConv())
                          ])]

        env = AppEnvironment.create()
        form = F(env)
        self.assertEqual(form.get_field('num').clean_value, None)

        form = F(env, initial={'num': 2})
        self.assertEqual(form.get_field('num').clean_value, 2)

        form = F(env)
        form.accept({'num': '4', 'f2': '4'})
        self.assertEqual(form.get_field('num').clean_value, 4)
        self.assertEqual(form.get_field('f2').clean_value, 4)
Exemplo n.º 8
0
    def test_clean_value(self):
        class AssertConv(convs.Int):
            def to_python(conv, value):
                value = convs.Int.to_python(conv, value)
                if value is not None:
                    field = conv.field.form.get_field('num')
                    self.assertEqual(field.clean_value, value)
                return value

        class F(Form):
            fields = [
                Field('top', conv=convs.Int(), initial=246),
                FieldBlock('',
                           fields=[
                               Field('num', conv=convs.Int()),
                               Field('f2', conv=AssertConv())
                           ])
            ]

        env = AppEnvironment.create()
        form = F(env)
        self.assertEqual(form.get_field('top').clean_value, 246)
        self.assertEqual(form.get_field('num').clean_value, None)

        form = F(env, initial={'num': 2})
        self.assertEqual(form.get_field('num').clean_value, 2)

        form = F(env)
        form.accept({'num': '4', 'f2': '4'})
        self.assertEqual(form.get_field('num').clean_value, 4)
        self.assertEqual(form.get_field('f2').clean_value, 4)
Exemplo n.º 9
0
    def test_get_initial(self):
        Form = self.get_form(required=True)

        env = AppEnvironment.create()
        form = Form(env)
        conv = form.get_field('pass').conv
        self.assertEqual(conv.get_initial(), '')
Exemplo n.º 10
0
    def test_get_initial(self):
        Form = self.get_form(required=True)

        env = AppEnvironment.create()
        form = Form(env)
        conv = form.get_field('pass').conv
        self.assertEqual(conv.get_initial(), '')
Exemplo n.º 11
0
 def test_id(self):
     class _Form(Form):
         fields=[Field('input', convs.Char())]
     env = AppEnvironment.create()
     form = _Form(env)
     self.assertEqual(form.get_field('input').id, 'input')
     form.id = 'myform'
     self.assertEqual(form.get_field('input').id, 'myform-input')
Exemplo n.º 12
0
    def test_required(self):
        Form = self.get_form(required=True)

        env = AppEnvironment.create()
        form = Form(env)
        form.accept(MultiDict({'pass.pass': '',
                               'pass.conf': ''}))
        self.assertEqual(form.errors.keys(), ['pass'])
Exemplo n.º 13
0
 def test_accept(self):
     'Method accept of bound field returns cleaned value'
     class _Form(Form):
         fields=[Field('input', convs.Char())]
     env = AppEnvironment.create()
     form = _Form(env)
     self.assert_(form.accept(MultiDict(input='value')))
     self.assertEqual(form.python_data['input'], 'value')
Exemplo n.º 14
0
    def test_mismatch(self):
        Form = self.get_form()
        env = AppEnvironment.create()
        form = Form(env)

        form.accept(MultiDict({'pass.pass': '******', 'pass.conf': '123'}))
        self.assertEqual(form.python_data, {'pass': None})
        self.assertEqual(list(form.errors.keys()), ['pass'])
Exemplo n.º 15
0
    def test_accept_multiple(self):
        class F(Form):
            fields = [Field('name', conv=convs.ListOf(convs.Int))]

        env = AppEnvironment.create()
        form = F(env)
        form.accept(MultiDict([('name', '1'), ('name', '2')]))
        self.assertEqual(form.python_data['name'], [1, 2])
Exemplo n.º 16
0
    def test_id(self):
        class _Form(Form):
            fields = [Field('input', convs.Char())]

        env = AppEnvironment.create()
        form = _Form(env)
        self.assertEqual(form.get_field('input').id, 'input')
        form.id = 'myform'
        self.assertEqual(form.get_field('input').id, 'myform-input')
Exemplo n.º 17
0
 def test_check_value_type(self):
     '''Pass file value to ordinary Field'''
     class F(Form):
         fields = [Field('inp')]
     request = Request.blank('/', POST=dict(inp=('foo.txt', 'ggg')))
     env = AppEnvironment.create()
     form = F(env)
     self.assertEqual(form.accept(request.POST), False)
     self.assertEqual(form.errors.keys(), ['inp'])
Exemplo n.º 18
0
    def test_accept(self):
        class _Form(Form):
            fields = [FileField('inp')]

        env = AppEnvironment.create()
        form = _Form(env)
        request = Request.blank('/', POST=dict(inp=('file.txt', 'ggg')))
        self.assert_(form.accept(request.POST), form.errors)
        self.assertEqual(form.python_data['inp'].file.read(), b'ggg')
Exemplo n.º 19
0
    def test_mismatch(self):
        Form = self.get_form()
        env = AppEnvironment.create()
        form = Form(env)

        form.accept(MultiDict({'pass.pass': '******',
                               'pass.conf': '123'}))
        self.assertEqual(form.python_data, {'pass': None})
        self.assertEqual(form.errors.keys(), ['pass'])
Exemplo n.º 20
0
 def test_mismatch(self):
     class _Form(Form):
         fields=[PasswordSet()]
     env = AppEnvironment.create()
     form = _Form(env)
     accept = form.accept({'password.pass':'******', 'password.conf':'second'})
     self.assertEqual(dict(password='******').items(),
                      form.errors.items())
     self.assertFalse(accept)
Exemplo n.º 21
0
 def setUp(self):
     self.transient_root = tempfile.mkdtemp()
     self.persistent_root = tempfile.mkdtemp()
     self.transient_url = "/transient/"
     self.persistent_url = "/media/"
     self.file_manager = FileManager(
         self.transient_root, self.persistent_root, self.transient_url, self.persistent_url
     )
     self.env = AppEnvironment.create(file_manager=self.file_manager)
Exemplo n.º 22
0
 def test_accept(self):
     class _Form(Form):
         fields=[FileField('inp')]
     env = AppEnvironment.create()
     form = _Form(env)
     request = Request.blank('/', POST=dict(inp=('file.txt', 'ggg')))
     self.assert_(form.accept(request.POST),
                  form.errors)
     self.assertEqual(form.python_data['inp'].file.read(), 'ggg')
Exemplo n.º 23
0
    def test_accept(self):
        'Method accept of bound field returns cleaned value'

        class _Form(Form):
            fields = [Field('input', convs.Char())]

        env = AppEnvironment.create()
        form = _Form(env)
        self.assert_(form.accept(MultiDict(input='value')))
        self.assertEqual(form.python_data['input'], 'value')
Exemplo n.º 24
0
 def setUp(self):
     self.transient_root = tempfile.mkdtemp()
     self.persistent_root = tempfile.mkdtemp()
     self.transient_url = '/transient/'
     self.persistent_url = '/media/'
     self.file_manager = FileManager(self.transient_root,
                                     self.persistent_root,
                                     self.transient_url,
                                     self.persistent_url)
     self.env = AppEnvironment.create(file_manager=self.file_manager)
Exemplo n.º 25
0
    def test_get_perms(self):
        class F(Form):
            fields = [Field("first", permissions="rw"), Field("second")]

        env = AppEnvironment.create()
        form = F(env, permissions="rwx")
        first = form.get_field("first")
        second = form.get_field("second")
        self.assertEqual(first.perm_getter.get_perms(first), set("rw"))
        self.assertEqual(second.perm_getter.get_perms(second), set("rwx"))
Exemplo n.º 26
0
    def test_from_python_multiple(self):
        class F(Form):
            fields = [
                Field('name', conv=convs.ListOf(convs.Int), initial=[1, 2])
            ]

        env = AppEnvironment.create()
        form = F(env)
        self.assertEqual(form.raw_data,
                         MultiDict([('name', '1'), ('name', '2')]))
Exemplo n.º 27
0
    def test_check_value_type(self):
        '''Pass file value to ordinary Field'''
        class F(Form):
            fields = [Field('inp')]

        request = Request.blank('/', POST=dict(inp=('foo.txt', 'ggg')))
        env = AppEnvironment.create()
        form = F(env)
        self.assertEqual(form.accept(request.POST), False)
        self.assertEqual(list(form.errors.keys()), ['inp'])
Exemplo n.º 28
0
 def test_prefix(self):
     class F(Form):
         fields=[
             Field('first', convs.Int()),
         ]
     env = AppEnvironment.create()
     form = F(env)
     self.assertEqual(form.get_field('first').input_name, 'first')
     form = F(env, name="pref")
     self.assertEqual(form.get_field('first').input_name, 'pref:first')
Exemplo n.º 29
0
    def test_accept_multiple(self):
        class F(Form):
            fields = [
                Field('name', conv=convs.ListOf(convs.Int))
            ]

        env = AppEnvironment.create()
        form = F(env)
        form.accept(MultiDict([('name', '1'), ('name', '2')]))
        self.assertEqual(form.python_data['name'], [1, 2])
Exemplo n.º 30
0
    def test_prefix(self):
        class F(Form):
            fields = [
                Field('first', convs.Int()),
            ]

        env = AppEnvironment.create()
        form = F(env)
        self.assertEqual(form.get_field('first').input_name, 'first')
        form = F(env, name="pref")
        self.assertEqual(form.get_field('first').input_name, 'pref:first')
Exemplo n.º 31
0
 def test_simple(self):
     'Accept with errors'
     class _Form(Form):
         fields=[
             Field('first', convs.Int(required=True)),
             Field('second', convs.Int(required=True)),
         ]
     env = AppEnvironment.create()
     form = _Form(env)
     self.assert_(not form.accept(MultiDict(first='1')))
     self.assertEqual(form.errors, {'second': convs.Converter.error_required})
Exemplo n.º 32
0
    def test_initial(self):
        class _Form(Form):
            fields = [
                FieldBlock('field block', [Field('number', convs.Int())]),
            ]

        env = AppEnvironment.create()
        form = _Form(env, initial={'number': 3})
        self.assertEqual(form.raw_data, MultiDict([('number', '3')]))
        self.assertEqual(form.python_data, {'number': 3})
        self.assertEqual(form.get_field('number').clean_value, 3)
Exemplo n.º 33
0
    def test_perm_getter_repr(self):
        class F(Form):
            fields = [Field("first", permissions="rw"), Field("second")]

        env = AppEnvironment.create()
        form = F(env, permissions="rwx")
        field = form.get_field("first")
        represent = repr(field.perm_getter)
        self.assertIn("FieldPerm", represent)
        self.assertIn("'r'", represent)
        self.assertIn("'w'", represent)
Exemplo n.º 34
0
    def test_required(self):
        Form = self.get_form(required=True)

        env = AppEnvironment.create()
        form = Form(env)
        form.accept(MultiDict({'dt.date': '', 'dt.time': '13:32'}))
        self.assertEqual(list(form.errors.keys()), ['dt'])

        form = Form(env)
        form.accept(MultiDict({'dt.date': '24.03.2013', 'dt.time': ''}))
        self.assertEqual(list(form.errors.keys()), ['dt'])
Exemplo n.º 35
0
    def test_from_python_multiple(self):
        class F(Form):
            fields = [
                Field('name', conv=convs.ListOf(convs.Int),
                      initial=[1,2])
            ]

        env = AppEnvironment.create()
        form = F(env)
        self.assertEqual(form.raw_data,
                         MultiDict([('name', '1'), ('name', '2')]))
Exemplo n.º 36
0
 def test_get_data(self):
     class F(Form):
         fields=[
             Field('first', convs.Int(), initial=1),
             Field('second', convs.Int()),
         ]
     env = AppEnvironment.create()
     form = F(env)
     form.accept({'first':'123', 'second':'246'})
     self.assertEqual(MultiDict([('first', u'123'), ('second', u'246')]),
                      form.get_data())
Exemplo n.º 37
0
 def test_field_block_existing_value(self):
     class f(Form):
         fields = [FieldBlock('block', fields=[Field('number', conv=convs.Int)])]
     env = AppEnvironment.create()
     form = f(env)
     field = form.fields[0]
     conv = field.conv
     form.accept({'number':'123'})
     self.assertEqual(conv._existing_value,  {'number':123})
     conv.field = None
     self.assertEqual(conv._existing_value,  {})
Exemplo n.º 38
0
 def test_readonly(self):
     'Accept of readonly fields'
     class _Form(Form):
         fields=[
             Field('first', convs.Int(), permissions='r'),
             Field('second', convs.Int()),
         ]
     env = AppEnvironment.create()
     form = _Form(env)
     self.assert_(form.accept(MultiDict(first='1', second='2')))
     self.assertEqual(form.python_data, {'first':None, 'second':2})
Exemplo n.º 39
0
 def test_initial(self):
     class _Form(Form):
         fields=[
             FieldBlock('field block', [
                 Field('number', convs.Int())
             ]),
         ]
     env = AppEnvironment.create()
     form = _Form(env, initial={'number': 3})
     self.assertEqual(form.raw_data, MultiDict([('number', '3')]))
     self.assertEqual(form.python_data, {'number': 3})
     self.assertEqual(form.get_field('number').clean_value, 3)
Exemplo n.º 40
0
 def test_init(self):
     'Initialization of form object'
     class F(Form):
         fields=[
             Field('first', convs.Int()),
             Field('second', convs.Int()),
         ]
     env = AppEnvironment.create()
     form = F(env)
     self.assertEqual(form.initial, {})
     self.assertEqual(form.raw_data, {'first':'', 'second':''})
     self.assertEqual(form.python_data, {'first':None, 'second':None})
Exemplo n.º 41
0
 def test_with_initial(self):
     'Accept with initial data'
     class _Form(Form):
         fields=[
             Field('first', convs.Int()),
             Field('second', convs.Int()),
         ]
     env = AppEnvironment.create()
     form = _Form(env, initial={'second':3})
     self.assert_(form.accept(MultiDict(first='1', second='2')))
     self.assertEqual(form.initial, {'second': 3})
     self.assertEqual(form.python_data, {'first':1, 'second':2})
Exemplo n.º 42
0
 def test_with_initial_and_initial(self):
     'Initialization of form object with initial and initial values'
     class F(Form):
         fields=[
             Field('first', convs.Int(), initial=3),
             Field('second', convs.Int()),
         ]
     env = AppEnvironment.create()
     form = F(env, initial={'first':1, 'second':2})
     self.assertEqual(form.initial, {'first':1, 'second':2})
     self.assertEqual(form.raw_data, {'first':'1', 'second':'2'})
     self.assertEqual(form.python_data, {'first':1, 'second':2})
Exemplo n.º 43
0
 def test_with_initial_at_def(self):
     'Initialization of form object with fields initial values'
     class F(Form):
         fields=[
             Field('first', convs.Int(), initial=1),
             Field('second', convs.Int(), get_initial=lambda: 2),
         ]
     env = AppEnvironment.create()
     form = F(env)
     self.assertEqual(form.initial, {})
     self.assertEqual(form.raw_data, {'first':'1', 'second':'2'})
     self.assertEqual(form.python_data, {'first':1, 'second':2})
Exemplo n.º 44
0
 def test_with_initial(self):
     'Accept of readonly fields with initial values'
     class _Form(Form):
         fields=[
             Field('first', convs.Int(), initial=1, permissions='r'),
             Field('second', convs.Int()),
         ]
     env = AppEnvironment.create()
     form = _Form(env)
     self.assert_(form.accept(MultiDict(first='3', second='2')))
     self.assertEqual(form.python_data, {'first':1, 'second':2})
     self.assertEqual(form.raw_data, {'first':'1', 'second':'2'})
Exemplo n.º 45
0
    def test_get_data(self):
        class F(Form):
            fields = [
                Field('first', convs.Int(), initial=1),
                Field('second', convs.Int()),
            ]

        env = AppEnvironment.create()
        form = F(env)
        form.accept({'first': '123', 'second': '246'})
        self.assertEqual(MultiDict([('first', u'123'), ('second', u'246')]),
                         form.get_data())
Exemplo n.º 46
0
    def test_get_perms(self):
        class F(Form):
            fields = [
                Field('first', permissions='rw'),
                Field('second'),
            ]

        env = AppEnvironment.create()
        form = F(env, permissions='rwx')
        first = form.get_field('first')
        second = form.get_field('second')
        self.assertEqual(first.perm_getter.get_perms(first), set('rw'))
        self.assertEqual(second.perm_getter.get_perms(second), set('rwx'))
Exemplo n.º 47
0
 def test_with_initial(self):
     'Accept with initial values'
     class _Form(Form):
         fields=[
             Field('first', convs.Int(), initial=2),
             Field('second', convs.Int(required=False), get_initial=lambda: 2),
         ]
     env = AppEnvironment.create()
     form = _Form(env)
     form.accept(MultiDict(first='1'))
     self.assert_(form.accept(MultiDict(first='1')))
     self.assertEqual(form.initial, {})
     self.assertEqual(form.python_data, {'first':1, 'second':None})
Exemplo n.º 48
0
    def test_readonly(self):
        'Accept of readonly fields'

        class _Form(Form):
            fields = [
                Field('first', convs.Int(), permissions='r'),
                Field('second', convs.Int()),
            ]

        env = AppEnvironment.create()
        form = _Form(env)
        self.assert_(form.accept(MultiDict(first='1', second='2')))
        self.assertEqual(form.python_data, {'first': None, 'second': 2})
Exemplo n.º 49
0
 def test_fieldlist_is_required(self):
     'Fieldlist is required and accepted value is empty'
     class _Form(Form):
         fields=[
             FieldList('list', field=Field('number', convs.Int())),
         ]
     env = AppEnvironment.create()
     form = _Form(env)
     self.assertEqual(form.raw_data, MultiDict())
     self.assertEqual(form.python_data, {'list': []})
     self.assert_(form.accept(MultiDict()))
     self.assertEqual(form.python_data, {'list': []})
     self.assertEqual(form.errors, {})
Exemplo n.º 50
0
    def test_required(self):
        Form = self.get_form(required=True)

        env = AppEnvironment.create()
        form = Form(env)
        form.accept(MultiDict({'dt.date': '',
                               'dt.time': '13:32'}))
        self.assertEqual(form.errors.keys(), ['dt'])

        form = Form(env)
        form.accept(MultiDict({'dt.date': '24.03.2013',
                               'dt.time': ''}))
        self.assertEqual(form.errors.keys(), ['dt'])
Exemplo n.º 51
0
    def test_accept(self):
        class _Form(Form):
            fields = [
                FieldBlock('field block', [Field('number', convs.Int())]),
            ]

        env = AppEnvironment.create()
        form = _Form(env)
        self.assertEqual(form.raw_data, MultiDict([('number', '')]))
        self.assertEqual(form.python_data, {'number': None})
        self.assert_(form.accept({'number': '4'}))
        self.assertEqual(form.python_data, {'number': 4})
        self.assertEqual(form.get_field('number').raw_value, '4')
        self.assertEqual(form.errors, {})
Exemplo n.º 52
0
    def test_mismatch(self):
        class _Form(Form):
            fields = [PasswordSet()]

        env = AppEnvironment.create()
        form = _Form(env)
        accept = form.accept({
            'password.pass': '******',
            'password.conf': 'second'
        })
        self.assertEqual(
            dict(password='******').items(),
            form.errors.items())
        self.assertFalse(accept)
Exemplo n.º 53
0
    def test_init(self):
        'Initialization of form object'

        class F(Form):
            fields = [
                Field('first', convs.Int()),
                Field('second', convs.Int()),
            ]

        env = AppEnvironment.create()
        form = F(env)
        self.assertEqual(form.initial, {})
        self.assertEqual(form.raw_data, {'first': '', 'second': ''})
        self.assertEqual(form.python_data, {'first': None, 'second': None})
Exemplo n.º 54
0
    def test_simple(self):
        'Accept with errors'

        class _Form(Form):
            fields = [
                Field('first', convs.Int(required=True)),
                Field('second', convs.Int(required=True)),
            ]

        env = AppEnvironment.create()
        form = _Form(env)
        self.assert_(not form.accept(MultiDict(first='1')))
        self.assertEqual(form.errors,
                         {'second': convs.Converter.error_required})
Exemplo n.º 55
0
    def test_with_initial(self):
        'Accept with initial data'

        class _Form(Form):
            fields = [
                Field('first', convs.Int()),
                Field('second', convs.Int()),
            ]

        env = AppEnvironment.create()
        form = _Form(env, initial={'second': 3})
        self.assert_(form.accept(MultiDict(first='1', second='2')))
        self.assertEqual(form.initial, {'second': 3})
        self.assertEqual(form.python_data, {'first': 1, 'second': 2})
Exemplo n.º 56
0
    def test_perm_getter_repr(self):
        class F(Form):
            fields = [
                Field('first', permissions='rw'),
                Field('second'),
            ]

        env = AppEnvironment.create()
        form = F(env, permissions='rwx')
        field = form.get_field('first')
        represent = repr(field.perm_getter)
        self.assertIn("FieldPerm", represent)
        self.assertIn("'r'", represent)
        self.assertIn("'w'", represent)
Exemplo n.º 57
0
    def test_field_block_existing_value(self):
        class f(Form):
            fields = [
                FieldBlock('block', fields=[Field('number', conv=convs.Int)])
            ]

        env = AppEnvironment.create()
        form = f(env)
        field = form.fields[0]
        conv = field.conv
        form.accept({'number': '123'})
        self.assertEqual(conv._existing_value, {'number': 123})
        conv.field = None
        self.assertEqual(conv._existing_value, {})
Exemplo n.º 58
0
    def test_with_initial_at_init(self):
        'Initialization of form object with initial values'

        class F(Form):
            fields = [
                Field('first', convs.Int()),
                Field('second', convs.Int()),
            ]

        env = AppEnvironment.create()
        form = F(env, initial={'first': 1, 'second': 2})
        self.assertEqual(form.initial, {'first': 1, 'second': 2})
        self.assertEqual(form.raw_data, {'first': '1', 'second': '2'})
        self.assertEqual(form.python_data, {'first': 1, 'second': 2})
Exemplo n.º 59
0
    def test_with_initial(self):
        'Accept of readonly fields with initial values'

        class _Form(Form):
            fields = [
                Field('first', convs.Int(), initial=1, permissions='r'),
                Field('second', convs.Int()),
            ]

        env = AppEnvironment.create()
        form = _Form(env)
        self.assert_(form.accept(MultiDict(first='3', second='2')))
        self.assertEqual(form.python_data, {'first': 1, 'second': 2})
        self.assertEqual(form.raw_data, {'first': '1', 'second': '2'})
Exemplo n.º 60
0
    def test_fieldlist_is_required(self):
        'Fieldlist is required and accepted value is empty'

        class _Form(Form):
            fields = [
                FieldList('list', field=Field('number', convs.Int())),
            ]

        env = AppEnvironment.create()
        form = _Form(env)
        self.assertEqual(form.raw_data, MultiDict())
        self.assertEqual(form.python_data, {'list': []})
        self.assert_(form.accept(MultiDict()))
        self.assertEqual(form.python_data, {'list': []})
        self.assertEqual(form.errors, {})