Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negative values with IntEnumField #895

Merged
merged 3 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changelog
- Add `Model.raw` method to support the raw sql query.
- Fix `QuerySet` subclass being lost when `_clone` is run on the instance.
- Fix bug in `.values` with `source_field`. (#844)
- Allow negative values with `IntEnumField`. (#889)

0.17.7
------
Expand Down
20 changes: 18 additions & 2 deletions tests/fields/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class BadIntEnum1(IntEnum):


class BadIntEnum2(IntEnum):
python_programming = -32769
database_design = 2
system_administration = 3


class BadIntEnumIfGenerated(IntEnum):
python_programming = -1
database_design = 2
system_administration = 3
Expand Down Expand Up @@ -82,16 +88,26 @@ def test_char_fails(self):

def test_range1_fails(self):
with self.assertRaisesRegex(
ConfigurationError, "The valid range of IntEnumField's values is 0..32767!"
ConfigurationError, "The valid range of IntEnumField's values is -32768..32767!"
):
IntEnumField(BadIntEnum1)

def test_range2_fails(self):
with self.assertRaisesRegex(
ConfigurationError, "The valid range of IntEnumField's values is 0..32767!"
ConfigurationError, "The valid range of IntEnumField's values is -32768..32767!"
):
IntEnumField(BadIntEnum2)

def test_range3_generated_fails(self):
with self.assertRaisesRegex(
ConfigurationError, "The valid range of IntEnumField's values is 1..32767!"
):
IntEnumField(BadIntEnumIfGenerated, generated=True)

def test_range3_manual(self):
fld = IntEnumField(BadIntEnumIfGenerated)
self.assertIs(fld.enum_type, BadIntEnumIfGenerated)

def test_auto_description(self):
fld = IntEnumField(testmodels.Service)
self.assertEqual(
Expand Down
13 changes: 10 additions & 3 deletions tortoise/fields/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,23 @@ class _db_mysql:

class IntEnumFieldInstance(SmallIntField):
def __init__(
self, enum_type: Type[IntEnum], description: Optional[str] = None, **kwargs: Any
self,
enum_type: Type[IntEnum],
description: Optional[str] = None,
generated: bool = False,
**kwargs: Any,
) -> None:
# Validate values
minimum = 1 if generated else -32768
for item in enum_type:
try:
value = int(item.value)
except ValueError:
raise ConfigurationError("IntEnumField only supports integer enums!")
if not 0 <= value < 32768:
raise ConfigurationError("The valid range of IntEnumField's values is 0..32767!")
if not minimum <= value < 32768:
raise ConfigurationError(
"The valid range of IntEnumField's values is {}..32767!".format(minimum)
)

# Automatic description for the field if not specified by the user
if description is None:
Expand Down