def test_typedchoicefield_4(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 = TypedChoiceField(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('')
Beispiel #2
0
 def test_typedchoicefield_4(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 = TypedChoiceField(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('')
Beispiel #3
0
    def test_typedchoicefield_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 = TypedChoiceField(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_typedchoicefield_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 = TypedChoiceField(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')
Beispiel #5
0
 def test_typedchoicefield_6(self):
     f = TypedChoiceField(
         choices=[(1, "+1"), (-1, "-1")],
         coerce=int,
         required=False,
         empty_value=None,
     )
     self.assertIsNone(f.clean(""))
Beispiel #6
0
 def test_typedchoicefield_1(self):
     f = TypedChoiceField(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')
Beispiel #7
0
 def test_typedchoicefield_5(self):
     # Non-required fields aren't required
     f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False)
     self.assertEqual('', f.clean(''))
Beispiel #8
0
 def test_typedchoicefield_3(self):
     # This can also cause weirdness: be careful (bool(-1) == True, remember)
     f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=bool)
     self.assertTrue(f.clean('-1'))
Beispiel #9
0
 def test_typedchoicefield_2(self):
     # Different coercion, same validation.
     f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=float)
     self.assertEqual(1.0, f.clean('1'))
 def test_typedchoicefield_1(self):
     f = TypedChoiceField(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_typedchoicefield_6(self):
     f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
     self.assertIsNone(f.clean(''))
 def test_typedchoicefield_5(self):
     # Non-required fields aren't required
     f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False)
     self.assertEqual('', f.clean(''))
 def test_typedchoicefield_3(self):
     # This can also cause weirdness: be careful (bool(-1) == True, remember)
     f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=bool)
     self.assertTrue(f.clean('-1'))
 def test_typedchoicefield_2(self):
     # Different coercion, same validation.
     f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=float)
     self.assertEqual(1.0, f.clean('1'))