Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Add test configuration and method tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slavov-v committed Jun 20, 2019
1 parent b03696b commit 7907497
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 2 deletions.
1 change: 0 additions & 1 deletion django_enum_choices/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
name = 'django_enum_choices'
2 changes: 1 addition & 1 deletion django_enum_choices/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_swapped_instance(self, base, **kwargs):
)

if base is EnumCharField:
max_length = self._calculate_max_length()
max_length = self._calculate_max_length(**kwargs)

# Removing `max_length` as it is calculated explicitly.
kwargs.pop('max_length', None)
Expand Down
5 changes: 5 additions & 0 deletions django_enum_choices/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Django==2.2.2
djangorestframework==3.9.4
flake8==3.7.7
pytest==4.6.3
pytest-django==3.5.0
Empty file.
16 changes: 16 additions & 0 deletions django_enum_choices/tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import unicode_literals

DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"tests.testapp.apps.TestAppConfig"
]

SITE_ID = 1

SECRET_KEY = "test"
208 changes: 208 additions & 0 deletions django_enum_choices/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
from enum import Enum

from django.test import TestCase

from django_enum_choices.fields import EnumChoiceFieldBuilder, EnumIntegerField, EnumCharField, EnumChoiceField
from django_enum_choices.exceptions import EnumChoiceFieldException
from .testapp.enumerations import CharTestEnum, IntTestEnum


class EnumChoiceFieldBuilderTests(TestCase):
def test_builder_initializes_string_choice_values(self):
builder = EnumChoiceFieldBuilder(enum_class=CharTestEnum)

self.assertEqual(
builder.enum_class,
CharTestEnum
)
self.assertEqual(
builder._choices,
['first', 'second', 'third']
)

def test_builder_initializes_int_choice_values(self):
builder = EnumChoiceFieldBuilder(enum_class=IntTestEnum)

self.assertEqual(
builder.enum_class,
IntTestEnum
)
self.assertEqual(
builder._choices,
[1, 2, 3]
)

def test_builder_packs_string_choices_correctly(self):
builder = EnumChoiceFieldBuilder(enum_class=CharTestEnum)

packed = builder._pack_choices()

self.assertEqual(
list(packed),
[
('first', 'first'),
('second', 'second'),
('third', 'third')
]
)

def test_builder_packs_int_choices_correctly(self):
builder = EnumChoiceFieldBuilder(enum_class=IntTestEnum)

packed = builder._pack_choices()

self.assertEqual(
list(packed),
[
(1, 1),
(2, 2),
(3, 3)
]
)

def test_get_inferred_base_returns_intfield_instance_when_all_choice_types_are_int(self):
builder = EnumChoiceFieldBuilder(enum_class=IntTestEnum)

base = builder.get_inferred_base()

self.assertIs(base, EnumIntegerField)

def test_get_inferred_base_returns_charfield_instance_when_all_choice_types_are_str(self):
builder = EnumChoiceFieldBuilder(enum_class=CharTestEnum)

base = builder.get_inferred_base()

self.assertIs(base, EnumCharField)

def test_get_inferred_base_raises_exception_when_choice_types_differ(self):
class FailingEnum(Enum):
FOO = 1
BAR = '2'

builder = EnumChoiceFieldBuilder(enum_class=FailingEnum)

with self.assertRaisesMessage(
EnumChoiceFieldException,
'All choices in provided enum class must be of the same type'
):

builder.get_inferred_base()

def test_calculate_max_length_returns_longest_choice_length_if_max_length_not_in_kwargs_and_longer_than_255(
self
):
class LongStringEnum(Enum):
FOO = 'foo'
BAR = 'A' * 256

builder = EnumChoiceFieldBuilder(enum_class=LongStringEnum)

result = builder._calculate_max_length()

self.assertEqual(result, 256)

def test_calculate_max_length_returns_255_when_longest_choice_is_less_than_255_and_max_length_not_in_kwargs(
self
):
class ShortStringEnum(Enum):
FOO = 'foo'
BAR = 'bar'

builder = EnumChoiceFieldBuilder(enum_class=ShortStringEnum)

result = builder._calculate_max_length()

self.assertEqual(result, 255)

def test_calculate_max_length_returns_255_when_max_length_in_kwargs_and_less_than_255(self):
builder = EnumChoiceFieldBuilder(enum_class=CharTestEnum)

