1from __future__ import unicode_literals 2 3from datetime import datetime, date 4from unittest import TestCase 5 6from wtforms.form import Form 7from wtforms.ext.dateutil.fields import DateTimeField, DateField 8from tests.common import DummyPostData 9 10 11class DateutilTest(TestCase): 12 class F(Form): 13 a = DateTimeField() 14 b = DateField(default=lambda: date(2004, 9, 12)) 15 c = DateField(parse_kwargs=dict(yearfirst=True, dayfirst=False)) 16 17 def test_form_input(self): 18 f = self.F(DummyPostData(a='2008/09/12 4:17 PM', b='04/05/06', c='04/05/06')) 19 self.assertEqual(f.a.data, datetime(2008, 9, 12, 16, 17)) 20 self.assertEqual(f.a._value(), '2008/09/12 4:17 PM') 21 self.assertEqual(f.b.data, date(2006, 4, 5)) 22 self.assertEqual(f.c.data, date(2004, 5, 6)) 23 self.assertTrue(f.validate()) 24 f = self.F(DummyPostData(a='Grok Grarg Rawr')) 25 self.assertFalse(f.validate()) 26 27 def test_blank_input(self): 28 f = self.F(DummyPostData(a='', b='')) 29 self.assertEqual(f.a.data, None) 30 self.assertEqual(f.b.data, None) 31 self.assertFalse(f.validate()) 32 33 def test_defaults_display(self): 34 f = self.F(a=datetime(2001, 11, 15)) 35 self.assertEqual(f.a.data, datetime(2001, 11, 15)) 36 self.assertEqual(f.a._value(), '2001-11-15 00:00') 37 self.assertEqual(f.b.data, date(2004, 9, 12)) 38 self.assertEqual(f.b._value(), '2004-09-12') 39 self.assertEqual(f.c.data, None) 40 self.assertTrue(f.validate()) 41 42 def test_render(self): 43 f = self.F() 44 self.assertEqual(f.b(), r'<input id="b" name="b" type="text" value="2004-09-12">') 45