Skip to content

Commit

Permalink
Add unittest for the custom int enum field
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Nov 24, 2024
1 parent 0d686bb commit 678e34b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ Changelog
0.22
====

0.22.1
------

Added
^^^^^
- Add IntEnumFieldMixin to make it easy to Custom IntEnumField (#1781)

0.22.0
------
Fixed
Expand Down
17 changes: 10 additions & 7 deletions examples/enum_fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from enum import Enum, IntEnum
from typing import Type, cast

from tortoise import Tortoise, fields, run_async
from tortoise.fields.data import IntEnumFieldMixin, IntField
from tortoise.fields.data import IntEnumFieldMixin, IntEnumType, IntField
from tortoise.models import Model


Expand All @@ -19,21 +20,23 @@ class Currency(str, Enum):

class Protocol(IntEnum):
A = 10000
B = 80000
B = 80000 # >32767, beyond the value range of fields.IntEnumField


class Int32EnumInstance(IntEnumFieldMixin, IntField):
class IntEnumInstance(IntEnumFieldMixin, IntField):
pass


def Int32EnumField(enum_type, **kwargs):
return Int32EnumInstance(enum_type, **kwargs)
def IntEnumField(enum_type: Type[IntEnumType], **kwargs) -> IntEnumType:
return cast(IntEnumType, IntEnumInstance(enum_type, **kwargs))


class EnumFields(Model):
service: Service = fields.IntEnumField(Service)
currency: Currency = fields.CharEnumField(Currency, default=Currency.HUF)
protocol: Protocol = Int32EnumField(Protocol)
# When each value of the enum_type is between [-32768, 32767], use the fields.IntEnumField
service: Service = fields.IntEnumField(Service)
# Else, you can use a custom Field
protocol: Protocol = IntEnumField(Protocol)


async def run():
Expand Down
14 changes: 13 additions & 1 deletion tests/fields/test_enum.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from enum import IntEnum

from examples.enum_fields import IntEnumField as CustomIntEnumField
from tests import testmodels
from tortoise.contrib import test
from tortoise.exceptions import ConfigurationError, IntegrityError
from tortoise.fields import CharEnumField, IntEnumField
from tortoise.fields import CharEnumField, IntEnumField, IntField


class BadIntEnum1(IntEnum):
Expand All @@ -24,6 +25,12 @@ class BadIntEnumIfGenerated(IntEnum):
system_administration = 3


class BadCustomIntEnum(IntEnum):
python_programming = IntField.VALUE_RANGE[1] + 1
database_design = 2
system_administration = 3


class TestIntEnumFields(test.TestCase):
async def test_empty(self):
with self.assertRaises(IntegrityError):
Expand Down Expand Up @@ -118,6 +125,11 @@ def test_manual_description(self):
fld = IntEnumField(testmodels.Service, description="foo")
self.assertEqual(fld.description, "foo")

def test_custom_int_enum_field(self):
assert CustomIntEnumField(BadIntEnum1)
with self.assertRaises(ConfigurationError):
CustomIntEnumField(BadCustomIntEnum)


class TestCharEnumFields(test.TestCase):
async def test_create(self):
Expand Down

0 comments on commit 678e34b

Please sign in to comment.