1. from django.core.exceptions import ValidationError
    
  2. from django.db import models
    
  3. from django.forms import ChoiceField, Form
    
  4. from django.test import SimpleTestCase
    
  5. 
    
  6. from . import FormFieldAssertionsMixin
    
  7. 
    
  8. 
    
  9. class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
    
  10.     def test_choicefield_1(self):
    
  11.         f = ChoiceField(choices=[("1", "One"), ("2", "Two")])
    
  12.         with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
    
  13.             f.clean("")
    
  14.         with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
    
  15.             f.clean(None)
    
  16.         self.assertEqual("1", f.clean(1))
    
  17.         self.assertEqual("1", f.clean("1"))
    
  18.         msg = "'Select a valid choice. 3 is not one of the available choices.'"
    
  19.         with self.assertRaisesMessage(ValidationError, msg):
    
  20.             f.clean("3")
    
  21. 
    
  22.     def test_choicefield_2(self):
    
  23.         f = ChoiceField(choices=[("1", "One"), ("2", "Two")], required=False)
    
  24.         self.assertEqual("", f.clean(""))
    
  25.         self.assertEqual("", f.clean(None))
    
  26.         self.assertEqual("1", f.clean(1))
    
  27.         self.assertEqual("1", f.clean("1"))
    
  28.         msg = "'Select a valid choice. 3 is not one of the available choices.'"
    
  29.         with self.assertRaisesMessage(ValidationError, msg):
    
  30.             f.clean("3")
    
  31. 
    
  32.     def test_choicefield_3(self):
    
  33.         f = ChoiceField(choices=[("J", "John"), ("P", "Paul")])
    
  34.         self.assertEqual("J", f.clean("J"))
    
  35.         msg = "'Select a valid choice. John is not one of the available choices.'"
    
  36.         with self.assertRaisesMessage(ValidationError, msg):
    
  37.             f.clean("John")
    
  38. 
    
  39.     def test_choicefield_4(self):
    
  40.         f = ChoiceField(
    
  41.             choices=[
    
  42.                 ("Numbers", (("1", "One"), ("2", "Two"))),
    
  43.                 ("Letters", (("3", "A"), ("4", "B"))),
    
  44.                 ("5", "Other"),
    
  45.             ]
    
  46.         )
    
  47.         self.assertEqual("1", f.clean(1))
    
  48.         self.assertEqual("1", f.clean("1"))
    
  49.         self.assertEqual("3", f.clean(3))
    
  50.         self.assertEqual("3", f.clean("3"))
    
  51.         self.assertEqual("5", f.clean(5))
    
  52.         self.assertEqual("5", f.clean("5"))
    
  53.         msg = "'Select a valid choice. 6 is not one of the available choices.'"
    
  54.         with self.assertRaisesMessage(ValidationError, msg):
    
  55.             f.clean("6")
    
  56. 
    
  57.     def test_choicefield_choices_default(self):
    
  58.         f = ChoiceField()
    
  59.         self.assertEqual(f.choices, [])
    
  60. 
    
  61.     def test_choicefield_callable(self):
    
  62.         def choices():
    
  63.             return [("J", "John"), ("P", "Paul")]
    
  64. 
    
  65.         f = ChoiceField(choices=choices)
    
  66.         self.assertEqual("J", f.clean("J"))
    
  67. 
    
  68.     def test_choicefield_callable_may_evaluate_to_different_values(self):
    
  69.         choices = []
    
  70. 
    
  71.         def choices_as_callable():
    
  72.             return choices
    
  73. 
    
  74.         class ChoiceFieldForm(Form):
    
  75.             choicefield = ChoiceField(choices=choices_as_callable)
    
  76. 
    
  77.         choices = [("J", "John")]
    
  78.         form = ChoiceFieldForm()
    
  79.         self.assertEqual([("J", "John")], list(form.fields["choicefield"].choices))
    
  80. 
    
  81.         choices = [("P", "Paul")]
    
  82.         form = ChoiceFieldForm()
    
  83.         self.assertEqual([("P", "Paul")], list(form.fields["choicefield"].choices))
    
  84. 
    
  85.     def test_choicefield_disabled(self):
    
  86.         f = ChoiceField(choices=[("J", "John"), ("P", "Paul")], disabled=True)
    
  87.         self.assertWidgetRendersTo(
    
  88.             f,
    
  89.             '<select id="id_f" name="f" disabled><option value="J">John</option>'
    
  90.             '<option value="P">Paul</option></select>',
    
  91.         )
    
  92. 
    
  93.     def test_choicefield_enumeration(self):
    
  94.         class FirstNames(models.TextChoices):
    
  95.             JOHN = "J", "John"
    
  96.             PAUL = "P", "Paul"
    
  97. 
    
  98.         f = ChoiceField(choices=FirstNames.choices)
    
  99.         self.assertEqual(f.clean("J"), "J")
    
  100.         msg = "'Select a valid choice. 3 is not one of the available choices.'"
    
  101.         with self.assertRaisesMessage(ValidationError, msg):
    
  102.             f.clean("3")