web/test/unittest_formfields.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 19 May 2009 10:05:56 +0200
branchstable
changeset 1858 69e41c88e195
parent 1836 d3f0c6e868ef
child 1977 606923dff11b
permissions -rw-r--r--
connect format field to form_field_format for its initial value
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
"""unittests for cw.web.formfields"""
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
     3
from logilab.common.testlib import TestCase, unittest_main
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
     4
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
     5
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
     6
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
from cubicweb.devtools import TestServerConfiguration
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
     8
from cubicweb.devtools.testlib import EnvBasedTC
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
     9
from cubicweb.web.form import EntityFieldsForm
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    10
from cubicweb.web.formwidgets import PasswordInput, TextArea, Select
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    11
from cubicweb.web.formfields import *
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    12
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    13
from cubes.file.entities import File
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
config = TestServerConfiguration('data')
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    16
config.bootstrap_cubes()
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
schema = config.load_schema()
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    18
state_schema = schema['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
    19
cwuser_schema = schema['CWUser']
1654
57b9fd2462b8 test updates
sylvain.thenault@logilab.fr
parents: 1570
diff changeset
    20
file_schema = schema['File']
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    21
salesterm_schema = schema['Salesterm']
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    22
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    23
class GuessFieldTC(TestCase):
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    24
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    25
    def test_state_fields(self):
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    26
        title_field = guess_field(state_schema, schema['name'])
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
        self.assertIsInstance(title_field, StringField)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
        self.assertEquals(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
    29
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    30
#         synopsis_field = guess_field(state_schema, schema['synopsis'])
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    31
#         self.assertIsInstance(synopsis_field, StringField)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    32
#         self.assertIsInstance(synopsis_field.widget, TextArea)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    33
#         self.assertEquals(synopsis_field.required, False)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    34
#         self.assertEquals(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
    35
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    36
        description_field = guess_field(state_schema, schema['description'])
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    37
        self.assertIsInstance(description_field, RichTextField)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    38
        self.assertEquals(description_field.required, False)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    39
        self.assertEquals(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
    40
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    41
        description_format_field = guess_field(state_schema, schema['description_format'])
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    42
        self.assertEquals(description_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
    43
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    44
        description_format_field = guess_field(state_schema, schema['description_format'], skip_meta_attr=False)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    45
        self.assertEquals(description_format_field.internationalizable, True)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    46
        self.assertEquals(description_format_field.sort, True)
1797
c2a80130b06d fix some web tests, adjusts rtags
sylvain.thenault@logilab.fr
parents: 1654
diff changeset
    47
        self.assertEquals(description_format_field.initial(None), 'text/rest')
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
    48
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    49
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    50
#         wikiid_field = guess_field(state_schema, schema['wikiid'])
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    51
#         self.assertIsInstance(wikiid_field, StringField)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1453
diff changeset
    52
#         self.assertEquals(wikiid_field.required, False)
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
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
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    55
    def test_cwuser_fields(self):
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    56
        upassword_field = guess_field(cwuser_schema, schema['upassword'])
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
        self.assertIsInstance(upassword_field, StringField)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
        self.assertIsInstance(upassword_field.widget, PasswordInput)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
        self.assertEquals(upassword_field.required, True)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    60
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    61
        last_login_time_field = guess_field(cwuser_schema, schema['last_login_time'])
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    62
        self.assertIsInstance(last_login_time_field, DateTimeField)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
        self.assertEquals(last_login_time_field.required, False)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    64
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    65
        in_group_field = guess_field(cwuser_schema, schema['in_group'])
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    66
        self.assertIsInstance(in_group_field, RelationField)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
        self.assertEquals(in_group_field.required, True)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
        self.assertEquals(in_group_field.role, 'subject')
1268
5db94912650b get field's help in guess_field
sylvain.thenault@logilab.fr
parents: 1265
diff changeset
    69
        self.assertEquals(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
    70
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    71
        owned_by_field = guess_field(cwuser_schema, schema['owned_by'], 'object')
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
        self.assertIsInstance(owned_by_field, RelationField)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
        self.assertEquals(owned_by_field.required, False)
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
        self.assertEquals(owned_by_field.role, 'object')
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
1268
5db94912650b get field's help in guess_field
sylvain.thenault@logilab.fr
parents: 1265
diff changeset
    76
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    77
    def test_file_fields(self):
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    78
        data_format_field = guess_field(file_schema, schema['data_format'])
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    79
        self.assertEquals(data_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
    80
        data_encoding_field = guess_field(file_schema, schema['data_encoding'])
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    81
        self.assertEquals(data_encoding_field, None)
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    82
1453
a9841184be7c guess_field now takes an entity schema as first argument, not an entity class
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    83
        data_field = guess_field(file_schema, schema['data'])
1104
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    84
        self.assertIsInstance(data_field, FileField)
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    85
        self.assertEquals(data_field.required, True)
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    86
        self.assertIsInstance(data_field.format_field, StringField)
58f27c3c0167 more guess_field tests and fixes
sylvain.thenault@logilab.fr
parents: 1095
diff changeset
    87
        self.assertIsInstance(data_field.encoding_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
    88
1836
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    89
    def test_constraints_priority(self):
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    90
        salesterm_field = guess_field(salesterm_schema, schema['reason'])
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    91
        constraints = schema['reason'].rproperty('Salesterm', 'String', 'constraints')
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    92
        self.assertEquals([c.__class__ for c in constraints],
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    93
                          [SizeConstraint, StaticVocabularyConstraint])
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    94
        self.assertIsInstance(salesterm_field.widget, Select)
d3f0c6e868ef fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
Florent <florent@secondweb.fr>
parents: 1797
diff changeset
    95
1858
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    96
class MoreFieldsTC(EnvBasedTC):
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
    97
    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
    98
        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
    99
        req.use_fckeditor = lambda: False
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   100
        e = self.etype_instance('State')
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   101
        form = EntityFieldsForm(req, entity=e)
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   102
        description_field = guess_field(state_schema, schema['description'])
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   103
        description_format_field = description_field.get_format_field(form)
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   104
        self.assertEquals(description_format_field.internationalizable, True)
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   105
        self.assertEquals(description_format_field.sort, True)
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   106
        # unlike below, initial is bound to form.form_field_format
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   107
        self.assertEquals(description_format_field.initial(form), 'text/html')
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   108
        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
   109
        self.commit()
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   110
        self.assertEquals(description_format_field.initial(form), 'text/rest')
69e41c88e195 connect format field to form_field_format for its initial value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1836
diff changeset
   111
1095
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
if __name__ == '__main__':
6917ebe281e9 test and fix guess_field, some pylint fixes
sylvain.thenault@logilab.fr
parents:
diff changeset
   113
    unittest_main()