result = builder._calculate_max_length(max_length=50)

self.assertEqual(result, 255)

def test_calculate_max_length_returns_max_length_from_kwargs_more_than_255(self):
builder = EnumChoiceFieldBuilder(enum_class=CharTestEnum)

result = builder._calculate_max_length(max_length=256)

self.assertEqual(result, 256)

def test_get_swapped_instance_returns_correct_model_field_instance(self):
with self.subTest('When base is `EnumIntegerField`'):
builder = EnumChoiceFieldBuilder(enum_class=IntTestEnum)

instance = builder.get_swapped_instance(base=EnumIntegerField)

self.assertIsInstance(instance, EnumIntegerField)

with self.subTest('When base is `EnumCharField`'):
builder = EnumChoiceFieldBuilder(enum_class=CharTestEnum)

instance = builder.get_swapped_instance(base=EnumCharField)

self.assertIsInstance(instance, EnumCharField)


class EnumChoiceFieldTests(TestCase):
def test_raises_exception_when_enum_class_is_not_enumeration(self):
class FailingEnum:
FOO = 'foo'
BAR = 'bar'

with self.assertRaisesMessage(
EnumChoiceFieldException,
'`enum_class` argument must be a child of `Enum`'
):
EnumChoiceField(enum_class=FailingEnum)

def test_instance_returns_EnumCharField_when_choices_are_strings(self):
instance = EnumChoiceField(enum_class=CharTestEnum)

self.assertIsInstance(instance, EnumCharField)

def test_instance_returns_EnumIntegerField_when_choices_are_integers(self):
instance = EnumChoiceField(enum_class=IntTestEnum)

self.assertIsInstance(instance, EnumIntegerField)

def test_get_prep_value_returns_primitive_value_when_base_is_integer(self):
instance = EnumChoiceField(enum_class=IntTestEnum)

result = instance.get_prep_value(IntTestEnum.FIRST)

self.assertEqual(result, 1)

def test_get_prep_value_returns_primitive_value_when_base_is_string(self):
instance = EnumChoiceField(enum_class=CharTestEnum)

result = instance.get_prep_value(CharTestEnum.FIRST)

self.assertEqual(result, 'first')

def test_from_db_value_returns_None_when_value_is_none(self):
instance = EnumChoiceField(enum_class=IntTestEnum)

result = instance.from_db_value(None, None, None)

self.assertIsNone(result)

def test_from_db_value_returns_enum_value_when_base_is_integer(self):
instance = EnumChoiceField(enum_class=IntTestEnum)

result = instance.from_db_value(1, None, None)

self.assertEqual(result, IntTestEnum.FIRST)

def test_from_db_value_returns_enum_value_when_base_is_string(self):
instance = EnumChoiceField(enum_class=CharTestEnum)

result = instance.from_db_value('first', None, None)

self.assertEqual(result, CharTestEnum.FIRST)

def test_from_db_value_raises_exception_when_int_value_not_contained_in_enum_class(self):
instance = EnumChoiceField(enum_class=IntTestEnum)

with self.assertRaises(EnumChoiceFieldException):
instance.from_db_value(7, None, None)
6 changes: 6 additions & 0 deletions django_enum_choices/tests/testapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TestAppConfig(AppConfig):
name = 'django_enum_choices.tests.testapp'
verbose_name = 'TestApp'
13 changes: 13 additions & 0 deletions django_enum_choices/tests/testapp/enumerations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from enum import Enum


class CharTestEnum(Enum):
FIRST = 'first'
SECOND = 'second'
THIRD = 'third'


class IntTestEnum(Enum):
FIRST = 1
SECOND = 2
THIRD = 3
13 changes: 13 additions & 0 deletions django_enum_choices/tests/testapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models

from django_enum_choices.fields import EnumChoiceField

from .enumerations import CharTestEnum, IntTestEnum


class IntegerEnumeratedModel(models.Model):
enumeration = EnumChoiceField(enum_class=IntTestEnum)


class StringEnumeratedModel(models.Model):
enumeration = EnumChoiceField(enum_class=CharTestEnum)
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE=tests.settings
django_find_project = false
python_paths = django_enum_choices
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120

0 comments on commit 7907497

Please sign in to comment.