web/test/unittest_formfields.py
changeset 8666 1dd655788ece
parent 8519 e436688f75f4
child 9444 6012cf57f48e
equal deleted inserted replaced
8665:e65af61bde7d 8666:1dd655788ece
    33     global schema
    33     global schema
    34     config = TestServerConfiguration('data', apphome=GuessFieldTC.datadir)
    34     config = TestServerConfiguration('data', apphome=GuessFieldTC.datadir)
    35     config.bootstrap_cubes()
    35     config.bootstrap_cubes()
    36     schema = config.load_schema()
    36     schema = config.load_schema()
    37 
    37 
    38 class GuessFieldTC(TestCase):
    38 class GuessFieldTC(CubicWebTC):
       
    39 
       
    40     def setUp(self):
       
    41         super(GuessFieldTC, self).setUp()
       
    42         self.req = self.request()
    39 
    43 
    40     def test_state_fields(self):
    44     def test_state_fields(self):
    41         title_field = guess_field(schema['State'], schema['name'])
    45         title_field = guess_field(schema['State'], schema['name'], req=self.req)
    42         self.assertIsInstance(title_field, StringField)
    46         self.assertIsInstance(title_field, StringField)
    43         self.assertEqual(title_field.required, True)
    47         self.assertEqual(title_field.required, True)
    44 
    48 
    45 #         synopsis_field = guess_field(schema['State'], schema['synopsis'])
    49 #         synopsis_field = guess_field(schema['State'], schema['synopsis'])
    46 #         self.assertIsInstance(synopsis_field, StringField)
    50 #         self.assertIsInstance(synopsis_field, StringField)
    47 #         self.assertIsInstance(synopsis_field.widget, TextArea)
    51 #         self.assertIsInstance(synopsis_field.widget, TextArea)
    48 #         self.assertEqual(synopsis_field.required, False)
    52 #         self.assertEqual(synopsis_field.required, False)
    49 #         self.assertEqual(synopsis_field.help, 'an abstract for this state')
    53 #         self.assertEqual(synopsis_field.help, 'an abstract for this state')
    50 
    54 
    51         description_field = guess_field(schema['State'], schema['description'])
    55         description_field = guess_field(schema['State'], schema['description'], req=self.req)
    52         self.assertIsInstance(description_field, RichTextField)
    56         self.assertIsInstance(description_field, RichTextField)
    53         self.assertEqual(description_field.required, False)
    57         self.assertEqual(description_field.required, False)
    54         self.assertEqual(description_field.format_field, None)
    58         self.assertEqual(description_field.format_field, None)
    55 
    59 
    56         # description_format_field = guess_field(schema['State'], schema['description_format'])
    60         # description_format_field = guess_field(schema['State'], schema['description_format'])
    57         # self.assertEqual(description_format_field, None)
    61         # self.assertEqual(description_format_field, None)
    58 
    62 
    59         description_format_field = guess_field(schema['State'], schema['description_format'])
    63         description_format_field = guess_field(schema['State'], schema['description_format'],
       
    64                                                req=self.req)
    60         self.assertEqual(description_format_field.internationalizable, True)
    65         self.assertEqual(description_format_field.internationalizable, True)
    61         self.assertEqual(description_format_field.sort, True)
    66         self.assertEqual(description_format_field.sort, True)
    62 
    67 
    63 #         wikiid_field = guess_field(schema['State'], schema['wikiid'])
    68 #         wikiid_field = guess_field(schema['State'], schema['wikiid'])
    64 #         self.assertIsInstance(wikiid_field, StringField)
    69 #         self.assertIsInstance(wikiid_field, StringField)
    65 #         self.assertEqual(wikiid_field.required, False)
    70 #         self.assertEqual(wikiid_field.required, False)
    66 
    71 
    67 
    72 
    68     def test_cwuser_fields(self):
    73     def test_cwuser_fields(self):
    69         upassword_field = guess_field(schema['CWUser'], schema['upassword'])
    74         upassword_field = guess_field(schema['CWUser'], schema['upassword'], req=self.req)
    70         self.assertIsInstance(upassword_field, StringField)
    75         self.assertIsInstance(upassword_field, StringField)
    71         self.assertIsInstance(upassword_field.widget, PasswordInput)
    76         self.assertIsInstance(upassword_field.widget, PasswordInput)
    72         self.assertEqual(upassword_field.required, True)
    77         self.assertEqual(upassword_field.required, True)
    73 
    78 
    74         last_login_time_field = guess_field(schema['CWUser'], schema['last_login_time'])
    79         last_login_time_field = guess_field(schema['CWUser'], schema['last_login_time'], req=self.req)
    75         self.assertIsInstance(last_login_time_field, DateTimeField)
    80         self.assertIsInstance(last_login_time_field, DateTimeField)
    76         self.assertEqual(last_login_time_field.required, False)
    81         self.assertEqual(last_login_time_field.required, False)
    77 
    82 
    78         in_group_field = guess_field(schema['CWUser'], schema['in_group'])
    83         in_group_field = guess_field(schema['CWUser'], schema['in_group'], req=self.req)
    79         self.assertIsInstance(in_group_field, RelationField)
    84         self.assertIsInstance(in_group_field, RelationField)
    80         self.assertEqual(in_group_field.required, True)
    85         self.assertEqual(in_group_field.required, True)
    81         self.assertEqual(in_group_field.role, 'subject')
    86         self.assertEqual(in_group_field.role, 'subject')
    82         self.assertEqual(in_group_field.help, 'groups grant permissions to the user')
    87         self.assertEqual(in_group_field.help, 'groups grant permissions to the user')
    83 
    88 
    84         owned_by_field = guess_field(schema['CWUser'], schema['owned_by'], 'object')
    89         owned_by_field = guess_field(schema['CWUser'], schema['owned_by'], 'object', req=self.req)
    85         self.assertIsInstance(owned_by_field, RelationField)
    90         self.assertIsInstance(owned_by_field, RelationField)
    86         self.assertEqual(owned_by_field.required, False)
    91         self.assertEqual(owned_by_field.required, False)
    87         self.assertEqual(owned_by_field.role, 'object')
    92         self.assertEqual(owned_by_field.role, 'object')
    88 
    93 
    89 
    94 
    93         # data_encoding_field = guess_field(schema['File'], schema['data_encoding'])
    98         # data_encoding_field = guess_field(schema['File'], schema['data_encoding'])
    94         # self.assertEqual(data_encoding_field, None)
    99         # self.assertEqual(data_encoding_field, None)
    95         # data_name_field = guess_field(schema['File'], schema['data_name'])
   100         # data_name_field = guess_field(schema['File'], schema['data_name'])
    96         # self.assertEqual(data_name_field, None)
   101         # self.assertEqual(data_name_field, None)
    97 
   102 
    98         data_field = guess_field(schema['File'], schema['data'])
   103         data_field = guess_field(schema['File'], schema['data'], req=self.req)
    99         self.assertIsInstance(data_field, FileField)
   104         self.assertIsInstance(data_field, FileField)
   100         self.assertEqual(data_field.required, True)
   105         self.assertEqual(data_field.required, True)
   101         self.assertIsInstance(data_field.format_field, StringField)
   106         self.assertIsInstance(data_field.format_field, StringField)
   102         self.assertIsInstance(data_field.encoding_field, StringField)
   107         self.assertIsInstance(data_field.encoding_field, StringField)
   103         self.assertIsInstance(data_field.name_field, StringField)
   108         self.assertIsInstance(data_field.name_field, StringField)
   104 
   109 
   105     def test_constraints_priority(self):
   110     def test_constraints_priority(self):
   106         salesterm_field = guess_field(schema['Salesterm'], schema['reason'])
   111         salesterm_field = guess_field(schema['Salesterm'], schema['reason'], req=self.req)
   107         constraints = schema['reason'].rdef('Salesterm', 'String').constraints
   112         constraints = schema['reason'].rdef('Salesterm', 'String').constraints
   108         self.assertEqual([c.__class__ for c in constraints],
   113         self.assertEqual([c.__class__ for c in constraints],
   109                           [SizeConstraint, StaticVocabularyConstraint])
   114                           [SizeConstraint, StaticVocabularyConstraint])
   110         self.assertIsInstance(salesterm_field, StringField)
   115         self.assertIsInstance(salesterm_field, StringField)
   111         self.assertIsInstance(salesterm_field.widget, Select)
   116         self.assertIsInstance(salesterm_field.widget, Select)
   112 
   117 
   113 
   118 
   114     def test_bool_field_base(self):
   119     def test_bool_field_base(self):
   115         field = guess_field(schema['CWAttribute'], schema['indexed'])
   120         field = guess_field(schema['CWAttribute'], schema['indexed'], req=self.req)
   116         self.assertIsInstance(field, BooleanField)
   121         self.assertIsInstance(field, BooleanField)
   117         self.assertEqual(field.required, False)
   122         self.assertEqual(field.required, False)
   118         self.assertIsInstance(field.widget, Radio)
   123         self.assertIsInstance(field.widget, Radio)
   119         self.assertEqual(field.vocabulary(mock(_cw=mock(_=unicode))),
   124         self.assertEqual(field.vocabulary(mock(_cw=mock(_=unicode))),
   120                           [(u'yes', '1'), (u'no', '')])
   125                           [(u'yes', '1'), (u'no', '')])
   121 
   126 
   122     def test_bool_field_explicit_choices(self):
   127     def test_bool_field_explicit_choices(self):
   123         field = guess_field(schema['CWAttribute'], schema['indexed'],
   128         field = guess_field(schema['CWAttribute'], schema['indexed'],
   124                             choices=[(u'maybe', '1'), (u'no', '')])
   129                             choices=[(u'maybe', '1'), (u'no', '')], req=self.req)
   125         self.assertIsInstance(field.widget, Radio)
   130         self.assertIsInstance(field.widget, Radio)
   126         self.assertEqual(field.vocabulary(mock(req=mock(_=unicode))),
   131         self.assertEqual(field.vocabulary(mock(req=mock(_=unicode))),
   127                           [(u'maybe', '1'), (u'no', '')])
   132                           [(u'maybe', '1'), (u'no', '')])
   128 
   133 
   129 
   134