# HG changeset patch # User Sylvain Thénault # Date 1455699723 -3600 # Node ID d034791621ad1b2cb46600e777fde52796d85b7f # Parent c1eb5a676c80ef28a03d555597200a1d86a6095d pep8 unittest_application and unittest_formfields diff -r c1eb5a676c80 -r d034791621ad cubicweb/web/test/unittest_application.py --- a/cubicweb/web/test/unittest_application.py Tue Jan 12 14:31:14 2016 +0100 +++ b/cubicweb/web/test/unittest_application.py Wed Feb 17 10:02:03 2016 +0100 @@ -1,4 +1,4 @@ -# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of CubicWeb. @@ -34,6 +34,7 @@ from cubicweb.web.application import anonymized_request from cubicweb import repoapi + class FakeMapping: """emulates a mapping module""" def __init__(self): @@ -41,9 +42,11 @@ self.ATTRIBUTES_MAP = {} self.RELATIONS_MAP = {} + class MockCursor: def __init__(self): self.executed = [] + def execute(self, rql, args=None, build_descr=False): args = args or {} self.executed.append(rql % args) @@ -69,21 +72,19 @@ def setUp(self): self._cw = FakeRequest() - def test_list_arg(self): """tests the list_arg() function""" list_arg = self._cw.list_form_param self.assertEqual(list_arg('arg3', {}), []) - d = {'arg1' : "value1", - 'arg2' : ('foo', INTERNAL_FIELD_VALUE,), - 'arg3' : ['bar']} + d = {'arg1': "value1", + 'arg2': ('foo', INTERNAL_FIELD_VALUE,), + 'arg3': ['bar']} self.assertEqual(list_arg('arg1', d, True), ['value1']) - self.assertEqual(d, {'arg2' : ('foo', INTERNAL_FIELD_VALUE), 'arg3' : ['bar'],}) + self.assertEqual(d, {'arg2': ('foo', INTERNAL_FIELD_VALUE), 'arg3': ['bar']}) self.assertEqual(list_arg('arg2', d, True), ['foo']) - self.assertEqual({'arg3' : ['bar'],}, d) - self.assertEqual(list_arg('arg3', d), ['bar',]) - self.assertEqual({'arg3' : ['bar'],}, d) - + self.assertEqual({'arg3': ['bar']}, d) + self.assertEqual(list_arg('arg3', d), ['bar']) + self.assertEqual({'arg3': ['bar']}, d) def test_from_controller(self): self._cw.vreg['controllers'] = {'view': 1, 'login': 1} @@ -107,51 +108,40 @@ def setUp(self): self.ctrl = FakeController() - #def test_which_mapping(self): - # """tests which mapping is used (application or core)""" - # init_mapping() - # from cubicweb.common import mapping - # self.assertEqual(mapping.MAPPING_USED, 'core') - # sys.modules['mapping'] = FakeMapping() - # init_mapping() - # self.assertEqual(mapping.MAPPING_USED, 'application') - # del sys.modules['mapping'] - def test_execute_linkto(self): """tests the execute_linkto() function""" self.assertEqual(self.ctrl.execute_linkto(), None) self.assertEqual(self.ctrl._cursor.executed, - []) + []) - self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:object', - 'eid': 8}) + self.ctrl.set_form({'__linkto': 'works_for:12_13_14:object', + 'eid': 8}) self.ctrl.execute_linkto() self.assertEqual(self.ctrl._cursor.executed, - ['SET Y works_for X WHERE X eid 8, Y eid %s' % i - for i in (12, 13, 14)]) + ['SET Y works_for X WHERE X eid 8, Y eid %s' % i + for i in (12, 13, 14)]) self.ctrl.new_cursor() - self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:subject', - 'eid': 8}) + self.ctrl.set_form({'__linkto': 'works_for:12_13_14:subject', + 'eid': 8}) self.ctrl.execute_linkto() self.assertEqual(self.ctrl._cursor.executed, - ['SET X works_for Y WHERE X eid 8, Y eid %s' % i - for i in (12, 13, 14)]) - + ['SET X works_for Y WHERE X eid 8, Y eid %s' % i + for i in (12, 13, 14)]) self.ctrl.new_cursor() - self.ctrl._cw.form = {'__linkto' : 'works_for:12_13_14:object'} + self.ctrl._cw.form = {'__linkto': 'works_for:12_13_14:object'} self.ctrl.execute_linkto(eid=8) self.assertEqual(self.ctrl._cursor.executed, - ['SET Y works_for X WHERE X eid 8, Y eid %s' % i - for i in (12, 13, 14)]) + ['SET Y works_for X WHERE X eid 8, Y eid %s' % i + for i in (12, 13, 14)]) self.ctrl.new_cursor() - self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:subject'}) + self.ctrl.set_form({'__linkto': 'works_for:12_13_14:subject'}) self.ctrl.execute_linkto(eid=8) self.assertEqual(self.ctrl._cursor.executed, - ['SET X works_for Y WHERE X eid 8, Y eid %s' % i - for i in (12, 13, 14)]) + ['SET X works_for Y WHERE X eid 8, Y eid %s' % i + for i in (12, 13, 14)]) class ApplicationTC(CubicWebTC): @@ -184,24 +174,24 @@ user = req.user eid = text_type(user.eid) req.form = { - 'eid': eid, - '__type:'+eid: 'CWUser', '_cw_entity_fields:'+eid: 'login-subject', - 'login-subject:'+eid: '', # ERROR: no login specified - # just a sample, missing some necessary information for real life + 'eid': eid, + '__type:' + eid: 'CWUser', + '_cw_entity_fields:' + eid: 'login-subject', + 'login-subject:' + eid: '', # ERROR: no login specified + # just a sample, missing some necessary information for real life '__errorurl': 'view?vid=edition...' - } + } path, params = self.expect_redirect_handle_request(req, 'edit') forminfo = req.session.data['view?vid=edition...'] eidmap = forminfo['eidmap'] self.assertEqual(eidmap, {}) values = forminfo['values'] - self.assertEqual(values['login-subject:'+eid], '') + self.assertEqual(values['login-subject:' + eid], '') self.assertEqual(values['eid'], eid) error = forminfo['error'] self.assertEqual(error.entity, user.eid) self.assertEqual(error.errors['login-subject'], 'required field') - def test_validation_error_dont_loose_subentity_data_ctrl(self): """test creation of two linked entities @@ -227,10 +217,9 @@ self.assertIsInstance(forminfo['eidmap']['Y'], int) self.assertEqual(forminfo['error'].entity, 'X') self.assertEqual(forminfo['error'].errors, - {'login-subject': 'required field'}) + {'login-subject': 'required field'}) self.assertEqual(forminfo['values'], req.form) - def test_validation_error_dont_loose_subentity_data_repo(self): """test creation of two linked entities @@ -238,17 +227,18 @@ """ with self.admin_access.web_request() as req: # set Y before X to ensure both entities are edited, not only X - req.form = {'eid': ['Y', 'X'], '__maineid': 'X', - '__type:X': 'CWUser', '_cw_entity_fields:X': 'login-subject,upassword-subject', - # already existent user - 'login-subject:X': u'admin', - 'upassword-subject:X': u'admin', 'upassword-subject-confirm:X': u'admin', - '__type:Y': 'EmailAddress', '_cw_entity_fields:Y': 'address-subject', - 'address-subject:Y': u'bougloup@logilab.fr', - 'use_email-object:Y': 'X', - # necessary to get validation error handling - '__errorurl': 'view?vid=edition...', - } + req.form = { + 'eid': ['Y', 'X'], '__maineid': 'X', + '__type:X': 'CWUser', '_cw_entity_fields:X': 'login-subject,upassword-subject', + # already existent user + 'login-subject:X': u'admin', + 'upassword-subject:X': u'admin', 'upassword-subject-confirm:X': u'admin', + '__type:Y': 'EmailAddress', '_cw_entity_fields:Y': 'address-subject', + 'address-subject:Y': u'bougloup@logilab.fr', + 'use_email-object:Y': 'X', + # necessary to get validation error handling + '__errorurl': 'view?vid=edition...', + } path, params = self.expect_redirect_handle_request(req, 'edit') forminfo = req.session.data['view?vid=edition...'] self.assertEqual(set(forminfo['eidmap']), set('XY')) @@ -256,7 +246,7 @@ self.assertIsInstance(forminfo['eidmap']['Y'], int) self.assertEqual(forminfo['error'].entity, forminfo['eidmap']['X']) self.assertEqual(forminfo['error'].errors, - {'login-subject': u'the value "admin" is already used, use another one'}) + {'login-subject': u'the value "admin" is already used, use another one'}) self.assertEqual(forminfo['values'], req.form) def _edit_parent(self, dir_eid, parent_eid, role='subject', @@ -515,13 +505,15 @@ def test_ajax_view_raise_arbitrary_error(self): class ErrorAjaxView(view.View): __regid__ = 'test.ajax.error' + def call(self): raise Exception('whatever') + with self.temporary_appobjects(ErrorAjaxView): with real_error_handling(self.app) as app: with self.admin_access.web_request(vid='test.ajax.error') as req: req.ajax_request = True - page = app.handle_request(req, '') + app.handle_request(req, '') self.assertEqual(http_client.INTERNAL_SERVER_ERROR, req.status_out) @@ -580,12 +572,12 @@ self.assertAuthFailure(req) try: form = self.app.handle_request(req, 'login') - except Redirect as redir: + except Redirect: self.fail('anonymous user should get login form') clear_cache(req, 'get_authorization') self.assertIn(b'__login', form) self.assertIn(b'__password', form) - self.assertFalse(req.cnx) # Mock cnx are False + self.assertFalse(req.cnx) # Mock cnx are False req.form['__login'] = self.admlogin req.form['__password'] = self.admpassword self.assertAuthSuccess(req, origsession) @@ -599,13 +591,7 @@ cnx.execute('INSERT EmailAddress X: X address %(address)s, U primary_email X ' 'WHERE U login %(login)s', {'address': address, 'login': login}) cnx.commit() - # # option allow-email-login not set req, origsession = self.init_authentication('cookie') - # req.form['__login'] = address - # req.form['__password'] = self.admpassword - # self.assertAuthFailure(req) - # option allow-email-login set - #origsession.login = address self.set_option('allow-email-login', True) req.form['__login'] = address req.form['__password'] = self.admpassword diff -r c1eb5a676c80 -r d034791621ad cubicweb/web/test/unittest_formfields.py --- a/cubicweb/web/test/unittest_formfields.py Tue Jan 12 14:31:14 2016 +0100 +++ b/cubicweb/web/test/unittest_formfields.py Wed Feb 17 10:02:03 2016 +0100 @@ -1,4 +1,4 @@ -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of CubicWeb. @@ -24,11 +24,10 @@ import cubicweb from cubicweb.devtools import TestServerConfiguration from cubicweb.devtools.testlib import CubicWebTC -from cubicweb.web.formwidgets import PasswordInput, TextArea, Select, Radio +from cubicweb.web.formwidgets import PasswordInput, Select, Radio from cubicweb.web.formfields import * from cubicweb.web.views.forms import EntityFieldsForm, FieldsForm -from cubes.file.entities import File def setUpModule(*args): global schema @@ -36,6 +35,7 @@ config.bootstrap_cubes() schema = config.load_schema() + class GuessFieldTC(CubicWebTC): def test_state_fields(self): @@ -44,12 +44,6 @@ self.assertIsInstance(title_field, StringField) self.assertEqual(title_field.required, True) -# synopsis_field = guess_field(schema['State'], schema['synopsis']) -# self.assertIsInstance(synopsis_field, StringField) -# self.assertIsInstance(synopsis_field.widget, TextArea) -# self.assertEqual(synopsis_field.required, False) -# self.assertEqual(synopsis_field.help, 'an abstract for this state') - with self.admin_access.web_request() as req: description_field = guess_field(schema['State'], schema['description'], req=req) self.assertIsInstance(description_field, RichTextField) @@ -65,11 +59,6 @@ self.assertEqual(description_format_field.internationalizable, True) self.assertEqual(description_format_field.sort, True) -# wikiid_field = guess_field(schema['State'], schema['wikiid']) -# self.assertIsInstance(wikiid_field, StringField) -# self.assertEqual(wikiid_field.required, False) - - def test_cwuser_fields(self): with self.admin_access.web_request() as req: upassword_field = guess_field(schema['CWUser'], schema['upassword'], req=req) @@ -78,7 +67,8 @@ self.assertEqual(upassword_field.required, True) with self.admin_access.web_request() as req: - last_login_time_field = guess_field(schema['CWUser'], schema['last_login_time'], req=req) + last_login_time_field = guess_field(schema['CWUser'], schema['last_login_time'], + req=req) self.assertIsInstance(last_login_time_field, DateTimeField) self.assertEqual(last_login_time_field.required, False) @@ -95,15 +85,7 @@ self.assertEqual(owned_by_field.required, False) self.assertEqual(owned_by_field.role, 'object') - def test_file_fields(self): - # data_format_field = guess_field(schema['File'], schema['data_format']) - # self.assertEqual(data_format_field, None) - # data_encoding_field = guess_field(schema['File'], schema['data_encoding']) - # self.assertEqual(data_encoding_field, None) - # data_name_field = guess_field(schema['File'], schema['data_name']) - # self.assertEqual(data_name_field, None) - with self.admin_access.web_request() as req: data_field = guess_field(schema['File'], schema['data'], req=req) self.assertIsInstance(data_field, FileField) @@ -117,11 +99,10 @@ salesterm_field = guess_field(schema['Salesterm'], schema['reason'], req=req) constraints = schema['reason'].rdef('Salesterm', 'String').constraints self.assertEqual([c.__class__ for c in constraints], - [SizeConstraint, StaticVocabularyConstraint]) + [SizeConstraint, StaticVocabularyConstraint]) self.assertIsInstance(salesterm_field, StringField) self.assertIsInstance(salesterm_field.widget, Select) - def test_bool_field_base(self): with self.admin_access.web_request() as req: field = guess_field(schema['CWAttribute'], schema['indexed'], req=req) @@ -129,7 +110,7 @@ self.assertEqual(field.required, False) self.assertIsInstance(field.widget, Radio) self.assertEqual(field.vocabulary(mock(_cw=mock(_=cubicweb._))), - [(u'yes', '1'), (u'no', '')]) + [(u'yes', '1'), (u'no', '')]) def test_bool_field_explicit_choices(self): with self.admin_access.web_request() as req: @@ -137,7 +118,7 @@ choices=[(u'maybe', '1'), (u'no', '')], req=req) self.assertIsInstance(field.widget, Radio) self.assertEqual(field.vocabulary(mock(req=mock(_=cubicweb._))), - [(u'maybe', '1'), (u'no', '')]) + [(u'maybe', '1'), (u'no', '')]) class MoreFieldsTC(CubicWebTC): @@ -152,11 +133,11 @@ self.assertEqual(description_format_field.sort, True) # unlike below, initial is bound to form.form_field_format self.assertEqual(description_format_field.value(form), 'text/plain') - req.cnx.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"') + req.cnx.create_entity('CWProperty', pkey=u"ui.default-text-format", value=u"text/rest", + for_user=req.user.eid) req.cnx.commit() self.assertEqual(description_format_field.value(form), 'text/rest') - def test_property_key_field(self): from cubicweb.web.views.cwproperties import PropertyKeyField with self.admin_access.web_request() as req: @@ -171,15 +152,17 @@ class CompoundFieldTC(CubicWebTC): def test_multipart(self): - """Ensures that compound forms have needs_multipart set if their - children require it""" + """Ensures that compound forms have needs_multipart set if their children require it""" class AForm(FieldsForm): comp = CompoundField([IntField(), StringField()]) + with self.admin_access.web_request() as req: aform = AForm(req, None) self.assertFalse(aform.needs_multipart) + class MForm(FieldsForm): comp = CompoundField([IntField(), FileField()]) + with self.admin_access.web_request() as req: mform = MForm(req, None) self.assertTrue(mform.needs_multipart) @@ -188,12 +171,12 @@ class UtilsTC(TestCase): def test_vocab_sort(self): self.assertEqual(vocab_sort([('Z', 1), ('A', 2), - ('Group 1', None), ('Y', 3), ('B', 4), - ('Group 2', None), ('X', 5), ('C', 6)]), - [('A', 2), ('Z', 1), - ('Group 1', None), ('B', 4), ('Y', 3), - ('Group 2', None), ('C', 6), ('X', 5)] - ) + ('Group 1', None), ('Y', 3), ('B', 4), + ('Group 2', None), ('X', 5), ('C', 6)]), + [('A', 2), ('Z', 1), + ('Group 1', None), ('B', 4), ('Y', 3), + ('Group 2', None), ('C', 6), ('X', 5)]) + if __name__ == '__main__': unittest_main() diff -r c1eb5a676c80 -r d034791621ad cubicweb/web/test/unittest_views_editforms.py --- a/cubicweb/web/test/unittest_views_editforms.py Tue Jan 12 14:31:14 2016 +0100 +++ b/cubicweb/web/test/unittest_views_editforms.py Wed Feb 17 10:02:03 2016 +0100 @@ -28,6 +28,7 @@ AFFK = uicfg.autoform_field_kwargs AFS = uicfg.autoform_section + def rbc(entity, formtype, section): if section in ('attributes', 'metadata', 'hidden'): permission = 'update'