-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #27910 -- Added a ChoiceEnum class for use in field choices
The class can serve as a base class for user enums, supporting translatable human-readable names, or names automatically inferred from the enum member name. A ChoiceIntEnum is also provided. Thanks Ian Foote for review.
- Loading branch information
Showing
7 changed files
with
236 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,6 +190,7 @@ elidable | |
encodings | ||
Endian | ||
Enero | ||
Enum | ||
environ | ||
esque | ||
Ess | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
from django.test import SimpleTestCase | ||
|
||
|
||
class TestChoiceEnumDefinition(SimpleTestCase): | ||
|
||
def test_simple_definition_int(self): | ||
|
||
class MyEnum(models.ChoiceIntEnum): | ||
ONE = 1, 'One' | ||
TWO = 2, 'Two' | ||
|
||
self.assertEqual(MyEnum.ONE, 1) | ||
self.assertEqual(MyEnum.TWO.display, 'Two') | ||
self.assertSequenceEqual(MyEnum.choices, [(1, 'One'), (2, 'Two')]) | ||
|
||
def test_simple_definition_int_default_display(self): | ||
class MyEnum(models.ChoiceIntEnum): | ||
ONE = 1, 'One' | ||
ONE_PLUS_ONE = 2 | ||
THREE = 3 | ||
|
||
self.assertEqual(MyEnum.ONE.display, 'One') | ||
self.assertEqual(MyEnum.ONE_PLUS_ONE.display, 'One Plus One') | ||
self.assertSequenceEqual(MyEnum.choices, [(1, 'One'), (2, 'One Plus One'), (3, 'Three')]) | ||
|
||
def test_simple_definition_str(self): | ||
|
||
class MyEnum(str, models.ChoiceEnum): | ||
ONE = '1', 'One' | ||
TWO = '2', 'Two' | ||
|
||
self.assertEqual(MyEnum.ONE, '1') | ||
self.assertEqual(MyEnum.TWO.display, 'Two') | ||
self.assertSequenceEqual(MyEnum.choices, [('1', 'One'), ('2', 'Two')]) | ||
|
||
def test_simple_definition_str_default_display(self): | ||
class MyEnum(str, models.ChoiceEnum): | ||
ONE = '1', 'One' | ||
ONE_PLUS_ONE = '2' | ||
THREE = '3' | ||
|
||
self.assertEqual(MyEnum.ONE.display, 'One') | ||
self.assertEqual(MyEnum.ONE_PLUS_ONE.display, 'One Plus One') | ||
self.assertSequenceEqual(MyEnum.choices, [('1', 'One'), ('2', 'One Plus One'), ('3', 'Three')]) | ||
|
||
def test_invalid_definition(self): | ||
with self.assertRaisesRegex(ValueError, '.*[Ii]nvalid.*ONE.*'): | ||
class MyEnum(models.ChoiceIntEnum): | ||
ONE = 1, 'One', 'One too many' | ||
|
||
|
||
class TestChoiceEnumValidation(SimpleTestCase): | ||
|
||
class IntEnum(models.ChoiceIntEnum): | ||
ONE = 1, 'One' | ||
TWO = 2, 'Two' | ||
|
||
class StrEnum(str, models.ChoiceEnum): | ||
ONE = '1', 'One' | ||
ONE_PLUS_ONE = '2' | ||
THREE = '3' | ||
|
||
def test_validates_valid_value(self): | ||
self.IntEnum.validate(1) | ||
self.StrEnum.validate('2') | ||
|
||
def test_invalidates_correctly_typed_value(self): | ||
with self.assertRaises(ValidationError): | ||
self.IntEnum.validate(7) | ||
with self.assertRaises(ValidationError): | ||
self.StrEnum.validate('monster') | ||
|
||
def test_invalidates_incorrectly_typed_value(self): | ||
with self.assertRaises(ValidationError): | ||
self.IntEnum.validate('not int') | ||
with self.assertRaises(ValidationError): | ||
self.StrEnum.validate(b'2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters