cubicweb/web/test/unittest_formfields.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10772 web/test/unittest_formfields.py@2ff5afce3216
child 11170 d034791621ad
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5712
e136d392bd71 [form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""unittests for cw.web.formfields"""
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
    20
from logilab.common.testlib import TestCase, unittest_main, mock_object as mock
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    21
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    22
from yams.constraints import StaticVocabularyConstraint, SizeConstraint
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    23
10772
2ff5afce3216 [web/test] unicode → cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10273
diff changeset
    24
import cubicweb
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
from cubicweb.devtools import TestServerConfiguration
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
    26
from cubicweb.devtools.testlib import CubicWebTC
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
    27
from cubicweb.web.formwidgets import PasswordInput, TextArea, Select, Radio
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    28
from cubicweb.web.formfields import *
9444
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
    29
from cubicweb.web.views.forms import EntityFieldsForm, FieldsForm
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    30
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    31
from cubes.file.entities import File
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
6781
5062d86d6ffe [unittest2] use unittest2 module fixture api
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
    33
def setUpModule(*args):
6631
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    34
    global schema
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    35
    config = TestServerConfiguration('data', apphome=GuessFieldTC.datadir)
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    36
    config.bootstrap_cubes()
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    37
    schema = config.load_schema()
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    38
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    39
class GuessFieldTC(CubicWebTC):
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    40
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    41
    def test_state_fields(self):
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    42
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    43
            title_field = guess_field(schema['State'], schema['name'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    44
            self.assertIsInstance(title_field, StringField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    45
            self.assertEqual(title_field.required, True)
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    46
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
    47
#         synopsis_field = guess_field(schema['State'], schema['synopsis'])
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    48
#         self.assertIsInstance(synopsis_field, StringField)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    49
#         self.assertIsInstance(synopsis_field.widget, TextArea)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    50
#         self.assertEqual(synopsis_field.required, False)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    51
#         self.assertEqual(synopsis_field.help, 'an abstract for this state')
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    52
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    53
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    54
            description_field = guess_field(schema['State'], schema['description'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    55
            self.assertIsInstance(description_field, RichTextField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    56
            self.assertEqual(description_field.required, False)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    57
            self.assertEqual(description_field.format_field, None)
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    58
5712
e136d392bd71 [form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    59
        # description_format_field = guess_field(schema['State'], schema['description_format'])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    60
        # self.assertEqual(description_format_field, None)
5712
e136d392bd71 [form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    61
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    62
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    63
            description_format_field = guess_field(schema['State'], schema['description_format'],
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    64
                                                   req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    65
            self.assertEqual(description_format_field.internationalizable, True)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    66
            self.assertEqual(description_format_field.sort, True)
1265
e5cdd5c0dce3 handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents: 1104
diff changeset
    67
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
    68
#         wikiid_field = guess_field(schema['State'], schema['wikiid'])
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    69
#         self.assertIsInstance(wikiid_field, StringField)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    70
#         self.assertEqual(wikiid_field.required, False)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    72
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    73
    def test_cwuser_fields(self):
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    74
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    75
            upassword_field = guess_field(schema['CWUser'], schema['upassword'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    76
            self.assertIsInstance(upassword_field, StringField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    77
            self.assertIsInstance(upassword_field.widget, PasswordInput)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    78
            self.assertEqual(upassword_field.required, True)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    80
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    81
            last_login_time_field = guess_field(schema['CWUser'], schema['last_login_time'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    82
            self.assertIsInstance(last_login_time_field, DateTimeField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    83
            self.assertEqual(last_login_time_field.required, False)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    85
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    86
            in_group_field = guess_field(schema['CWUser'], schema['in_group'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    87
            self.assertIsInstance(in_group_field, RelationField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    88
            self.assertEqual(in_group_field.required, True)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    89
            self.assertEqual(in_group_field.role, 'subject')
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    90
            self.assertEqual(in_group_field.help, 'groups grant permissions to the user')
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    92
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    93
            owned_by_field = guess_field(schema['CWUser'], schema['owned_by'], 'object', req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    94
            self.assertIsInstance(owned_by_field, RelationField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    95
            self.assertEqual(owned_by_field.required, False)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
    96
            self.assertEqual(owned_by_field.role, 'object')
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
1268
5db94912650b get field's help in guess_field
sylvain.thenault@logilab.fr
parents: 1265
diff changeset
    98
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    99
    def test_file_fields(self):
5712
e136d392bd71 [form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   100
        # data_format_field = guess_field(schema['File'], schema['data_format'])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   101
        # self.assertEqual(data_format_field, None)
5712
e136d392bd71 [form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   102
        # data_encoding_field = guess_field(schema['File'], schema['data_encoding'])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   103
        # self.assertEqual(data_encoding_field, None)
5712
e136d392bd71 [form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   104
        # data_name_field = guess_field(schema['File'], schema['data_name'])
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   105
        # self.assertEqual(data_name_field, None)
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
   106
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   107
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   108
            data_field = guess_field(schema['File'], schema['data'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   109
            self.assertIsInstance(data_field, FileField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   110
            self.assertEqual(data_field.required, True)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   111
            self.assertIsInstance(data_field.format_field, StringField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   112
            self.assertIsInstance(data_field.encoding_field, StringField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   113
            self.assertIsInstance(data_field.name_field, StringField)
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   114
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
   115
    def test_constraints_priority(self):
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   116
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   117
            salesterm_field = guess_field(schema['Salesterm'], schema['reason'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   118
            constraints = schema['reason'].rdef('Salesterm', 'String').constraints
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   119
            self.assertEqual([c.__class__ for c in constraints],
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   120
                              [SizeConstraint, StaticVocabularyConstraint])
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   121
            self.assertIsInstance(salesterm_field, StringField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   122
            self.assertIsInstance(salesterm_field.widget, Select)
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
   123
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   124
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   125
    def test_bool_field_base(self):
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   126
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   127
            field = guess_field(schema['CWAttribute'], schema['indexed'], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   128
            self.assertIsInstance(field, BooleanField)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   129
            self.assertEqual(field.required, False)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   130
            self.assertIsInstance(field.widget, Radio)
10772
2ff5afce3216 [web/test] unicode → cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10273
diff changeset
   131
            self.assertEqual(field.vocabulary(mock(_cw=mock(_=cubicweb._))),
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   132
                              [(u'yes', '1'), (u'no', '')])
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   133
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   134
    def test_bool_field_explicit_choices(self):
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   135
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   136
            field = guess_field(schema['CWAttribute'], schema['indexed'],
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   137
                                choices=[(u'maybe', '1'), (u'no', '')], req=req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   138
            self.assertIsInstance(field.widget, Radio)
10772
2ff5afce3216 [web/test] unicode → cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10273
diff changeset
   139
            self.assertEqual(field.vocabulary(mock(req=mock(_=cubicweb._))),
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   140
                              [(u'maybe', '1'), (u'no', '')])
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   141
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   142
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   143
class MoreFieldsTC(CubicWebTC):
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   144
    def test_rtf_format_field(self):
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   145
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   146
            req.use_fckeditor = lambda: False
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   147
            e = self.vreg['etypes'].etype_class('State')(req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   148
            form = EntityFieldsForm(req, entity=e)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   149
            description_field = guess_field(schema['State'], schema['description'])
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   150
            description_format_field = description_field.get_format_field(form)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   151
            self.assertEqual(description_format_field.internationalizable, True)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   152
            self.assertEqual(description_format_field.sort, True)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   153
            # unlike below, initial is bound to form.form_field_format
10273
ef74abcf369d [config] disable fckeditor by default (closes #1368900)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9647
diff changeset
   154
            self.assertEqual(description_format_field.value(form), 'text/plain')
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   155
            req.cnx.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"')
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   156
            req.cnx.commit()
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   157
            self.assertEqual(description_format_field.value(form), 'text/rest')
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   158
3334
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   159
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
   160
    def test_property_key_field(self):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
   161
        from cubicweb.web.views.cwproperties import PropertyKeyField
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   162
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   163
            field = PropertyKeyField(name='test')
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   164
            e = self.vreg['etypes'].etype_class('CWProperty')(req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   165
            renderer = self.vreg['formrenderers'].select('base', req)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   166
            form = EntityFieldsForm(req, entity=e)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   167
            form.formvalues = {}
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   168
            field.render(form, renderer)
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
   169
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
   170
9444
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   171
class CompoundFieldTC(CubicWebTC):
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   172
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   173
    def test_multipart(self):
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   174
        """Ensures that compound forms have needs_multipart set if their
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   175
        children require it"""
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   176
        class AForm(FieldsForm):
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   177
            comp = CompoundField([IntField(), StringField()])
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   178
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   179
            aform = AForm(req, None)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   180
            self.assertFalse(aform.needs_multipart)
9444
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   181
        class MForm(FieldsForm):
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   182
            comp = CompoundField([IntField(), FileField()])
9647
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   183
        with self.admin_access.web_request() as req:
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   184
            mform = MForm(req, None)
74cddb36ce1e [web/test] port unittest_formfields to RepoAccess API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9444
diff changeset
   185
            self.assertTrue(mform.needs_multipart)
9444
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   186
6012cf57f48e [web/formfields] Make CompoundField inherit needs_multipart from its subfields
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8666
diff changeset
   187
3334
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   188
class UtilsTC(TestCase):
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   189
    def test_vocab_sort(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   190
        self.assertEqual(vocab_sort([('Z', 1), ('A', 2),
3334
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   191
                                      ('Group 1', None), ('Y', 3), ('B', 4),
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   192
                                      ('Group 2', None), ('X', 5), ('C', 6)]),
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   193
                          [('A', 2), ('Z', 1),
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   194
                           ('Group 1', None), ('B', 4), ('Y', 3),
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   195
                           ('Group 2', None), ('C', 6), ('X', 5)]
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   196
                          )
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   197
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
   198
if __name__ == '__main__':
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
   199
    unittest_main()