James Crasta avatar James Crasta committed c56afe4

Improve test coverage: >99% coverage in WTForms core, 96% including wtforms ext.

Comments (0)

Files changed (4)

     def test_formdata(self):
         form = self.F1(DummyPostData({'a-a':['moo']}))
         self.assertEqual(form.a.form.a.name, 'a-a')
-        self.assertEqual(form.a.form.a.data, 'moo')
-        self.assertEqual(form.a.form.b.data, '')
+        self.assertEqual(form.a['a'].data, 'moo')
+        self.assertEqual(form.a['b'].data, '')
     def test_iteration(self):
         form = C()
         self.assertRaises(TypeError, form.validate)
+    def test_populate_missing_obj(self):
+        obj = AttrDict(a=None)
+        obj2 = AttrDict(a=AttrDict(a='mmm'))
+        form = self.F1()
+        self.assertRaises(TypeError, form.populate_obj, obj)
+        form.populate_obj(obj2)
 class FieldListTest(TestCase):
     t = TextField(validators=[validators.Required()])
     def test_form(self):
-        F = make_form(a = FieldList(self.t))
+        F = make_form(a=FieldList(self.t))
         data = ['foo', 'hi', 'rawr']
         a = F(a=data).a
         self.assertEqual(a.entries[1].data, 'hi')
         form = F(pdata, a=data)
         self.assertEqual(len(form.a.entries), 2)
         self.assertEqual(form.a.data, ['a', 'b'])
+        self.assertEqual(list(iter(form.a)), list(form.a.entries))
     def test_enclosed_subform(self):
         make_inner = lambda: AttrDict(a=None)
         self.assertEqual(obj.a[0].a, 'foo')
         self.assertEqual(obj.a[1].a, 'bar')
+        # Test failure on populate
+        obj2 = AttrDict(a=42)
+        self.assertRaises(TypeError, form.populate_obj, obj2)
     def test_entry_management(self):
         F = make_form(a = FieldList(self.t))
         a = F(a=['hello', 'bye']).a
         assert not form.validate()
         self.assertEqual(form.a.errors, [['This field is required.']])
+    def test_no_filters(self):
+        my_filter = lambda x: x
+        self.assertRaises(TypeError, FieldList, self.t, filters=[my_filter], _form=Form(), _name='foo')
 class MyCustomField(TextField):
     def process_data(self, data):
         self.assertEqual([x.name for x in MyForm()], ['strawberry', 'kiwi', 'apple'])
         MyForm.strawberry = TextField()
         self.assertEqual([x.name for x in MyForm()], ['kiwi', 'apple', 'strawberry'])
-        # Ensure sort is stable: two fields with the same creation counter 
+        # Ensure sort is stable: two fields with the same creation counter
         # should be subsequently sorted by name.
         MyForm.cherry = MyForm.kiwi
         self.assertEqual([x.name for x in MyForm()], ['cherry', 'kiwi', 'apple', 'strawberry'])


     except ValidationError as e:
         return e.args[0]
+def grab_stop_message(callable, form, field):
+    try:
+        callable(form, field)
+    except StopValidation as e:
+        return e.args[0]
 class ValidatorsTest(TestCase):
     def setUp(self):
         self.form = DummyForm()
         for good_address in ('::1', 'dead:beef:0:0:0:0:42:1', 'abcd:ef::42:1'):
             self.assertEqual(ip_address(ipv6=True)(self.form, DummyField(good_address)), None)
+        #Test ValueError on ipv6=False and ipv4=False
+        self.assertRaises(ValueError, ip_address, ipv4=False, ipv6=False)
     def test_mac_address(self):
                                        DummyField('01:23:45:67:ab:CD')), None)
         self.assertTrue('between 2 and 5' in grab(min=2, max=5))
     def test_required(self):
+        # Make sure we stop the validation chain
         self.assertEqual(required()(self.form, DummyField('foobar')), None)
         self.assertRaises(StopValidation, required(), self.form, DummyField(''))
         self.assertRaises(StopValidation, required(), self.form, DummyField(' '))
         self.assertEqual(required().field_flags, ('required', ))
+        # Make sure we clobber errors
         f = DummyField('', ['Invalid Integer Value'])
         self.assertEqual(len(f.errors), 1)
         self.assertRaises(StopValidation, required(), self.form, f)
         self.assertEqual(len(f.errors), 0)
+        # Check message and custom message
+        grab = lambda **k: grab_stop_message(required(**k), self.form, DummyField(''))
+        self.assertEqual(grab(), 'This field is required.')
+        self.assertEqual(grab(message='foo'), 'foo')
     def test_input_required(self):
         self.assertEqual(input_required()(self.form, DummyField('foobar', raw_data=['foobar'])), None)
         self.assertRaises(StopValidation, input_required(), self.form, DummyField('', raw_data=['']))
         self.assertEqual(input_required().field_flags, ('required', ))
+        # Check message and custom message
+        grab = lambda **k: grab_stop_message(input_required(**k), self.form, DummyField('', raw_data=['']))
+        self.assertEqual(grab(), 'This field is required.')
+        self.assertEqual(grab(message='foo'), 'foo')
     def test_optional(self):
         self.assertEqual(optional()(self.form, DummyField('foobar', raw_data=['foobar'])), None)
         self.assertRaises(StopValidation, optional(), self.form, DummyField('', raw_data=['']))
         self.assertRaises(ValidationError, regexp(re.compile('^a')), self.form, DummyField('foo'))
         self.assertRaises(ValidationError, regexp(re.compile('^a')), self.form, DummyField(None))
+        # Check custom message
+        self.assertEqual(grab_error_message(regexp('^a', message='foo'), self.form, DummyField('f')), 'foo')
     def test_url(self):
         self.assertEqual(url()(self.form, DummyField('http://foobar.dk')), None)
         self.assertEqual(url()(self.form, DummyField('http://foobar.dk/')), None)
         if hasattr(fields, 'iteritems'):
             fields = fields.iteritems()
         elif hasattr(fields, 'items'):
+            # Python 3.x
             fields = fields.items()
         translations = self._get_translations()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.