PageRenderTime 149ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/forms/tests/extra.py

https://code.google.com/p/mango-py/
Python | 627 lines | 607 code | 9 blank | 11 comment | 1 complexity | 79da945cfc085847e84f2c29c7223252 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import time
  4. from django.conf import settings
  5. from django.forms import *
  6. from django.forms.extras import SelectDateWidget
  7. from django.forms.util import ErrorList
  8. from django.utils import translation
  9. from django.utils import unittest
  10. from django.utils.encoding import force_unicode
  11. from django.utils.encoding import smart_unicode
  12. from error_messages import AssertFormErrorsMixin
  13. class GetDate(Form):
  14. mydate = DateField(widget=SelectDateWidget)
  15. class FormsExtraTestCase(unittest.TestCase, AssertFormErrorsMixin):
  16. ###############
  17. # Extra stuff #
  18. ###############
  19. # The forms library comes with some extra, higher-level Field and Widget
  20. def test_selectdate(self):
  21. w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'))
  22. self.assertEqual(w.render('mydate', ''), """<select name="mydate_month" id="id_mydate_month">
  23. <option value="0">---</option>
  24. <option value="1">January</option>
  25. <option value="2">February</option>
  26. <option value="3">March</option>
  27. <option value="4">April</option>
  28. <option value="5">May</option>
  29. <option value="6">June</option>
  30. <option value="7">July</option>
  31. <option value="8">August</option>
  32. <option value="9">September</option>
  33. <option value="10">October</option>
  34. <option value="11">November</option>
  35. <option value="12">December</option>
  36. </select>
  37. <select name="mydate_day" id="id_mydate_day">
  38. <option value="0">---</option>
  39. <option value="1">1</option>
  40. <option value="2">2</option>
  41. <option value="3">3</option>
  42. <option value="4">4</option>
  43. <option value="5">5</option>
  44. <option value="6">6</option>
  45. <option value="7">7</option>
  46. <option value="8">8</option>
  47. <option value="9">9</option>
  48. <option value="10">10</option>
  49. <option value="11">11</option>
  50. <option value="12">12</option>
  51. <option value="13">13</option>
  52. <option value="14">14</option>
  53. <option value="15">15</option>
  54. <option value="16">16</option>
  55. <option value="17">17</option>
  56. <option value="18">18</option>
  57. <option value="19">19</option>
  58. <option value="20">20</option>
  59. <option value="21">21</option>
  60. <option value="22">22</option>
  61. <option value="23">23</option>
  62. <option value="24">24</option>
  63. <option value="25">25</option>
  64. <option value="26">26</option>
  65. <option value="27">27</option>
  66. <option value="28">28</option>
  67. <option value="29">29</option>
  68. <option value="30">30</option>
  69. <option value="31">31</option>
  70. </select>
  71. <select name="mydate_year" id="id_mydate_year">
  72. <option value="0">---</option>
  73. <option value="2007">2007</option>
  74. <option value="2008">2008</option>
  75. <option value="2009">2009</option>
  76. <option value="2010">2010</option>
  77. <option value="2011">2011</option>
  78. <option value="2012">2012</option>
  79. <option value="2013">2013</option>
  80. <option value="2014">2014</option>
  81. <option value="2015">2015</option>
  82. <option value="2016">2016</option>
  83. </select>""")
  84. self.assertEqual(w.render('mydate', None), w.render('mydate', ''))
  85. self.assertEqual(w.render('mydate', '2010-04-15'), """<select name="mydate_month" id="id_mydate_month">
  86. <option value="1">January</option>
  87. <option value="2">February</option>
  88. <option value="3">March</option>
  89. <option value="4" selected="selected">April</option>
  90. <option value="5">May</option>
  91. <option value="6">June</option>
  92. <option value="7">July</option>
  93. <option value="8">August</option>
  94. <option value="9">September</option>
  95. <option value="10">October</option>
  96. <option value="11">November</option>
  97. <option value="12">December</option>
  98. </select>
  99. <select name="mydate_day" id="id_mydate_day">
  100. <option value="1">1</option>
  101. <option value="2">2</option>
  102. <option value="3">3</option>
  103. <option value="4">4</option>
  104. <option value="5">5</option>
  105. <option value="6">6</option>
  106. <option value="7">7</option>
  107. <option value="8">8</option>
  108. <option value="9">9</option>
  109. <option value="10">10</option>
  110. <option value="11">11</option>
  111. <option value="12">12</option>
  112. <option value="13">13</option>
  113. <option value="14">14</option>
  114. <option value="15" selected="selected">15</option>
  115. <option value="16">16</option>
  116. <option value="17">17</option>
  117. <option value="18">18</option>
  118. <option value="19">19</option>
  119. <option value="20">20</option>
  120. <option value="21">21</option>
  121. <option value="22">22</option>
  122. <option value="23">23</option>
  123. <option value="24">24</option>
  124. <option value="25">25</option>
  125. <option value="26">26</option>
  126. <option value="27">27</option>
  127. <option value="28">28</option>
  128. <option value="29">29</option>
  129. <option value="30">30</option>
  130. <option value="31">31</option>
  131. </select>
  132. <select name="mydate_year" id="id_mydate_year">
  133. <option value="2007">2007</option>
  134. <option value="2008">2008</option>
  135. <option value="2009">2009</option>
  136. <option value="2010" selected="selected">2010</option>
  137. <option value="2011">2011</option>
  138. <option value="2012">2012</option>
  139. <option value="2013">2013</option>
  140. <option value="2014">2014</option>
  141. <option value="2015">2015</option>
  142. <option value="2016">2016</option>
  143. </select>""")
  144. # Accepts a datetime or a string:
  145. self.assertEqual(w.render('mydate', datetime.date(2010, 4, 15)), w.render('mydate', '2010-04-15'))
  146. # Invalid dates still render the failed date:
  147. self.assertEqual(w.render('mydate', '2010-02-31'), """<select name="mydate_month" id="id_mydate_month">
  148. <option value="1">January</option>
  149. <option value="2" selected="selected">February</option>
  150. <option value="3">March</option>
  151. <option value="4">April</option>
  152. <option value="5">May</option>
  153. <option value="6">June</option>
  154. <option value="7">July</option>
  155. <option value="8">August</option>
  156. <option value="9">September</option>
  157. <option value="10">October</option>
  158. <option value="11">November</option>
  159. <option value="12">December</option>
  160. </select>
  161. <select name="mydate_day" id="id_mydate_day">
  162. <option value="1">1</option>
  163. <option value="2">2</option>
  164. <option value="3">3</option>
  165. <option value="4">4</option>
  166. <option value="5">5</option>
  167. <option value="6">6</option>
  168. <option value="7">7</option>
  169. <option value="8">8</option>
  170. <option value="9">9</option>
  171. <option value="10">10</option>
  172. <option value="11">11</option>
  173. <option value="12">12</option>
  174. <option value="13">13</option>
  175. <option value="14">14</option>
  176. <option value="15">15</option>
  177. <option value="16">16</option>
  178. <option value="17">17</option>
  179. <option value="18">18</option>
  180. <option value="19">19</option>
  181. <option value="20">20</option>
  182. <option value="21">21</option>
  183. <option value="22">22</option>
  184. <option value="23">23</option>
  185. <option value="24">24</option>
  186. <option value="25">25</option>
  187. <option value="26">26</option>
  188. <option value="27">27</option>
  189. <option value="28">28</option>
  190. <option value="29">29</option>
  191. <option value="30">30</option>
  192. <option value="31" selected="selected">31</option>
  193. </select>
  194. <select name="mydate_year" id="id_mydate_year">
  195. <option value="2007">2007</option>
  196. <option value="2008">2008</option>
  197. <option value="2009">2009</option>
  198. <option value="2010" selected="selected">2010</option>
  199. <option value="2011">2011</option>
  200. <option value="2012">2012</option>
  201. <option value="2013">2013</option>
  202. <option value="2014">2014</option>
  203. <option value="2015">2015</option>
  204. <option value="2016">2016</option>
  205. </select>""")
  206. # Using a SelectDateWidget in a form:
  207. w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False)
  208. self.assertEqual(w.render('mydate', ''), """<select name="mydate_month" id="id_mydate_month">
  209. <option value="0">---</option>
  210. <option value="1">January</option>
  211. <option value="2">February</option>
  212. <option value="3">March</option>
  213. <option value="4">April</option>
  214. <option value="5">May</option>
  215. <option value="6">June</option>
  216. <option value="7">July</option>
  217. <option value="8">August</option>
  218. <option value="9">September</option>
  219. <option value="10">October</option>
  220. <option value="11">November</option>
  221. <option value="12">December</option>
  222. </select>
  223. <select name="mydate_day" id="id_mydate_day">
  224. <option value="0">---</option>
  225. <option value="1">1</option>
  226. <option value="2">2</option>
  227. <option value="3">3</option>
  228. <option value="4">4</option>
  229. <option value="5">5</option>
  230. <option value="6">6</option>
  231. <option value="7">7</option>
  232. <option value="8">8</option>
  233. <option value="9">9</option>
  234. <option value="10">10</option>
  235. <option value="11">11</option>
  236. <option value="12">12</option>
  237. <option value="13">13</option>
  238. <option value="14">14</option>
  239. <option value="15">15</option>
  240. <option value="16">16</option>
  241. <option value="17">17</option>
  242. <option value="18">18</option>
  243. <option value="19">19</option>
  244. <option value="20">20</option>
  245. <option value="21">21</option>
  246. <option value="22">22</option>
  247. <option value="23">23</option>
  248. <option value="24">24</option>
  249. <option value="25">25</option>
  250. <option value="26">26</option>
  251. <option value="27">27</option>
  252. <option value="28">28</option>
  253. <option value="29">29</option>
  254. <option value="30">30</option>
  255. <option value="31">31</option>
  256. </select>
  257. <select name="mydate_year" id="id_mydate_year">
  258. <option value="0">---</option>
  259. <option value="2007">2007</option>
  260. <option value="2008">2008</option>
  261. <option value="2009">2009</option>
  262. <option value="2010">2010</option>
  263. <option value="2011">2011</option>
  264. <option value="2012">2012</option>
  265. <option value="2013">2013</option>
  266. <option value="2014">2014</option>
  267. <option value="2015">2015</option>
  268. <option value="2016">2016</option>
  269. </select>""")
  270. self.assertEqual(w.render('mydate', '2010-04-15'), """<select name="mydate_month" id="id_mydate_month">
  271. <option value="0">---</option>
  272. <option value="1">January</option>
  273. <option value="2">February</option>
  274. <option value="3">March</option>
  275. <option value="4" selected="selected">April</option>
  276. <option value="5">May</option>
  277. <option value="6">June</option>
  278. <option value="7">July</option>
  279. <option value="8">August</option>
  280. <option value="9">September</option>
  281. <option value="10">October</option>
  282. <option value="11">November</option>
  283. <option value="12">December</option>
  284. </select>
  285. <select name="mydate_day" id="id_mydate_day">
  286. <option value="0">---</option>
  287. <option value="1">1</option>
  288. <option value="2">2</option>
  289. <option value="3">3</option>
  290. <option value="4">4</option>
  291. <option value="5">5</option>
  292. <option value="6">6</option>
  293. <option value="7">7</option>
  294. <option value="8">8</option>
  295. <option value="9">9</option>
  296. <option value="10">10</option>
  297. <option value="11">11</option>
  298. <option value="12">12</option>
  299. <option value="13">13</option>
  300. <option value="14">14</option>
  301. <option value="15" selected="selected">15</option>
  302. <option value="16">16</option>
  303. <option value="17">17</option>
  304. <option value="18">18</option>
  305. <option value="19">19</option>
  306. <option value="20">20</option>
  307. <option value="21">21</option>
  308. <option value="22">22</option>
  309. <option value="23">23</option>
  310. <option value="24">24</option>
  311. <option value="25">25</option>
  312. <option value="26">26</option>
  313. <option value="27">27</option>
  314. <option value="28">28</option>
  315. <option value="29">29</option>
  316. <option value="30">30</option>
  317. <option value="31">31</option>
  318. </select>
  319. <select name="mydate_year" id="id_mydate_year">
  320. <option value="0">---</option>
  321. <option value="2007">2007</option>
  322. <option value="2008">2008</option>
  323. <option value="2009">2009</option>
  324. <option value="2010" selected="selected">2010</option>
  325. <option value="2011">2011</option>
  326. <option value="2012">2012</option>
  327. <option value="2013">2013</option>
  328. <option value="2014">2014</option>
  329. <option value="2015">2015</option>
  330. <option value="2016">2016</option>
  331. </select>""")
  332. a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'})
  333. self.assertTrue(a.is_valid())
  334. self.assertEqual(a.cleaned_data['mydate'], datetime.date(2008, 4, 1))
  335. # As with any widget that implements get_value_from_datadict,
  336. # we must be prepared to accept the input from the "as_hidden"
  337. # rendering as well.
  338. self.assertEqual(a['mydate'].as_hidden(), '<input type="hidden" name="mydate" value="2008-4-1" id="id_mydate" />')
  339. b = GetDate({'mydate':'2008-4-1'})
  340. self.assertTrue(b.is_valid())
  341. self.assertEqual(b.cleaned_data['mydate'], datetime.date(2008, 4, 1))
  342. # Invalid dates shouldn't be allowed
  343. c = GetDate({'mydate_month':'2', 'mydate_day':'31', 'mydate_year':'2010'})
  344. self.assertFalse(c.is_valid())
  345. self.assertEqual(c.errors, {'mydate': [u'Enter a valid date.']})
  346. # label tag is correctly associated with month dropdown
  347. d = GetDate({'mydate_month':'1', 'mydate_day':'1', 'mydate_year':'2010'})
  348. self.assertTrue('<label for="id_mydate_month">' in d.as_p())
  349. def test_multiwidget(self):
  350. # MultiWidget and MultiValueField #############################################
  351. # MultiWidgets are widgets composed of other widgets. They are usually
  352. # combined with MultiValueFields - a field that is composed of other fields.
  353. # MulitWidgets can themselved be composed of other MultiWidgets.
  354. # SplitDateTimeWidget is one example of a MultiWidget.
  355. class ComplexMultiWidget(MultiWidget):
  356. def __init__(self, attrs=None):
  357. widgets = (
  358. TextInput(),
  359. SelectMultiple(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))),
  360. SplitDateTimeWidget(),
  361. )
  362. super(ComplexMultiWidget, self).__init__(widgets, attrs)
  363. def decompress(self, value):
  364. if value:
  365. data = value.split(',')
  366. return [data[0], data[1], datetime.datetime(*time.strptime(data[2], "%Y-%m-%d %H:%M:%S")[0:6])]
  367. return [None, None, None]
  368. def format_output(self, rendered_widgets):
  369. return u'\n'.join(rendered_widgets)
  370. w = ComplexMultiWidget()
  371. self.assertEqual(w.render('name', 'some text,JP,2007-04-25 06:24:00'), """<input type="text" name="name_0" value="some text" />
  372. <select multiple="multiple" name="name_1">
  373. <option value="J" selected="selected">John</option>
  374. <option value="P" selected="selected">Paul</option>
  375. <option value="G">George</option>
  376. <option value="R">Ringo</option>
  377. </select>
  378. <input type="text" name="name_2_0" value="2007-04-25" /><input type="text" name="name_2_1" value="06:24:00" />""")
  379. class ComplexField(MultiValueField):
  380. def __init__(self, required=True, widget=None, label=None, initial=None):
  381. fields = (
  382. CharField(),
  383. MultipleChoiceField(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))),
  384. SplitDateTimeField()
  385. )
  386. super(ComplexField, self).__init__(fields, required, widget, label, initial)
  387. def compress(self, data_list):
  388. if data_list:
  389. return '%s,%s,%s' % (data_list[0],''.join(data_list[1]),data_list[2])
  390. return None
  391. f = ComplexField(widget=w)
  392. self.assertEqual(f.clean(['some text', ['J','P'], ['2007-04-25','6:24:00']]), u'some text,JP,2007-04-25 06:24:00')
  393. self.assertFormErrors([u'Select a valid choice. X is not one of the available choices.'], f.clean, ['some text',['X'], ['2007-04-25','6:24:00']])
  394. # If insufficient data is provided, None is substituted
  395. self.assertFormErrors([u'This field is required.'], f.clean, ['some text',['JP']])
  396. class ComplexFieldForm(Form):
  397. field1 = ComplexField(widget=w)
  398. f = ComplexFieldForm()
  399. self.assertEqual(f.as_table(), """<tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" />
  400. <select multiple="multiple" name="field1_1" id="id_field1_1">
  401. <option value="J">John</option>
  402. <option value="P">Paul</option>
  403. <option value="G">George</option>
  404. <option value="R">Ringo</option>
  405. </select>
  406. <input type="text" name="field1_2_0" id="id_field1_2_0" /><input type="text" name="field1_2_1" id="id_field1_2_1" /></td></tr>""")
  407. f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'})
  408. self.assertEqual(f.as_table(), """<tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" />
  409. <select multiple="multiple" name="field1_1" id="id_field1_1">
  410. <option value="J" selected="selected">John</option>
  411. <option value="P" selected="selected">Paul</option>
  412. <option value="G">George</option>
  413. <option value="R">Ringo</option>
  414. </select>
  415. <input type="text" name="field1_2_0" value="2007-04-25" id="id_field1_2_0" /><input type="text" name="field1_2_1" value="06:24:00" id="id_field1_2_1" /></td></tr>""")
  416. self.assertEqual(f.cleaned_data['field1'], u'some text,JP,2007-04-25 06:24:00')
  417. def test_ipaddress(self):
  418. f = IPAddressField()
  419. self.assertFormErrors([u'This field is required.'], f.clean, '')
  420. self.assertFormErrors([u'This field is required.'], f.clean, None)
  421. self.assertEqual(f.clean('127.0.0.1'), u'127.0.0.1')
  422. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, 'foo')
  423. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, '127.0.0.')
  424. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, '1.2.3.4.5')
  425. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, '256.125.1.5')
  426. f = IPAddressField(required=False)
  427. self.assertEqual(f.clean(''), u'')
  428. self.assertEqual(f.clean(None), u'')
  429. self.assertEqual(f.clean('127.0.0.1'), u'127.0.0.1')
  430. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, 'foo')
  431. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, '127.0.0.')
  432. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, '1.2.3.4.5')
  433. self.assertFormErrors([u'Enter a valid IPv4 address.'], f.clean, '256.125.1.5')
  434. def test_smart_unicode(self):
  435. class Test:
  436. def __str__(self):
  437. return 'Š??Ž?žš?'
  438. class TestU:
  439. def __str__(self):
  440. return 'Foo'
  441. def __unicode__(self):
  442. return u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'
  443. self.assertEqual(smart_unicode(Test()), u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111')
  444. self.assertEqual(smart_unicode(TestU()), u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111')
  445. self.assertEqual(smart_unicode(1), u'1')
  446. self.assertEqual(smart_unicode('foo'), u'foo')
  447. def test_accessing_clean(self):
  448. class UserForm(Form):
  449. username = CharField(max_length=10)
  450. password = CharField(widget=PasswordInput)
  451. def clean(self):
  452. data = self.cleaned_data
  453. if not self.errors:
  454. data['username'] = data['username'].lower()
  455. return data
  456. f = UserForm({'username': 'SirRobin', 'password': 'blue'})
  457. self.assertTrue(f.is_valid())
  458. self.assertEqual(f.cleaned_data['username'], u'sirrobin')
  459. def test_overriding_errorlist(self):
  460. class DivErrorList(ErrorList):
  461. def __unicode__(self):
  462. return self.as_divs()
  463. def as_divs(self):
  464. if not self: return u''
  465. return u'<div class="errorlist">%s</div>' % ''.join([u'<div class="error">%s</div>' % force_unicode(e) for e in self])
  466. class CommentForm(Form):
  467. name = CharField(max_length=50, required=False)
  468. email = EmailField()
  469. comment = CharField()
  470. data = dict(email='invalid')
  471. f = CommentForm(data, auto_id=False, error_class=DivErrorList)
  472. self.assertEqual(f.as_p(), """<p>Name: <input type="text" name="name" maxlength="50" /></p>
  473. <div class="errorlist"><div class="error">Enter a valid e-mail address.</div></div>
  474. <p>Email: <input type="text" name="email" value="invalid" /></p>
  475. <div class="errorlist"><div class="error">This field is required.</div></div>
  476. <p>Comment: <input type="text" name="comment" /></p>""")
  477. def test_multipart_encoded_form(self):
  478. class FormWithoutFile(Form):
  479. username = CharField()
  480. class FormWithFile(Form):
  481. username = CharField()
  482. file = FileField()
  483. class FormWithImage(Form):
  484. image = ImageField()
  485. self.assertFalse(FormWithoutFile().is_multipart())
  486. self.assertTrue(FormWithFile().is_multipart())
  487. self.assertTrue(FormWithImage().is_multipart())
  488. class FormsExtraL10NTestCase(unittest.TestCase):
  489. def setUp(self):
  490. super(FormsExtraL10NTestCase, self).setUp()
  491. self.old_use_l10n = getattr(settings, 'USE_L10N', False)
  492. settings.USE_L10N = True
  493. translation.activate('nl')
  494. def tearDown(self):
  495. translation.deactivate()
  496. settings.USE_L10N = self.old_use_l10n
  497. super(FormsExtraL10NTestCase, self).tearDown()
  498. def test_l10n(self):
  499. w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False)
  500. self.assertEqual(w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date'), '13-08-2010')
  501. self.assertEqual(w.render('date', '13-08-2010'), """<select name="date_day" id="id_date_day">
  502. <option value="0">---</option>
  503. <option value="1">1</option>
  504. <option value="2">2</option>
  505. <option value="3">3</option>
  506. <option value="4">4</option>
  507. <option value="5">5</option>
  508. <option value="6">6</option>
  509. <option value="7">7</option>
  510. <option value="8">8</option>
  511. <option value="9">9</option>
  512. <option value="10">10</option>
  513. <option value="11">11</option>
  514. <option value="12">12</option>
  515. <option value="13" selected="selected">13</option>
  516. <option value="14">14</option>
  517. <option value="15">15</option>
  518. <option value="16">16</option>
  519. <option value="17">17</option>
  520. <option value="18">18</option>
  521. <option value="19">19</option>
  522. <option value="20">20</option>
  523. <option value="21">21</option>
  524. <option value="22">22</option>
  525. <option value="23">23</option>
  526. <option value="24">24</option>
  527. <option value="25">25</option>
  528. <option value="26">26</option>
  529. <option value="27">27</option>
  530. <option value="28">28</option>
  531. <option value="29">29</option>
  532. <option value="30">30</option>
  533. <option value="31">31</option>
  534. </select>
  535. <select name="date_month" id="id_date_month">
  536. <option value="0">---</option>
  537. <option value="1">januari</option>
  538. <option value="2">februari</option>
  539. <option value="3">maart</option>
  540. <option value="4">april</option>
  541. <option value="5">mei</option>
  542. <option value="6">juni</option>
  543. <option value="7">juli</option>
  544. <option value="8" selected="selected">augustus</option>
  545. <option value="9">september</option>
  546. <option value="10">oktober</option>
  547. <option value="11">november</option>
  548. <option value="12">december</option>
  549. </select>
  550. <select name="date_year" id="id_date_year">
  551. <option value="0">---</option>
  552. <option value="2007">2007</option>
  553. <option value="2008">2008</option>
  554. <option value="2009">2009</option>
  555. <option value="2010" selected="selected">2010</option>
  556. <option value="2011">2011</option>
  557. <option value="2012">2012</option>
  558. <option value="2013">2013</option>
  559. <option value="2014">2014</option>
  560. <option value="2015">2015</option>
  561. <option value="2016">2016</option>
  562. </select>""")
  563. # Years before 1900 work
  564. w = SelectDateWidget(years=('1899',))
  565. self.assertEqual(w.value_from_datadict({'date_year': '1899', 'date_month': '8', 'date_day': '13'}, {}, 'date'), '13-08-1899')
  566. def test_l10n_invalid_date_in(self):
  567. # Invalid dates shouldn't be allowed
  568. a = GetDate({'mydate_month':'2', 'mydate_day':'31', 'mydate_year':'2010'})
  569. self.assertFalse(a.is_valid())
  570. # 'Geef een geldige datum op.' = 'Enter a valid date.'
  571. self.assertEqual(a.errors, {'mydate': [u'Geef een geldige datum op.']})
  572. def test_form_label_association(self):
  573. # label tag is correctly associated with first rendered dropdown
  574. a = GetDate({'mydate_month':'1', 'mydate_day':'1', 'mydate_year':'2010'})
  575. self.assertTrue('<label for="id_mydate_day">' in a.as_p())