From 7cbfbdb4539b7d4b5c3863933130b23ad4ce1183 Mon Sep 17 00:00:00 2001 From: Ryan Kaskel Date: Thu, 10 May 2012 12:07:42 +0100 Subject: [PATCH] len support for Choices. --- model_utils/__init__.py | 3 +++ model_utils/tests/tests.py | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/model_utils/__init__.py b/model_utils/__init__.py index 5ad4572..ad47bc6 100644 --- a/model_utils/__init__.py +++ b/model_utils/__init__.py @@ -130,6 +130,9 @@ class Choices(object): def __getitem__(self, index): return self._choices[index] + def __len__(self): + return len(self._choices) + def __repr__(self): return '%s(%s)' % (self.__class__.__name__, ', '.join(("%s" % str(i) for i in self._full))) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index 3b5a46a..531d735 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -183,6 +183,10 @@ class ChoicesTests(TestCase): self.assertEquals(tuple(self.STATUS), (('DRAFT', 'DRAFT'), ('PUBLISHED', 'PUBLISHED'))) + def test_len(self): + self.assertEqual(len(self.STATUS), 2) + + def test_repr(self): self.assertEquals(repr(self.STATUS), "Choices(" @@ -224,6 +228,10 @@ class LabelChoicesTests(ChoicesTests): self.assertEquals(self.STATUS.DRAFT, 'DRAFT') + def test_len(self): + self.assertEqual(len(self.STATUS), 3) + + def test_repr(self): self.assertEquals(repr(self.STATUS), "Choices(" @@ -256,6 +264,10 @@ class IdentifierChoicesTests(ChoicesTests): self.assertEquals(self.STATUS.DRAFT, 0) + def test_len(self): + self.assertEqual(len(self.STATUS), 3) + + def test_repr(self): self.assertEquals(repr(self.STATUS), "Choices("