diff options
| author | Shai Berger <shai@platonix.com> | 2018-12-31 19:57:35 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-09-04 14:42:49 +0200 |
| commit | 72ebe85a269aab4bdb3829de4846b41f90973c5d (patch) | |
| tree | abad31b5f100466e582a0e69f5b12f7fe2517b33 /tests/model_fields | |
| parent | 25706d728536fea88e12f62856de7c4b569ae282 (diff) | |
Fixed #27910 -- Added enumeration helpers for use in Field.choices.
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 <shai@platonix.com>
Co-authored-by: Nick Pope <nick.pope@flightdataservices.com>
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'tests/model_fields')
| -rw-r--r-- | tests/model_fields/test_charfield.py | 27 | ||||
| -rw-r--r-- | tests/model_fields/test_integerfield.py | 14 |
2 files changed, 41 insertions, 0 deletions
diff --git a/tests/model_fields/test_charfield.py b/tests/model_fields/test_charfield.py index 1b50f01f3a..051be2eeec 100644 --- a/tests/model_fields/test_charfield.py +++ b/tests/model_fields/test_charfield.py @@ -28,9 +28,27 @@ class TestCharField(TestCase): p.refresh_from_db() self.assertEqual(p.title, 'Smile 😀') + def test_assignment_from_choice_enum(self): + class Event(models.TextChoices): + C = 'Carnival!' + F = 'Festival!' + + p1 = Post.objects.create(title=Event.C, body=Event.F) + p1.refresh_from_db() + self.assertEqual(p1.title, 'Carnival!') + self.assertEqual(p1.body, 'Festival!') + self.assertEqual(p1.title, Event.C) + self.assertEqual(p1.body, Event.F) + p2 = Post.objects.get(title='Carnival!') + self.assertEquals(p1, p2) + self.assertEquals(p2.title, Event.C) + class ValidationTests(SimpleTestCase): + class Choices(models.TextChoices): + C = 'c', 'C' + def test_charfield_raises_error_on_empty_string(self): f = models.CharField() with self.assertRaises(ValidationError): @@ -49,6 +67,15 @@ class ValidationTests(SimpleTestCase): with self.assertRaises(ValidationError): f.clean('not a', None) + def test_enum_choices_cleans_valid_string(self): + f = models.CharField(choices=self.Choices.choices, max_length=1) + self.assertEqual(f.clean('c', None), 'c') + + def test_enum_choices_invalid_input(self): + f = models.CharField(choices=self.Choices.choices, max_length=1) + with self.assertRaises(ValidationError): + f.clean('a', None) + def test_charfield_raises_error_on_empty_input(self): f = models.CharField(null=False) with self.assertRaises(ValidationError): 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) |
