Skip to content

Commit 5e90f79

Browse files
committed
add optional dependency tests
1 parent 44fb200 commit 5e90f79

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

django_enum/choices.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,39 @@ def __init__(self, *args, **kwargs):
7373
f'installed.'
7474
)
7575

76-
DjangoEnumPropertiesMeta = MissingEnumProperties
7776
DjangoSymmetricMixin = MissingEnumProperties
78-
TextChoices = MissingEnumProperties
79-
IntegerChoices = MissingEnumProperties
80-
FloatChoices = MissingEnumProperties
77+
78+
79+
class DjangoEnumPropertiesMeta(EnumPropertiesMeta, ChoicesMeta):
80+
"""
81+
Throw error if metaclass is used without enum-properties
82+
83+
Needs to be strict subclass of same metaclass as Enum to make it to
84+
the ImportError.
85+
"""
86+
def __init__(self, *args, **kwargs):
87+
raise ImportError(
88+
f'{self.__class__.__name__} requires enum-properties to be '
89+
f'installed.'
90+
)
91+
92+
class TextChoices(
93+
DjangoSymmetricMixin,
94+
str,
95+
Choices
96+
):
97+
"""Raises ImportError on class definition"""
98+
99+
class IntegerChoices(
100+
DjangoSymmetricMixin,
101+
int,
102+
Choices
103+
):
104+
"""Raises ImportError on class definition"""
105+
106+
class FloatChoices(
107+
DjangoSymmetricMixin,
108+
float,
109+
Choices
110+
):
111+
"""Raises ImportError on class definition"""

django_enum/tests/tests.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def setUp(self):
5555
pass
5656

5757
def test_integer_choices(self):
58+
self.do_test_integer_choices()
59+
60+
def do_test_integer_choices(self):
5861

5962
EnumTester.objects.create(dj_int_enum=DJIntEnum.ONE)
6063
EnumTester.objects.create(dj_int_enum=DJIntEnum.TWO)
@@ -84,6 +87,9 @@ def test_integer_choices(self):
8487
EnumTester.objects.all().delete()
8588

8689
def test_text_choices(self):
90+
self.do_test_text_choices()
91+
92+
def do_test_text_choices(self):
8793

8894
EnumTester.objects.create(dj_text_enum=DJTextEnum.A)
8995
EnumTester.objects.create(dj_text_enum=DJTextEnum.B)
@@ -1530,3 +1536,78 @@ def test_migration_test_marker_tag():
15301536
assert MIGRATION_TEST_MARKER in TestRemoveBlackMigration.tags
15311537
assert MIGRATION_TEST_MARKER in TestRemoveIntEnumMigration.tags
15321538
assert MIGRATION_TEST_MARKER in TestAddIntEnumMigration.tags
1539+
1540+
1541+
class TestOptionalDependencies(TestChoices):
1542+
1543+
def test_django_filters_missing(self):
1544+
import sys
1545+
from unittest.mock import patch
1546+
from importlib import reload
1547+
1548+
with patch.dict(sys.modules, {'django_filters': None}):
1549+
reload(sys.modules['django_enum.filters'])
1550+
from django_enum.filters import (
1551+
FilterSet as EnumFilterSet,
1552+
EnumFilter
1553+
)
1554+
1555+
class EnumTesterFilter(EnumFilterSet):
1556+
class Meta:
1557+
model = EnumTester
1558+
fields = '__all__'
1559+
1560+
self.assertRaises(ImportError, EnumTesterFilter)
1561+
self.assertRaises(ImportError, EnumFilter)
1562+
1563+
def test_enum_properties_missing(self):
1564+
import sys
1565+
from unittest.mock import patch
1566+
from importlib import reload
1567+
import enum
1568+
1569+
with patch.dict(sys.modules, {'enum_properties': None}):
1570+
reload(sys.modules['django_enum.choices'])
1571+
from django_enum.choices import (
1572+
DjangoEnumPropertiesMeta,
1573+
DjangoSymmetricMixin,
1574+
TextChoices,
1575+
IntegerChoices,
1576+
FloatChoices
1577+
)
1578+
1579+
with self.assertRaises(ImportError):
1580+
class ThrowsEnum(DjangoSymmetricMixin, enum.Enum):
1581+
A = 1
1582+
B = 2
1583+
C = 3
1584+
1585+
with self.assertRaises(ImportError):
1586+
class ThrowsEnum(
1587+
enum.Enum,
1588+
metaclass=DjangoEnumPropertiesMeta
1589+
):
1590+
A = 1
1591+
B = 2
1592+
C = 3
1593+
1594+
with self.assertRaises(ImportError):
1595+
class ThrowsEnum(IntegerChoices):
1596+
A = 1
1597+
B = 2
1598+
C = 3
1599+
1600+
with self.assertRaises(ImportError):
1601+
class ThrowsEnum(TextChoices):
1602+
A = 'A'
1603+
B = 'B'
1604+
C = 'C'
1605+
1606+
with self.assertRaises(ImportError):
1607+
class ThrowsEnum(FloatChoices):
1608+
A = 1.1
1609+
B = 2.2
1610+
C = 3.3
1611+
1612+
self.do_test_integer_choices()
1613+
self.do_test_text_choices()

0 commit comments

Comments
 (0)