Skip to content

Commit

Permalink
Input Wrapping revamp
Browse files Browse the repository at this point in the history
* All input is now put into a `FormInput` class which represents all input
* Vastly simplified the most common use case of processing input.
* Abstracted out resolving defaults to kill boilerplate.
* JSON input is now first-class input, not needing hackery.
* Rename 'obj' to 'model' for clarity
  • Loading branch information
James Crasta committed Mar 29, 2015
1 parent f33acff commit ba37c21
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 135 deletions.
16 changes: 8 additions & 8 deletions tests/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ def test_str_coerce(self):
def test_unicode_coerce(self):
self.assertEqual(text_type(self.field), self.field())

def test_process_formdata(self):
Field.process_formdata(self.field, [42])
self.assertEqual(self.field.data, 42)

def test_meta_attribute(self):
# Can we pass in meta via _form?
form = self.F()
Expand Down Expand Up @@ -329,10 +325,12 @@ def test(self):
)

def test_text_coercion(self):
# Regression test for text coercsion scenarios where the value is a boolean.
coerce_func = lambda x: False if x == 'False' else bool(x)
# Regression test for text coercion scenarios where the value is a boolean.
def coerce_func(x):
return False if x == 'False' else bool(x)
F = make_form(a=RadioField(choices=[(True, 'yes'), (False, 'no')], coerce=coerce_func))
form = F()
self.assertEqual(form.a.data, False)
self.assertEqual(
form.a(),
'''<ul id="a">'''
Expand Down Expand Up @@ -709,11 +707,13 @@ def test_enclosed_subform(self):

# Test failure on populate
obj2 = AttrDict(a=42)
self.assertRaises(TypeError, form.populate_obj, obj2)
form.populate_obj(obj2)
self.assertEqual(len(obj2.a), 2)

def test_entry_management(self):
F = make_form(a=FieldList(self.t))
a = F(a=['hello', 'bye']).a
self.assertEqual(a.data, ['hello', 'bye'])
self.assertEqual(a.pop_entry().name, 'a-1')
self.assertEqual(a.data, ['hello'])
a.append_entry('orange')
Expand All @@ -727,7 +727,7 @@ def test_min_max_entries(self):
F = make_form(a=FieldList(self.t, min_entries=1, max_entries=3))
a = F().a
self.assertEqual(len(a), 1)
self.assertEqual(a[0].data, None)
self.assertEqual(a[0].data, '')
big_input = ['foo', 'flaf', 'bar', 'baz']
self.assertRaises(AssertionError, F, a=big_input)
pdata = DummyPostData(('a-%d' % i, v) for i, v in enumerate(big_input))
Expand Down
2 changes: 1 addition & 1 deletion tests/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_data_arg(self):
form = self.F(data=data)
self.assertEqual(form.test.data, 'foo')
form = self.F(data=data, test='bar')
self.assertEqual(form.test.data, 'bar')
self.assertEqual(form.test.data, 'foo')


class MetaTest(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/webob_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def setUp(self):

def test_automatic_wrapping(self):
def _check(formdata):
self.assertTrue(isinstance(formdata, WebobInputWrapper))
self.assertTrue(isinstance(formdata.form_input, WebobInputWrapper))

form = BaseForm({'a': SneakyField(_check)})
form.process(self.filled_mdict)
Expand Down
Loading

0 comments on commit ba37c21

Please sign in to comment.