From f64f16b05391191025626de5372c41c71a469a51 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Fri, 16 Apr 2010 00:11:31 -0400 Subject: [PATCH] Choices should be indexable as if it were a two-tuple --- model_utils/__init__.py | 3 +++ model_utils/tests/tests.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/model_utils/__init__.py b/model_utils/__init__.py index 8b67332..3cfa1ff 100644 --- a/model_utils/__init__.py +++ b/model_utils/__init__.py @@ -102,6 +102,9 @@ class Choices(object): except KeyError: raise AttributeError(attname) + def __getitem__(self, index): + return self._choices[index] + def __repr__(self): return '%s(%s)' % (self.__class__.__name__, ', '.join(("'%s'" % i[0] for i in self._choices))) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index b6e319c..609f796 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -105,6 +105,9 @@ class ChoicesTests(TestCase): def test_getattr(self): self.assertEquals(self.STATUS.DRAFT, 'DRAFT') + def test_indexing(self): + self.assertEquals(self.STATUS[1], ('PUBLISHED', 'PUBLISHED')) + def test_iteration(self): self.assertEquals(tuple(self.STATUS), (('DRAFT', 'DRAFT'), ('PUBLISHED', 'PUBLISHED'))) @@ -124,9 +127,15 @@ class LabelChoicesTests(ChoicesTests): ('DELETED', 'DELETED')) ) - def test_display(self): + def test_indexing(self): + self.assertEquals(self.STATUS[1], ('PUBLISHED', 'is published')) + + def test_default(self): self.assertEquals(self.STATUS.DELETED, 'DELETED') + def test_provided(self): + self.assertEquals(self.STATUS.DRAFT, 'DRAFT') + class InheritanceCastModelTests(TestCase): def setUp(self):