From 72ebe85a269aab4bdb3829de4846b41f90973c5d Mon Sep 17 00:00:00 2001 From: Shai Berger Date: Mon, 31 Dec 2018 19:57:35 +0200 Subject: Fixed #27910 -- Added enumeration helpers for use in Field.choices. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These classes can serve as a base class for user enums, supporting translatable human-readable names, or names automatically inferred from the enum member name. Additional properties make it easy to access the list of names, values and display labels. Thanks to the following for ideas and reviews: Carlton Gibson, Fran Hrženjak, Ian Foote, Mariusz Felisiak, Shai Berger. Co-authored-by: Shai Berger Co-authored-by: Nick Pope Co-authored-by: Mariusz Felisiak --- tests/model_fields/test_integerfield.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'tests/model_fields/test_integerfield.py') diff --git a/tests/model_fields/test_integerfield.py b/tests/model_fields/test_integerfield.py index c0bb7627cf..606d71057a 100644 --- a/tests/model_fields/test_integerfield.py +++ b/tests/model_fields/test_integerfield.py @@ -184,6 +184,9 @@ class PositiveIntegerFieldTests(IntegerFieldTests): class ValidationTests(SimpleTestCase): + class Choices(models.IntegerChoices): + A = 1 + def test_integerfield_cleans_valid_string(self): f = models.IntegerField() self.assertEqual(f.clean('2', None), 2) @@ -217,3 +220,14 @@ class ValidationTests(SimpleTestCase): f = models.IntegerField(choices=((1, 1),)) with self.assertRaises(ValidationError): f.clean('0', None) + + def test_enum_choices_cleans_valid_string(self): + f = models.IntegerField(choices=self.Choices.choices) + self.assertEqual(f.clean('1', None), 1) + + def test_enum_choices_invalid_input(self): + f = models.IntegerField(choices=self.Choices.choices) + with self.assertRaises(ValidationError): + f.clean('A', None) + with self.assertRaises(ValidationError): + f.clean('3', None) -- cgit v1.3