Choices should be indexable as if it were a two-tuple

This commit is contained in:
Carl Meyer 2010-04-16 00:11:31 -04:00
parent acbf46abb2
commit f64f16b053
2 changed files with 13 additions and 1 deletions

View file

@ -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)))

View file

@ -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):