Skip to content

Commit

Permalink
Merge pull request #681 from awesto/releases/0.11.x
Browse files Browse the repository at this point in the history
fixed failing unit tests
  • Loading branch information
jrief committed Nov 2, 2017
2 parents c7aabe4 + 79d9afc commit 2e13fd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions example/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class MyChoices(ChoiceEnum):
B = 1


class MyModel(models.Model):
f = ChoiceEnumField(enum_type=MyChoices)

class Meta:
app_label = 'shop'
managed = False


class EnumTest(TestCase):
def test_enum(self):
choice_a = MyChoices.A
Expand Down Expand Up @@ -99,12 +107,9 @@ def test_get_prep_value(self):
f = ChoiceEnumField(enum_type=MyChoices)
self.assertEqual(f.get_prep_value(MyChoices.A), 0)
self.assertEqual(f.get_prep_value(MyChoices.B), 1)
with self.assertRaises(ValueError):
f.get_prep_value('X')

def test_value_to_string(self):
f = ChoiceEnumField(enum_type=MyChoices)
self.assertEqual(f.value_to_string(MyChoices.A), 'A')
self.assertEqual(f.value_to_string(MyChoices.B), 'B')
obj = MyModel(f=MyChoices.A)
self.assertEqual(ChoiceEnumField(name='f').value_to_string(obj), 'A')
with self.assertRaises(ValueError):
f.value_to_string(0)
ChoiceEnumField(name='f').value_to_string(0)
2 changes: 1 addition & 1 deletion shop/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def to_python(self, state):
return self.enum_type(state)

def value_to_string(self, obj):
value = getattr(obj, self.name)
value = getattr(obj, self.name, obj)
if not isinstance(value, self.enum_type):
raise ValueError("Value must be of type {}".format(self.enum_type))
return value.name

0 comments on commit 2e13fd9

Please sign in to comment.