def test_typedmultiplechoicefield_1(self):
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int)
     self.assertEqual([1], f.clean(["1"]))
     msg = "'Select a valid choice. 2 is not one of the available choices.'"
     with self.assertRaisesMessage(ValidationError, msg):
         f.clean(["2"])
 def test_typedmultiplechoicefield_7(self):
     # If you want cleaning an empty value to return a different type, tell the field
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int,
                                  required=False,
                                  empty_value=None)
     self.assertIsNone(f.clean([]))
 def test_typedmultiplechoicefield_5(self):
     # Even more weirdness: if you have a valid choice but your coercion function
     # can't coerce, you'll still get a validation error. Don't do this!
     f = TypedMultipleChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int)
     msg = "'Select a valid choice. B is not one of the available choices.'"
     with self.assertRaisesMessage(ValidationError, msg):
         f.clean(['B'])
     # Required fields require values
     with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
         f.clean([])
 def test_typedmultiplechoicefield_5(self):
     # Even more weirdness: if you have a valid choice but your coercion function
     # can't coerce, you'll still get a validation error. Don't do this!
     f = TypedMultipleChoiceField(choices=[("A", "A"), ("B", "B")],
                                  coerce=int)
     msg = "'Select a valid choice. B is not one of the available choices.'"
     with self.assertRaisesMessage(ValidationError, msg):
         f.clean(["B"])
     # Required fields require values
     with self.assertRaisesMessage(ValidationError,
                                   "'This field is required.'"):
         f.clean([])
    def test_typedmultiplechoicefield_special_coerce(self):
        """
        A coerce function which results in a value not present in choices
        should raise an appropriate error (#21397).
        """
        def coerce_func(val):
            return decimal.Decimal("1.%s" % val)

        f = TypedMultipleChoiceField(choices=[(1, "1"), (2, "2")],
                                     coerce=coerce_func,
                                     required=True)
        self.assertEqual([decimal.Decimal("1.2")], f.clean(["2"]))
        with self.assertRaisesMessage(ValidationError,
                                      "'This field is required.'"):
            f.clean([])
        msg = "'Select a valid choice. 3 is not one of the available choices.'"
        with self.assertRaisesMessage(ValidationError, msg):
            f.clean(["3"])
Ejemplo n.º 6
0
class AllFieldTypesForm(Form):
    char = CharField()
    int_ = IntegerField()
    date = DateField()
    time = TimeField()
    datetime_ = DateTimeField()
    regex = RegexField(regex='^[a-f]{3}$')
    email = EmailField()
    file = FileField()
    # image = ImageField()
    url = URLField()
    bool = BooleanField()
    nullbool = NullBooleanField()
    choice = ChoiceField(choices=(('test choice', 'yay test choice'), ))
    multichoice = MultipleChoiceField(choices=(
        ('test choice', 'yay test choice'),
        ('test choice 2', 'yay another choice'),
        ('test choice 3', 'yay test choice'),
    ))
    float = FloatField()
    decimal = DecimalField()
    ip = IPAddressField()
    generic_ip = GenericIPAddressField()
    filepath = FilePathField(path=tempfile.gettempdir(),
                             allow_files=True,
                             allow_folders=True)
    slug = SlugField()
    typed_choice = TypedChoiceField(choices=(
        (1, 'test'),
        (2, 'test 2'),
        (3, 'bah'),
    ),
                                    coerce=int)
    typed_multichoice = TypedMultipleChoiceField(choices=(
        (1, 'test'),
        (2, 'test 2'),
        (3, 'bah'),
    ),
                                                 coerce=int)
    model_choice = ModelChoiceField(queryset=get_user_model().objects.all())
    model_multichoice = ModelMultipleChoiceField(
        queryset=get_user_model().objects.all())
    def test_typedmultiplechoicefield_special_coerce(self):
        """
        A coerce function which results in a value not present in choices
        should raise an appropriate error (#21397).
        """
        def coerce_func(val):
            return decimal.Decimal('1.%s' % val)

        f = TypedMultipleChoiceField(
            choices=[(1, "1"), (2, "2")], coerce=coerce_func, required=True)
        self.assertEqual([decimal.Decimal('1.2')], f.clean(['2']))
        with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
            f.clean([])
        msg = "'Select a valid choice. 3 is not one of the available choices.'"
        with self.assertRaisesMessage(ValidationError, msg):
            f.clean(['3'])
 def test_typedmultiplechoicefield_has_changed(self):
     # has_changed should not trigger required validation
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int,
                                  required=True)
     self.assertFalse(f.has_changed(None, ""))
 def test_typedmultiplechoicefield_6(self):
     # Non-required fields aren't required
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int,
                                  required=False)
     self.assertEqual([], f.clean([]))
 def test_typedmultiplechoicefield_3(self):
     # This can also cause weirdness: be careful (bool(-1) == True, remember)
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=bool)
     self.assertEqual([True], f.clean(["-1"]))
 def test_typedmultiplechoicefield_2(self):
     # Different coercion, same validation.
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=float)
     self.assertEqual([1.0], f.clean(["1"]))
 def test_typedmultiplechoicefield_has_changed(self):
     # has_changed should not trigger required validation
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
     self.assertFalse(f.has_changed(None, ''))
 def test_typedmultiplechoicefield_7(self):
     # If you want cleaning an empty value to return a different type, tell the field
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
     self.assertIsNone(f.clean([]))
 def test_typedmultiplechoicefield_6(self):
     # Non-required fields aren't required
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False)
     self.assertEqual([], f.clean([]))
 def test_typedmultiplechoicefield_3(self):
     # This can also cause weirdness: be careful (bool(-1) == True, remember)
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=bool)
     self.assertEqual([True], f.clean(['-1']))
 def test_typedmultiplechoicefield_2(self):
     # Different coercion, same validation.
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=float)
     self.assertEqual([1.0], f.clean(['1']))
Ejemplo n.º 17
0
class LessonForm(forms.ModelForm):
    week_number = TypedMultipleChoiceField(choices=Lesson.WEEK_NUMBERS)
 def test_typedmultiplechoicefield_1(self):
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
     self.assertEqual([1], f.clean(['1']))
     msg = "'Select a valid choice. 2 is not one of the available choices.'"
     with self.assertRaisesMessage(ValidationError, msg):
         f.clean(['2'])