2024-08-04 20:35:41 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
|
|
|
from unittest import TestCase
|
2024-08-10 21:07:38 -05:00
|
|
|
from unittest.mock import MagicMock, patch
|
2024-08-04 20:35:41 -05:00
|
|
|
|
|
|
|
import colander
|
|
|
|
import deform
|
|
|
|
from pyramid import testing
|
|
|
|
|
|
|
|
from wuttjamaican.conf import WuttaConfig
|
2024-08-12 21:17:08 -05:00
|
|
|
from wuttaweb.forms import base, widgets
|
2024-08-04 20:35:41 -05:00
|
|
|
from wuttaweb import helpers
|
|
|
|
|
|
|
|
|
|
|
|
class TestForm(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2024-08-05 19:21:58 -05:00
|
|
|
self.config = WuttaConfig(defaults={
|
2024-08-12 21:17:08 -05:00
|
|
|
'wutta.web.menus.handler_spec': 'tests.util:NullMenuHandler',
|
2024-08-05 19:21:58 -05:00
|
|
|
})
|
2024-08-11 16:52:13 -05:00
|
|
|
self.app = self.config.get_app()
|
2024-08-05 21:49:18 -05:00
|
|
|
self.request = testing.DummyRequest(wutta_config=self.config, use_oruga=False)
|
2024-08-04 20:35:41 -05:00
|
|
|
|
|
|
|
self.pyramid_config = testing.setUp(request=self.request, settings={
|
|
|
|
'mako.directories': ['wuttaweb:templates'],
|
|
|
|
'pyramid_deform.template_search_path': 'wuttaweb:templates/deform',
|
|
|
|
})
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
testing.tearDown()
|
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
def make_form(self, **kwargs):
|
|
|
|
return base.Form(self.request, **kwargs)
|
2024-08-04 20:35:41 -05:00
|
|
|
|
|
|
|
def make_schema(self):
|
|
|
|
schema = colander.Schema(children=[
|
|
|
|
colander.SchemaNode(colander.String(),
|
|
|
|
name='foo'),
|
|
|
|
colander.SchemaNode(colander.String(),
|
|
|
|
name='bar'),
|
|
|
|
])
|
|
|
|
return schema
|
|
|
|
|
|
|
|
def test_init_with_none(self):
|
|
|
|
form = self.make_form()
|
2024-08-12 21:17:08 -05:00
|
|
|
self.assertEqual(form.fields, [])
|
2024-08-04 20:35:41 -05:00
|
|
|
|
|
|
|
def test_init_with_fields(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.fields, ['foo', 'bar'])
|
|
|
|
|
|
|
|
def test_init_with_schema(self):
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
self.assertEqual(form.fields, ['foo', 'bar'])
|
|
|
|
|
|
|
|
def test_vue_tagname(self):
|
|
|
|
form = self.make_form()
|
|
|
|
self.assertEqual(form.vue_tagname, 'wutta-form')
|
|
|
|
|
|
|
|
def test_vue_component(self):
|
|
|
|
form = self.make_form()
|
|
|
|
self.assertEqual(form.vue_component, 'WuttaForm')
|
|
|
|
|
|
|
|
def test_contains(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertIn('foo', form)
|
|
|
|
self.assertNotIn('baz', form)
|
|
|
|
|
|
|
|
def test_iter(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
|
|
|
|
fields = list(iter(form))
|
|
|
|
self.assertEqual(fields, ['foo', 'bar'])
|
|
|
|
|
|
|
|
fields = []
|
|
|
|
for field in form:
|
|
|
|
fields.append(field)
|
|
|
|
self.assertEqual(fields, ['foo', 'bar'])
|
|
|
|
|
|
|
|
def test_set_fields(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.fields, ['foo', 'bar'])
|
|
|
|
form.set_fields(['baz'])
|
|
|
|
self.assertEqual(form.fields, ['baz'])
|
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
def test_remove(self):
|
|
|
|
form = self.make_form(fields=['one', 'two', 'three', 'four'])
|
|
|
|
self.assertEqual(form.fields, ['one', 'two', 'three', 'four'])
|
|
|
|
form.remove('two', 'three')
|
|
|
|
self.assertEqual(form.fields, ['one', 'four'])
|
|
|
|
|
|
|
|
def test_set_node(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.nodes, {})
|
|
|
|
|
|
|
|
# complete node
|
|
|
|
node = colander.SchemaNode(colander.Bool(), name='foo')
|
|
|
|
form.set_node('foo', node)
|
|
|
|
self.assertIs(form.nodes['foo'], node)
|
|
|
|
|
|
|
|
# type only
|
|
|
|
typ = colander.Bool()
|
|
|
|
form.set_node('foo', typ)
|
|
|
|
node = form.nodes['foo']
|
|
|
|
self.assertIsInstance(node, colander.SchemaNode)
|
|
|
|
self.assertIsInstance(node.typ, colander.Bool)
|
|
|
|
self.assertEqual(node.name, 'foo')
|
|
|
|
|
|
|
|
# schema is updated if already present
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIsNotNone(schema)
|
|
|
|
typ = colander.Date()
|
|
|
|
form.set_node('foo', typ)
|
|
|
|
node = form.nodes['foo']
|
|
|
|
self.assertIsInstance(node, colander.SchemaNode)
|
|
|
|
self.assertIsInstance(node.typ, colander.Date)
|
|
|
|
self.assertEqual(node.name, 'foo')
|
|
|
|
|
|
|
|
def test_set_widget(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.widgets, {})
|
|
|
|
|
|
|
|
# basic
|
|
|
|
widget = widgets.SelectWidget()
|
|
|
|
form.set_widget('foo', widget)
|
|
|
|
self.assertIs(form.widgets['foo'], widget)
|
|
|
|
|
|
|
|
# schema is updated if already present
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIsNotNone(schema)
|
|
|
|
self.assertIs(schema['foo'].widget, widget)
|
|
|
|
new_widget = widgets.TextInputWidget()
|
|
|
|
form.set_widget('foo', new_widget)
|
|
|
|
self.assertIs(form.widgets['foo'], new_widget)
|
|
|
|
self.assertIs(schema['foo'].widget, new_widget)
|
|
|
|
|
|
|
|
def test_set_validator(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.validators, {})
|
|
|
|
|
|
|
|
def validate1(node, value):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# basic
|
|
|
|
form.set_validator('foo', validate1)
|
|
|
|
self.assertIs(form.validators['foo'], validate1)
|
|
|
|
|
|
|
|
def validate2(node, value):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# schema is updated if already present
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIsNotNone(schema)
|
|
|
|
self.assertIs(schema['foo'].validator, validate1)
|
|
|
|
form.set_validator('foo', validate2)
|
|
|
|
self.assertIs(form.validators['foo'], validate2)
|
|
|
|
self.assertIs(schema['foo'].validator, validate2)
|
|
|
|
|
2024-08-04 20:35:41 -05:00
|
|
|
def test_get_schema(self):
|
2024-08-11 16:52:13 -05:00
|
|
|
model = self.app.model
|
2024-08-04 20:35:41 -05:00
|
|
|
form = self.make_form()
|
|
|
|
self.assertIsNone(form.schema)
|
|
|
|
|
|
|
|
# provided schema is returned
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
self.assertIs(form.schema, schema)
|
|
|
|
self.assertIs(form.get_schema(), schema)
|
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
# schema is auto-generated if fields provided
|
2024-08-04 20:35:41 -05:00
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
2024-08-07 19:47:24 -05:00
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertEqual(len(schema.children), 2)
|
|
|
|
self.assertEqual(schema['foo'].name, 'foo')
|
|
|
|
|
|
|
|
# but auto-generating without fields is not supported
|
|
|
|
form = self.make_form()
|
2024-08-04 20:35:41 -05:00
|
|
|
self.assertIsNone(form.schema)
|
|
|
|
self.assertRaises(NotImplementedError, form.get_schema)
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
# schema is auto-generated if model_class provided
|
|
|
|
form = self.make_form(model_class=model.Setting)
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertEqual(len(schema.children), 2)
|
|
|
|
self.assertIn('name', schema)
|
|
|
|
self.assertIn('value', schema)
|
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
# but node overrides are honored when auto-generating
|
|
|
|
form = self.make_form(model_class=model.Setting)
|
|
|
|
value_node = colander.SchemaNode(colander.Bool(), name='value')
|
|
|
|
form.set_node('value', value_node)
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIs(schema['value'], value_node)
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
# schema is auto-generated if model_instance provided
|
|
|
|
form = self.make_form(model_instance=model.Setting(name='uhoh'))
|
|
|
|
self.assertEqual(form.fields, ['name', 'value'])
|
|
|
|
self.assertIsNone(form.schema)
|
|
|
|
# nb. force method to get new fields
|
|
|
|
del form.fields
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertEqual(len(schema.children), 2)
|
|
|
|
self.assertIn('name', schema)
|
|
|
|
self.assertIn('value', schema)
|
|
|
|
|
2024-08-11 12:41:22 -05:00
|
|
|
# schema nodes are required by default
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIs(schema['foo'].missing, colander.required)
|
|
|
|
self.assertIs(schema['bar'].missing, colander.required)
|
|
|
|
|
|
|
|
# but fields can be marked *not* required
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
form.set_required('bar', False)
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIs(schema['foo'].missing, colander.required)
|
2024-08-12 21:17:08 -05:00
|
|
|
self.assertIs(schema['bar'].missing, colander.null)
|
|
|
|
|
|
|
|
# validator overrides are honored
|
|
|
|
def validate(node, value): pass
|
|
|
|
form = self.make_form(model_class=model.Setting)
|
|
|
|
form.set_validator('name', validate)
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIs(schema['name'].validator, validate)
|
|
|
|
|
|
|
|
# validator can be set for whole form
|
|
|
|
form = self.make_form(model_class=model.Setting)
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIsNone(schema.validator)
|
|
|
|
form = self.make_form(model_class=model.Setting)
|
|
|
|
form.set_validator(None, validate)
|
|
|
|
schema = form.get_schema()
|
|
|
|
self.assertIs(schema.validator, validate)
|
2024-08-11 12:41:22 -05:00
|
|
|
|
2024-08-04 20:35:41 -05:00
|
|
|
def test_get_deform(self):
|
2024-08-11 16:52:13 -05:00
|
|
|
model = self.app.model
|
2024-08-04 20:35:41 -05:00
|
|
|
schema = self.make_schema()
|
2024-08-07 19:47:24 -05:00
|
|
|
|
|
|
|
# basic
|
2024-08-04 20:35:41 -05:00
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
self.assertFalse(hasattr(form, 'deform_form'))
|
|
|
|
dform = form.get_deform()
|
|
|
|
self.assertIsInstance(dform, deform.Form)
|
|
|
|
self.assertIs(form.deform_form, dform)
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
# with model instance as dict
|
2024-08-07 19:47:24 -05:00
|
|
|
myobj = {'foo': 'one', 'bar': 'two'}
|
|
|
|
form = self.make_form(schema=schema, model_instance=myobj)
|
|
|
|
dform = form.get_deform()
|
|
|
|
self.assertEqual(dform.cstruct, myobj)
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
# with sqlalchemy model instance
|
|
|
|
myobj = model.Setting(name='foo', value='bar')
|
|
|
|
form = self.make_form(model_instance=myobj)
|
|
|
|
dform = form.get_deform()
|
|
|
|
self.assertEqual(dform.cstruct, {'name': 'foo', 'value': 'bar'})
|
|
|
|
|
|
|
|
# sqlalchemy instance with null value
|
|
|
|
myobj = model.Setting(name='foo', value=None)
|
|
|
|
form = self.make_form(model_instance=myobj)
|
|
|
|
dform = form.get_deform()
|
|
|
|
self.assertEqual(dform.cstruct, {'name': 'foo', 'value': colander.null})
|
|
|
|
|
2024-08-10 21:07:38 -05:00
|
|
|
def test_get_cancel_url(self):
|
|
|
|
|
|
|
|
# is referrer by default
|
|
|
|
form = self.make_form()
|
|
|
|
self.request.get_referrer = MagicMock(return_value='/cancel-default')
|
|
|
|
self.assertEqual(form.get_cancel_url(), '/cancel-default')
|
|
|
|
del self.request.get_referrer
|
|
|
|
|
|
|
|
# or can be static URL
|
|
|
|
form = self.make_form(cancel_url='/cancel-static')
|
|
|
|
self.assertEqual(form.get_cancel_url(), '/cancel-static')
|
|
|
|
|
|
|
|
# or can be fallback URL (nb. 'NOPE' indicates no referrer)
|
|
|
|
form = self.make_form(cancel_url_fallback='/cancel-fallback')
|
|
|
|
self.request.get_referrer = MagicMock(return_value='NOPE')
|
|
|
|
self.assertEqual(form.get_cancel_url(), '/cancel-fallback')
|
|
|
|
del self.request.get_referrer
|
|
|
|
|
|
|
|
# or can be referrer fallback, i.e. home page
|
|
|
|
form = self.make_form()
|
|
|
|
def get_referrer(default=None):
|
|
|
|
if default == 'NOPE':
|
|
|
|
return 'NOPE'
|
|
|
|
return '/home-page'
|
|
|
|
self.request.get_referrer = get_referrer
|
|
|
|
self.assertEqual(form.get_cancel_url(), '/home-page')
|
|
|
|
del self.request.get_referrer
|
|
|
|
|
2024-08-04 20:35:41 -05:00
|
|
|
def test_get_label(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.get_label('foo'), "Foo")
|
|
|
|
form.set_label('foo', "Baz")
|
|
|
|
self.assertEqual(form.get_label('foo'), "Baz")
|
|
|
|
|
|
|
|
def test_set_label(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.get_label('foo'), "Foo")
|
|
|
|
form.set_label('foo', "Baz")
|
|
|
|
self.assertEqual(form.get_label('foo'), "Baz")
|
|
|
|
|
|
|
|
# schema should be updated when setting label
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
form.set_label('foo', "Woohoo")
|
|
|
|
self.assertEqual(form.get_label('foo'), "Woohoo")
|
|
|
|
self.assertEqual(schema['foo'].title, "Woohoo")
|
|
|
|
|
2024-08-10 21:07:38 -05:00
|
|
|
def test_readonly_fields(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.readonly_fields, set())
|
|
|
|
self.assertFalse(form.is_readonly('foo'))
|
|
|
|
|
|
|
|
form.set_readonly('foo')
|
|
|
|
self.assertEqual(form.readonly_fields, {'foo'})
|
|
|
|
self.assertTrue(form.is_readonly('foo'))
|
|
|
|
self.assertFalse(form.is_readonly('bar'))
|
|
|
|
|
|
|
|
form.set_readonly('bar')
|
|
|
|
self.assertEqual(form.readonly_fields, {'foo', 'bar'})
|
|
|
|
self.assertTrue(form.is_readonly('foo'))
|
|
|
|
self.assertTrue(form.is_readonly('bar'))
|
|
|
|
|
|
|
|
form.set_readonly('foo', False)
|
|
|
|
self.assertEqual(form.readonly_fields, {'bar'})
|
|
|
|
self.assertFalse(form.is_readonly('foo'))
|
|
|
|
self.assertTrue(form.is_readonly('bar'))
|
|
|
|
|
2024-08-11 12:41:22 -05:00
|
|
|
def test_required_fields(self):
|
|
|
|
form = self.make_form(fields=['foo', 'bar'])
|
|
|
|
self.assertEqual(form.required_fields, {})
|
|
|
|
self.assertIsNone(form.is_required('foo'))
|
|
|
|
|
|
|
|
form.set_required('foo')
|
|
|
|
self.assertEqual(form.required_fields, {'foo': True})
|
|
|
|
self.assertTrue(form.is_required('foo'))
|
|
|
|
self.assertIsNone(form.is_required('bar'))
|
|
|
|
|
|
|
|
form.set_required('bar')
|
|
|
|
self.assertEqual(form.required_fields, {'foo': True, 'bar': True})
|
|
|
|
self.assertTrue(form.is_required('foo'))
|
|
|
|
self.assertTrue(form.is_required('bar'))
|
|
|
|
|
|
|
|
form.set_required('foo', False)
|
|
|
|
self.assertEqual(form.required_fields, {'foo': False, 'bar': True})
|
|
|
|
self.assertFalse(form.is_required('foo'))
|
|
|
|
self.assertTrue(form.is_required('bar'))
|
|
|
|
|
2024-08-04 20:35:41 -05:00
|
|
|
def test_render_vue_tag(self):
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
html = form.render_vue_tag()
|
|
|
|
self.assertEqual(html, '<wutta-form></wutta-form>')
|
|
|
|
|
|
|
|
def test_render_vue_template(self):
|
|
|
|
self.pyramid_config.include('pyramid_mako')
|
|
|
|
self.pyramid_config.add_subscriber('wuttaweb.subscribers.before_render',
|
|
|
|
'pyramid.events.BeforeRender')
|
|
|
|
|
|
|
|
# form button is disabled on @submit by default
|
|
|
|
schema = self.make_schema()
|
2024-08-10 21:07:38 -05:00
|
|
|
form = self.make_form(schema=schema, cancel_url='/')
|
2024-08-04 20:35:41 -05:00
|
|
|
html = form.render_vue_template()
|
|
|
|
self.assertIn('<script type="text/x-template" id="wutta-form-template">', html)
|
|
|
|
self.assertIn('@submit', html)
|
|
|
|
|
|
|
|
# but not if form is configured otherwise
|
2024-08-10 21:07:38 -05:00
|
|
|
form = self.make_form(schema=schema, auto_disable_submit=False, cancel_url='/')
|
2024-08-04 20:35:41 -05:00
|
|
|
html = form.render_vue_template()
|
|
|
|
self.assertIn('<script type="text/x-template" id="wutta-form-template">', html)
|
|
|
|
self.assertNotIn('@submit', html)
|
|
|
|
|
|
|
|
def test_render_vue_field(self):
|
|
|
|
self.pyramid_config.include('pyramid_deform')
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
2024-08-05 11:45:00 -05:00
|
|
|
dform = form.get_deform()
|
|
|
|
|
|
|
|
# typical
|
2024-08-04 20:35:41 -05:00
|
|
|
html = form.render_vue_field('foo')
|
|
|
|
self.assertIn('<b-field :horizontal="true" label="Foo">', html)
|
|
|
|
self.assertIn('<b-input name="foo"', html)
|
2024-08-05 11:45:00 -05:00
|
|
|
# nb. no error message
|
|
|
|
self.assertNotIn('message', html)
|
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
# readonly
|
|
|
|
html = form.render_vue_field('foo', readonly=True)
|
|
|
|
self.assertIn('<b-field :horizontal="true" label="Foo">', html)
|
|
|
|
self.assertNotIn('<b-input name="foo"', html)
|
|
|
|
# nb. no error message
|
|
|
|
self.assertNotIn('message', html)
|
|
|
|
|
2024-08-05 11:45:00 -05:00
|
|
|
# with single "static" error
|
|
|
|
dform['foo'].error = MagicMock(msg="something is wrong")
|
|
|
|
html = form.render_vue_field('foo')
|
|
|
|
self.assertIn(' message="something is wrong"', html)
|
|
|
|
|
|
|
|
# with single "dynamic" error
|
|
|
|
dform['foo'].error = MagicMock(msg="`something is wrong`")
|
|
|
|
html = form.render_vue_field('foo')
|
|
|
|
self.assertIn(':message="`something is wrong`"', html)
|
|
|
|
|
2024-08-10 21:07:38 -05:00
|
|
|
# add another field, but not to deform, so it should still
|
|
|
|
# display but with no widget
|
|
|
|
form.fields.append('zanzibar')
|
|
|
|
html = form.render_vue_field('zanzibar')
|
|
|
|
self.assertIn('<b-field :horizontal="true" label="Zanzibar">', html)
|
|
|
|
self.assertNotIn('<b-input', html)
|
|
|
|
# nb. no error message
|
|
|
|
self.assertNotIn('message', html)
|
|
|
|
|
|
|
|
# try that once more but with a model record instance
|
|
|
|
with patch.object(form, 'model_instance', new={'zanzibar': 'omgwtfbbq'}):
|
|
|
|
html = form.render_vue_field('zanzibar')
|
|
|
|
self.assertIn('<b-field', html)
|
|
|
|
self.assertIn('label="Zanzibar"', html)
|
|
|
|
self.assertNotIn('<b-input', html)
|
|
|
|
self.assertIn('>omgwtfbbq<', html)
|
|
|
|
# nb. no error message
|
|
|
|
self.assertNotIn('message', html)
|
|
|
|
|
2024-08-05 11:45:00 -05:00
|
|
|
def test_get_field_errors(self):
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
dform = form.get_deform()
|
|
|
|
|
|
|
|
# no error
|
|
|
|
errors = form.get_field_errors('foo')
|
|
|
|
self.assertEqual(len(errors), 0)
|
|
|
|
|
|
|
|
# simple error
|
|
|
|
dform['foo'].error = MagicMock(msg="something is wrong")
|
|
|
|
errors = form.get_field_errors('foo')
|
|
|
|
self.assertEqual(len(errors), 1)
|
|
|
|
self.assertEqual(errors[0], "something is wrong")
|
2024-08-04 20:35:41 -05:00
|
|
|
|
|
|
|
def test_validate(self):
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
self.assertFalse(hasattr(form, 'validated'))
|
|
|
|
|
|
|
|
# will not validate unless request is POST
|
|
|
|
self.request.POST = {'foo': 'blarg', 'bar': 'baz'}
|
|
|
|
self.request.method = 'GET'
|
|
|
|
self.assertFalse(form.validate())
|
|
|
|
self.request.method = 'POST'
|
|
|
|
data = form.validate()
|
|
|
|
self.assertEqual(data, {'foo': 'blarg', 'bar': 'baz'})
|
|
|
|
|
2024-08-11 09:56:47 -05:00
|
|
|
# validating a second time updates form.validated
|
2024-08-04 20:35:41 -05:00
|
|
|
self.request.POST = {'foo': 'BLARG', 'bar': 'BAZ'}
|
|
|
|
data = form.validate()
|
|
|
|
self.assertEqual(data, {'foo': 'BLARG', 'bar': 'BAZ'})
|
|
|
|
self.assertIs(form.validated, data)
|
|
|
|
|
|
|
|
# bad data does not validate
|
|
|
|
self.request.POST = {'foo': 42, 'bar': None}
|
|
|
|
self.assertFalse(form.validate())
|
|
|
|
dform = form.get_deform()
|
|
|
|
self.assertEqual(len(dform.error.children), 2)
|
|
|
|
self.assertEqual(dform['foo'].errormsg, "Pstruct is not a string")
|
2024-08-11 09:56:47 -05:00
|
|
|
|
|
|
|
# when a form has readonly fields, validating it will *remove*
|
|
|
|
# those fields from deform/schema as well as final data dict
|
|
|
|
schema = self.make_schema()
|
|
|
|
form = self.make_form(schema=schema)
|
|
|
|
form.set_readonly('foo')
|
|
|
|
self.request.POST = {'foo': 'one', 'bar': 'two'}
|
|
|
|
data = form.validate()
|
|
|
|
self.assertEqual(data, {'bar': 'two'})
|
|
|
|
dform = form.get_deform()
|
|
|
|
self.assertNotIn('foo', schema)
|
|
|
|
self.assertNotIn('foo', dform)
|
|
|
|
self.assertIn('bar', schema)
|
|
|
|
self.assertIn('bar', dform)
|