test_choicefield.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import unicode_literals
  2. from django.forms import ChoiceField, Form, ValidationError
  3. from django.test import SimpleTestCase
  4. from . import FormFieldAssertionsMixin
  5. class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
  6. def test_choicefield_1(self):
  7. f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')])
  8. with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
  9. f.clean('')
  10. with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
  11. f.clean(None)
  12. self.assertEqual('1', f.clean(1))
  13. self.assertEqual('1', f.clean('1'))
  14. msg = "'Select a valid choice. 3 is not one of the available choices.'"
  15. with self.assertRaisesMessage(ValidationError, msg):
  16. f.clean('3')
  17. def test_choicefield_2(self):
  18. f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False)
  19. self.assertEqual('', f.clean(''))
  20. self.assertEqual('', f.clean(None))
  21. self.assertEqual('1', f.clean(1))
  22. self.assertEqual('1', f.clean('1'))
  23. msg = "'Select a valid choice. 3 is not one of the available choices.'"
  24. with self.assertRaisesMessage(ValidationError, msg):
  25. f.clean('3')
  26. def test_choicefield_3(self):
  27. f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')])
  28. self.assertEqual('J', f.clean('J'))
  29. msg = "'Select a valid choice. John is not one of the available choices.'"
  30. with self.assertRaisesMessage(ValidationError, msg):
  31. f.clean('John')
  32. def test_choicefield_4(self):
  33. f = ChoiceField(
  34. choices=[
  35. ('Numbers', (('1', 'One'), ('2', 'Two'))),
  36. ('Letters', (('3', 'A'), ('4', 'B'))), ('5', 'Other'),
  37. ]
  38. )
  39. self.assertEqual('1', f.clean(1))
  40. self.assertEqual('1', f.clean('1'))
  41. self.assertEqual('3', f.clean(3))
  42. self.assertEqual('3', f.clean('3'))
  43. self.assertEqual('5', f.clean(5))
  44. self.assertEqual('5', f.clean('5'))
  45. msg = "'Select a valid choice. 6 is not one of the available choices.'"
  46. with self.assertRaisesMessage(ValidationError, msg):
  47. f.clean('6')
  48. def test_choicefield_callable(self):
  49. def choices():
  50. return [('J', 'John'), ('P', 'Paul')]
  51. f = ChoiceField(choices=choices)
  52. self.assertEqual('J', f.clean('J'))
  53. def test_choicefield_callable_may_evaluate_to_different_values(self):
  54. choices = []
  55. def choices_as_callable():
  56. return choices
  57. class ChoiceFieldForm(Form):
  58. choicefield = ChoiceField(choices=choices_as_callable)
  59. choices = [('J', 'John')]
  60. form = ChoiceFieldForm()
  61. self.assertEqual([('J', 'John')], list(form.fields['choicefield'].choices))
  62. choices = [('P', 'Paul')]
  63. form = ChoiceFieldForm()
  64. self.assertEqual([('P', 'Paul')], list(form.fields['choicefield'].choices))
  65. def test_choicefield_disabled(self):
  66. f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')], disabled=True)
  67. self.assertWidgetRendersTo(
  68. f,
  69. '<select id="id_f" name="f" disabled required><option value="J">John</option>'
  70. '<option value="P">Paul</option></select>'
  71. )