Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
Seems we're up to 92% now.
  • Loading branch information
akx committed Feb 23, 2015
1 parent e493237 commit 8e91e67
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion enumfields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def to_python(self, value):
return value
if value == m.value or str(value) == str(m.value) or str(value) == str(m):
return m
raise ValidationError('%s is not a valid value for enum %s' % (value, self.enum))
raise ValidationError('%s is not a valid value for enum %s' % (value, self.enum), code="invalid_enum_value")

def get_prep_value(self, value):
return None if value is None else value.value
Expand Down
10 changes: 10 additions & 0 deletions tests/test_django_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ def test_zero_enum_loads():

m = MyModel.objects.get(id=m.id)
assert m.zero_field == MyModel.ZeroEnum.ZERO


def test_serialization():
from django.core.serializers.python import Serializer as PythonSerializer
m = MyModel(color=MyModel.Color.RED, taste=MyModel.Taste.SALTY)
ser = PythonSerializer()
ser.serialize([m])
fields = ser.getvalue()[0]["fields"]
assert fields["color"] == m.color.value
assert fields["taste"] == m.taste.value
19 changes: 15 additions & 4 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# -- encoding: UTF-8 --
from django.core.exceptions import ValidationError

from django.forms import BaseForm
from django.utils.translation import ugettext_lazy
from enumfields import Enum, EnumField
import pytest
import six
from six import u


class Color(Enum):
__order__ = 'RED GREEN BLUE'

GREEN = 'g'
RED = 'r'
GREEN = 'g'
BLUE = 'b'

class Labels:
RED = 'Reddish'
BLUE = ugettext_lazy(u'bluë')
BLUE = ugettext_lazy(u('bluë'))


def test_choice_ordering():
EXPECTED_CHOICES = (
('r', 'Reddish'),
('g', 'Green'),
('b', u'bluë'),
('b', u('bluë')),
)
for ((ex_key, ex_val), (key, val)) in zip(EXPECTED_CHOICES, Color.choices()):
assert key == ex_key
Expand All @@ -41,7 +44,7 @@ def test_automatic_labels():
def test_lazy_labels():
# Lazy label
assert isinstance(six.text_type(Color.BLUE), six.string_types)
assert six.text_type(Color.BLUE) == u'bluë'
assert six.text_type(Color.BLUE) == u('bluë')

def test_formfield_labels():
# Formfield choice label
Expand All @@ -58,3 +61,11 @@ def test_formfield_functionality():
form = form_cls(data={"color": "r"})
assert not form.errors
assert form.cleaned_data["color"] == Color.RED

def test_invalid_to_python_fails():
with pytest.raises(ValidationError) as ve:
EnumField(Color).to_python("invalid")
assert ve.value.code == "invalid_enum_value"

def test_import_by_string():
assert EnumField("tests.test_enums.Color").enum == Color

0 comments on commit 8e91e67

Please sign in to comment.