def test_edit_form(self): from ptah.manage.settings import SettingsModule, EditForm ptah.register_settings( 'test', ptah.form.TextField('node1', default='test1', required=False), ptah.form.TextField('node2', default='test2', required=False), title = 'Test settings', ttw = True, ttw_skip_fields = ('node2',)) self.init_ptah() mod = SettingsModule(None, self.request) grp = mod['test'] settings = grp.group form = EditForm(grp, self.request) self.assertEqual(form.label, settings.__title__) self.assertEqual(form.description, settings.__description__) self.assertIs(form.form_content(), settings) fields = form.fields self.assertIn('node1', fields) self.assertNotIn('node2', fields) res = form.back_handler() self.assertIsInstance(res, HTTPFound) self.assertEqual(res.headers['location'], '..') form.update_form() form.modify_handler() self.assertIn('Settings have been modified.', self.request.render_messages())
def test_form_content_from_update(self): from ptah.form.form import Form request = DummyRequest() form = Form(None, request) form_content = {'test': 'test1'} form.update_form(form_content) self.assertEqual(form.form_content(), form_content)
def test_form_extract_missing(self): """ Extract form data, composite field no data """ fields=(ptah.form.TextField('firstname', missing='name'), ptah.form.TextField('lastname')) field = ptah.form.CompositeField('test', fields=fields) form = ptah.form.Form(object(), self.request, fields=(field,)) form.update_form() data, errors = form.extract() self.assertEqual({'lastname': fields[1].missing, 'firstname': fields[0].missing}, data['test'])
def test_form_extract_missing(self): """ Extract form data, composite field no data """ fields = (ptah.form.TextField('firstname', missing='name'), ptah.form.TextField('lastname')) field = ptah.form.CompositeField('test', fields=fields) form = ptah.form.Form(object(), self.request, fields=(field, )) form.update_form() data, errors = form.extract() self.assertEqual( { 'lastname': fields[1].missing, 'firstname': fields[0].missing }, data['test'])
def test_edit_form(self): from ptah.manage.settings import SettingsModule, EditForm ptah.register_settings('test', ptah.form.TextField('node1', default='test1', required=False), ptah.form.TextField('node2', default='test2', required=False), title='Test settings', ttw=True, ttw_skip_fields=('node2', )) self.init_ptah() mod = SettingsModule(None, self.request) grp = mod['test'] settings = grp.group form = EditForm(grp, self.request) self.assertEqual(form.label, settings.__title__) self.assertEqual(form.description, settings.__description__) self.assertIs(form.form_content(), settings) fields = form.fields self.assertIn('node1', fields) self.assertNotIn('node2', fields) res = form.back_handler() self.assertIsInstance(res, HTTPFound) self.assertEqual(res.headers['location'], '..') form.update_form() form.modify_handler() self.assertIn('Settings have been modified.', self.request.render_messages())
def test_update_form(self): import ptah.form request = self.make_request() class CustomForm(ptah.form.Form): fields = ptah.form.Fieldset(ptah.form.TextField('test')) def update(self): return {1: 'test'} form = CustomForm(object(), request) res = form.update_form() self.assertEqual(res, {1: 'test'})
def test_update_form_action_return_dict_combine_with_update(self): import ptah.form request = self.make_request(POST={'form.buttons.test': 'test'}) class CustomForm(ptah.form.Form): fields = ptah.form.Fieldset(ptah.form.TextField('test')) def update(self): return {0: '0'} @ptah.form.button('test') def test_handler(self): return {1: 'test'} form = CustomForm(object(), request) res = form.update_form() self.assertEqual(res, {0: '0', 1: 'test'})