Commits

Mark Lavin  committed 53239d6

Testing limiting results. See #5.

  • Participants
  • Parent commits 16f72d4

Comments (0)

Files changed (5)

File selectable/forms/base.py

 __all__ = ('BaseLookupForm', )
 
 
-DEFAULT_LIMIT = getattr(settings, 'SELECTABLE_MAX_LIMIT', 25)
-
-
 class BaseLookupForm(forms.Form):
     term = forms.CharField(required=False)
     limit = forms.IntegerField(required=False, min_value=1)
 
     def clean_limit(self):
         "Ensure given limit is less than default if defined"
+        DEFAULT_LIMIT = getattr(settings, 'SELECTABLE_MAX_LIMIT', 25)
         limit = self.cleaned_data.get('limit', None)
         if DEFAULT_LIMIT and (not limit or limit > DEFAULT_LIMIT):
             limit = DEFAULT_LIMIT

File selectable/tests/__init__.py

 
 from selectable.tests.base import *
 from selectable.tests.fields import *
+from selectable.tests.forms import *
 from selectable.tests.views import *
 from selectable.tests.widgets import *

File selectable/tests/base.py

 import random
 import string
 
+from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.test import TestCase
 
 )
 
 
+class PatchSettingsMixin(object):
+    def setUp(self):
+        super(PatchSettingsMixin, self).setUp()
+        self.is_limit_set = hasattr(settings, 'SELECTABLE_MAX_LIMIT')
+        if self.is_limit_set:
+            self.original_limit = settings.SELECTABLE_MAX_LIMIT
+        settings.SELECTABLE_MAX_LIMIT = 25
+
+    def tearDown(self):
+        super(PatchSettingsMixin, self).tearDown()
+        if self.is_limit_set:
+            settings.SELECTABLE_MAX_LIMIT = self.original_limit
+
+
 class BaseSelectableTestCase(TestCase):
     urls = 'selectable.tests.urls'
 

File selectable/tests/forms.py

+from django.conf import settings
+
+from selectable.forms import BaseLookupForm
+from selectable.tests.base import BaseSelectableTestCase, PatchSettingsMixin
+
+
+__all__ = (
+    'BaseLookupFormTestCase',
+)
+
+
+class BaseLookupFormTestCase(PatchSettingsMixin, BaseSelectableTestCase):
+
+    def get_valid_data(self):
+        data = {
+            'term': 'foo',
+            'limit': 10,
+        }
+        return data
+
+    def test_valid_data(self):
+        data = self.get_valid_data()
+        form = BaseLookupForm(data)
+        self.assertTrue(form.is_valid(), u"%s" % form.errors)
+
+    def test_invalid_limit(self):
+        """
+        Test giving the form an invalid limit.
+        """
+
+        data = self.get_valid_data()
+        data['limit'] = 'bar'
+        form = BaseLookupForm(data)
+        self.assertFalse(form.is_valid())
+
+    def test_no_limit(self):
+        """
+        If SELECTABLE_MAX_LIMIT is set and limit is not given then
+        the form will return SELECTABLE_MAX_LIMIT.
+        """
+
+        data = self.get_valid_data()
+        if 'limit' in data:
+            del data['limit']
+        form = BaseLookupForm(data)
+        self.assertTrue(form.is_valid(), u"%s" % form.errors)
+        self.assertEqual(form.cleaned_data['limit'], settings.SELECTABLE_MAX_LIMIT)
+
+    def test_no_max_set(self):
+        """
+        If SELECTABLE_MAX_LIMIT is not set but given then the form
+        will return the given limit.
+        """
+
+        settings.SELECTABLE_MAX_LIMIT = None
+        data = self.get_valid_data()
+        form = BaseLookupForm(data)
+        self.assertTrue(form.is_valid(), u"%s" % form.errors)
+        if 'limit' in data:
+            self.assertTrue(form.cleaned_data['limit'], data['limit'])
+
+    def test_no_max_set_not_given(self):
+        """
+        If SELECTABLE_MAX_LIMIT is not set and not given then the form
+        will return no limit.
+        """
+
+        settings.SELECTABLE_MAX_LIMIT = None
+        data = self.get_valid_data()
+        if 'limit' in data:
+            del data['limit']
+        form = BaseLookupForm(data)
+        self.assertTrue(form.is_valid(), u"%s" % form.errors)
+        self.assertFalse(form.cleaned_data.get('limit'))
+
+    def test_over_limit(self):
+        """
+        If SELECTABLE_MAX_LIMIT is set and limit given is greater then
+        the form will return SELECTABLE_MAX_LIMIT.
+        """
+
+        data = self.get_valid_data()
+        data['limit'] = settings.SELECTABLE_MAX_LIMIT + 100
+        form = BaseLookupForm(data)
+        self.assertTrue(form.is_valid(), u"%s" % form.errors)
+        self.assertEqual(form.cleaned_data['limit'], settings.SELECTABLE_MAX_LIMIT)

File selectable/tests/views.py

+from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.http import HttpResponseNotFound, HttpResponseServerError
 from django.utils import simplejson as json
 
 from selectable.tests import ThingLookup
-from selectable.tests.base import BaseSelectableTestCase
+from selectable.tests.base import BaseSelectableTestCase, PatchSettingsMixin
 
 
 __all__ = (
     return HttpResponseServerError()
 
 
-class SelectableViewTest(BaseSelectableTestCase):
+class SelectableViewTest(PatchSettingsMixin, BaseSelectableTestCase):
     
     def setUp(self):
         super(SelectableViewTest, self).setUp()
         unknown_url = reverse('selectable-lookup', args=["XXXXXXX"])
         response = self.client.get(unknown_url)
         self.assertEqual(response.status_code, 404)
+
+    def test_basic_limit(self):
+        for i in range(settings.SELECTABLE_MAX_LIMIT):
+            self.create_thing(data={'name': 'Thing%s' % i})
+        response = self.client.get(self.url)
+        data = json.loads(response.content)
+        self.assertEqual(len(data), settings.SELECTABLE_MAX_LIMIT + 1)
+        last_item = data[-1]
+        self.assertTrue('page' in last_item)