web/test/unittest_formfields.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 19 Mar 2013 15:17:34 +0100
changeset 8734 3530b7494195
parent 8666 1dd655788ece
child 9444 6012cf57f48e
permissions -rw-r--r--
[test/web] fix invisibly bogus test (prepares #2755994) The test was wrong but that was cancelled out by a cache effect and fuzzy naming. Wiping the entity caches restores sanity: the choices list are the same before and after the SET. Also field.choices uses entity.unrelated but always returns related + unrelated elements. Hence `choice` replaces `unrelated` where it makes sense. AssertIn is used in place of AssertTrue.
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
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
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
    25
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
    26
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
    27
from cubicweb.web.formfields import *
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    28
from cubicweb.web.views.forms import EntityFieldsForm
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    29
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    30
from cubes.file.entities import File
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
6781
5062d86d6ffe [unittest2] use unittest2 module fixture api
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
    32
def setUpModule(*args):
6631
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    33
    global schema
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    34
    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
    35
    config.bootstrap_cubes()
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    36
    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
    37
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    38
class GuessFieldTC(CubicWebTC):
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    39
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    40
    def setUp(self):
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    41
        super(GuessFieldTC, self).setUp()
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    42
        self.req = self.request()
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    43
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    44
    def test_state_fields(self):
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    45
        title_field = guess_field(schema['State'], schema['name'], req=self.req)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
        self.assertIsInstance(title_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
    47
        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
    48
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
    49
#         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
    50
#         self.assertIsInstance(synopsis_field, StringField)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    51
#         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
    52
#         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
    53
#         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
    54
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    55
        description_field = guess_field(schema['State'], schema['description'], req=self.req)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    56
        self.assertIsInstance(description_field, RichTextField)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    57
        self.assertEqual(description_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
    58
        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
    59
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
    60
        # 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
    61
        # 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
    62
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    63
        description_format_field = guess_field(schema['State'], schema['description_format'],
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    64
                                               req=self.req)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    65
        self.assertEqual(description_format_field.internationalizable, True)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
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):
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    74
        upassword_field = guess_field(schema['CWUser'], schema['upassword'], req=self.req)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
        self.assertIsInstance(upassword_field, StringField)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
        self.assertIsInstance(upassword_field.widget, PasswordInput)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    77
        self.assertEqual(upassword_field.required, True)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    79
        last_login_time_field = guess_field(schema['CWUser'], schema['last_login_time'], req=self.req)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
        self.assertIsInstance(last_login_time_field, DateTimeField)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    81
        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
    82
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    83
        in_group_field = guess_field(schema['CWUser'], schema['in_group'], req=self.req)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
        self.assertIsInstance(in_group_field, RelationField)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    85
        self.assertEqual(in_group_field.required, True)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    86
        self.assertEqual(in_group_field.role, 'subject')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    87
        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
    88
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
    89
        owned_by_field = guess_field(schema['CWUser'], schema['owned_by'], 'object', req=self.req)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
        self.assertIsInstance(owned_by_field, RelationField)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
    91
        self.assertEqual(owned_by_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
    92
        self.assertEqual(owned_by_field.role, 'object')
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
1268
5db94912650b get field's help in guess_field
sylvain.thenault@logilab.fr
parents: 1265
diff changeset
    94
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    95
    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
    96
        # 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
    97
        # 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
    98
        # 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
    99
        # 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
   100
        # 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
   101
        # self.assertEqual(data_name_field, None)
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
   102
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
   103
        data_field = guess_field(schema['File'], schema['data'], req=self.req)
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
   104
        self.assertIsInstance(data_field, FileField)
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_field.required, True)
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
   106
        self.assertIsInstance(data_field.format_field, StringField)
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
   107
        self.assertIsInstance(data_field.encoding_field, StringField)
3535
106b95ec1144 more test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3334
diff changeset
   108
        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
   109
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
   110
    def test_constraints_priority(self):
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
   111
        salesterm_field = guess_field(schema['Salesterm'], schema['reason'], req=self.req)
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4084
diff changeset
   112
        constraints = schema['reason'].rdef('Salesterm', 'String').constraints
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   113
        self.assertEqual([c.__class__ for c in constraints],
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
   114
                          [SizeConstraint, StaticVocabularyConstraint])
4172
4d4cef034eec all web tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4084
diff changeset
   115
        self.assertIsInstance(salesterm_field, StringField)
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
   116
        self.assertIsInstance(salesterm_field.widget, Select)
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
   117
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
   118
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
   119
    def test_bool_field_base(self):
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
   120
        field = guess_field(schema['CWAttribute'], schema['indexed'], req=self.req)
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
   121
        self.assertIsInstance(field, BooleanField)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   122
        self.assertEqual(field.required, False)
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
   123
        self.assertIsInstance(field.widget, Radio)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   124
        self.assertEqual(field.vocabulary(mock(_cw=mock(_=unicode))),
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
   125
                          [(u'yes', '1'), (u'no', '')])
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
   126
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
   127
    def test_bool_field_explicit_choices(self):
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
   128
        field = guess_field(schema['CWAttribute'], schema['indexed'],
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8519
diff changeset
   129
                            choices=[(u'maybe', '1'), (u'no', '')], req=self.req)
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
   130
        self.assertIsInstance(field.widget, Radio)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   131
        self.assertEqual(field.vocabulary(mock(req=mock(_=unicode))),
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
   132
                          [(u'maybe', '1'), (u'no', '')])
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
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   135
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
   136
    def test_rtf_format_field(self):
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   137
        req = self.request()
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   138
        req.use_fckeditor = lambda: False
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   139
        e = self.vreg['etypes'].etype_class('State')(req)
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   140
        form = EntityFieldsForm(req, entity=e)
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
        description_field = guess_field(schema['State'], schema['description'])
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   142
        description_format_field = description_field.get_format_field(form)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   143
        self.assertEqual(description_format_field.internationalizable, True)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   144
        self.assertEqual(description_format_field.sort, True)
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   145
        # unlike below, initial is bound to form.form_field_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
   146
        self.assertEqual(description_format_field.value(form), 'text/html')
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   147
        self.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"')
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   148
        self.commit()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5712
diff changeset
   149
        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
   150
3334
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   151
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
   152
    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
   153
        from cubicweb.web.views.cwproperties import PropertyKeyField
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
   154
        req = self.request()
8519
e436688f75f4 [web test] fix test broken by 199fefe396ab
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   155
        field = PropertyKeyField(name='test')
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
   156
        e = self.vreg['etypes'].etype_class('CWProperty')(req)
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
   157
        renderer = self.vreg['formrenderers'].select('base', req)
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
   158
        form = EntityFieldsForm(req, entity=e)
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
   159
        form.formvalues = {}
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
        field.render(form, renderer)
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
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
   162
3334
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   163
class UtilsTC(TestCase):
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   164
    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
   165
        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
   166
                                      ('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
   167
                                      ('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
   168
                          [('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
   169
                           ('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
   170
                           ('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
   171
                          )
8d831c02da9a fix sort of fields vocabulary: should consider option groups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2091
diff changeset
   172
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
   173
if __name__ == '__main__':
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
   174
    unittest_main()