Commits

Tim Savage  committed 7038d86

Changed to setuptools (for develop support), added test cases over choices, fixed several bugs

  • Participants
  • Parent commits d6c878b

Comments (0)

Files changed (5)

File django_extras/db/models/choices.py

 from collections import OrderedDict
+from django.db.models.fields import NOT_PROVIDED
 
 
 class ChoiceEnum(object):
         MY_CHOICES % 'value'
 
     """
-    __slots__ = ('__default', '__value_map', '__choices', '__max_length')
+    __slots__ = ('__choices', '__default', '__max_length', '__value_map')
 
     def __init__(self, *args, **entries):
         if args:
         """
         Resolve a value to it's display version.
         """
-        return self._value_map[value]
+        return self.__value_map[value]

File django_extras/tests/__init__.py

 from django_extras.tests.contrib.auth import *
 from django_extras.tests.core.types import *
 from django_extras.tests.core.validators import *
+from django_extras.tests.db.choices import *
 from django_extras.tests.forms.fields import *
 from django_extras.tests.http.responses import *
 from django_extras.tests.middleware.timing import *

File django_extras/tests/db/__init__.py

Empty file added.

File django_extras/tests/db/choices.py

+from django import test
+from django.db.models.fields import NOT_PROVIDED
+from django_extras.db.models.choices import ChoiceEnum
+
+
+class ChoicesTestCase(test.TestCase):
+    def test_kwargs(self):
+        target = ChoiceEnum(
+            OPTION_ONE = ('value', 'Verbose value'),
+            OPTION_TWO = ('value2', 'Verbose value 2', True), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual('value', target.OPTION_ONE)
+        self.assertEqual('value2', target.OPTION_TWO)
+
+    def test_args(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', ('value', 'Verbose value')),
+            ('OPTION_TWO', ('value2', 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual('value', target.OPTION_ONE)
+        self.assertEqual('value2', target.OPTION_TWO)
+
+    def test_max_length(self):
+        target = ChoiceEnum(
+            OPTION_ONE = ('value', 'Verbose value'),
+            OPTION_TWO = ('value2', 'Verbose value 2', True), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual(6, target.max_length)
+
+    def test_default_set(self):
+        target = ChoiceEnum(
+            OPTION_ONE = ('value', 'Verbose value'),
+            OPTION_TWO = ('value2', 'Verbose value 2', True), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual('value2', target.default)
+
+    def test_default_not_set(self):
+        target = ChoiceEnum(
+            OPTION_ONE = ('value', 'Verbose value'),
+            OPTION_TWO = ('value2', 'Verbose value 2'),
+        )
+
+        self.assertEqual(NOT_PROVIDED, target.default)
+
+    def test_kwargs_string_value(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', ('value', 'Verbose value')),
+            ('OPTION_TWO', ('value2', 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual({
+            'choices': target,
+            'default': 'value2',
+            'max_length': 6
+        }, target.kwargs)
+
+    def test_kwargs_int_value(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', (1, 'Verbose value')),
+            ('OPTION_TWO', (2, 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual({
+            'choices': target,
+            'default': 2
+        }, target.kwargs)
+
+    def test_resolve_value(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', (1, 'Verbose value')),
+            ('OPTION_TWO', (2, 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual('Verbose value', target.resolve_value(1))
+
+    def test_mod(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', (1, 'Verbose value')),
+            ('OPTION_TWO', (2, 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual('Verbose value 2', target % 2)
+
+    def test_iterator(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', (1, 'Verbose value')),
+            ('OPTION_TWO', (2, 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual([(1, 'Verbose value'), (2, 'Verbose value 2'),], list(target))
+
+    def test_getattr(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', (1, 'Verbose value')),
+            ('OPTION_TWO', (2, 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual(1, target.OPTION_ONE)
+
+    def test_getattr_display(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', (1, 'Verbose value')),
+            ('OPTION_TWO', (2, 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertEqual('Verbose value 2', target.OPTION_TWO__display)
+
+    def test_contains(self):
+        target = ChoiceEnum(
+            ('OPTION_ONE', (1, 'Verbose value')),
+            ('OPTION_TWO', (2, 'Verbose value 2', True)), # Default, the value can be anything ;)
+        )
+
+        self.assertTrue(1 in target)
+        self.assertFalse(3 in target)
+
+    def test_init_no_entries(self):
+        self.assertRaises(ValueError, lambda: ChoiceEnum())
+
+    def test_init_invalid_entry(self):
+        self.assertRaises(TypeError, lambda: ChoiceEnum(
+            OPTION_ONE = ('value', 'Verbose value'),
+            OPTION_TWO = 'value2'
+        ))
+
+        self.assertRaises(ValueError, lambda: ChoiceEnum(
+            OPTION_ONE = ('value', 'Verbose value'),
+            OPTION_TWO = ('value2', 'Verbose value 2', True, 1)
+        ))
-from distutils.core import setup
+from setuptools import setup
 import os
 
 # Hack to force dist utils to install data files in correct location along with