mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #21397 -- Re-added flexibility to TypedChoiceField coercion
Thanks Elec for the report and Simon Charette for the review.
This commit is contained in:
		| @@ -956,6 +956,22 @@ class FieldsTests(SimpleTestCase): | ||||
|         f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True) | ||||
|         self.assertFalse(f._has_changed(None, '')) | ||||
|  | ||||
|     def test_typedchoicefield_special_coerce(self): | ||||
|         """ | ||||
|         Test a coerce function which results in a value not present in choices. | ||||
|         Refs #21397. | ||||
|         """ | ||||
|         def coerce_func(val): | ||||
|             return Decimal('1.%s' % val) | ||||
|  | ||||
|         f = TypedChoiceField(choices=[(1, "1"), (2, "2")], coerce=coerce_func, required=True) | ||||
|         self.assertEqual(Decimal('1.2'), f.clean('2')) | ||||
|         self.assertRaisesMessage(ValidationError, | ||||
|             "'This field is required.'", f.clean, '') | ||||
|         self.assertRaisesMessage(ValidationError, | ||||
|             "'Select a valid choice. 3 is not one of the available choices.'", | ||||
|             f.clean, '3') | ||||
|  | ||||
|     # NullBooleanField ############################################################ | ||||
|  | ||||
|     def test_nullbooleanfield_1(self): | ||||
| @@ -1110,6 +1126,23 @@ class FieldsTests(SimpleTestCase): | ||||
|         f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True) | ||||
|         self.assertFalse(f._has_changed(None, '')) | ||||
|  | ||||
|     def test_typedmultiplechoicefield_special_coerce(self): | ||||
|         """ | ||||
|         Test a coerce function which results in a value not present in choices. | ||||
|         Refs #21397. | ||||
|         """ | ||||
|         def coerce_func(val): | ||||
|             return Decimal('1.%s' % val) | ||||
|  | ||||
|         f = TypedMultipleChoiceField( | ||||
|             choices=[(1, "1"), (2, "2")], coerce=coerce_func, required=True) | ||||
|         self.assertEqual([Decimal('1.2')], f.clean(['2'])) | ||||
|         self.assertRaisesMessage(ValidationError, | ||||
|             "'This field is required.'", f.clean, []) | ||||
|         self.assertRaisesMessage(ValidationError, | ||||
|             "'Select a valid choice. 3 is not one of the available choices.'", | ||||
|             f.clean, ['3']) | ||||
|  | ||||
|    # ComboField ################################################################## | ||||
|  | ||||
|     def test_combofield_1(self): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user