Import newer version of django-selectable
[pgweb/local.git] / dep / django-selectable / selectable / tests / test_forms.py
blobed6bac90e60663df38bb6f958c7899713748d6f8
1 from ..forms import BaseLookupForm
2 from .base import BaseSelectableTestCase
5 __all__ = (
6 'BaseLookupFormTestCase',
10 class BaseLookupFormTestCase(BaseSelectableTestCase):
12 def get_valid_data(self):
13 data = {
14 'term': 'foo',
15 'limit': 10,
17 return data
19 def test_valid_data(self):
20 data = self.get_valid_data()
21 form = BaseLookupForm(data)
22 self.assertTrue(form.is_valid(), "%s" % form.errors)
24 def test_invalid_limit(self):
25 """
26 Test giving the form an invalid limit.
27 """
29 data = self.get_valid_data()
30 data['limit'] = 'bar'
31 form = BaseLookupForm(data)
32 self.assertFalse(form.is_valid())
34 def test_no_limit(self):
35 """
36 If SELECTABLE_MAX_LIMIT is set and limit is not given then
37 the form will return SELECTABLE_MAX_LIMIT.
38 """
40 with self.settings(SELECTABLE_MAX_LIMIT=25):
41 data = self.get_valid_data()
42 if 'limit' in data:
43 del data['limit']
44 form = BaseLookupForm(data)
45 self.assertTrue(form.is_valid(), "%s" % form.errors)
46 self.assertEqual(form.cleaned_data['limit'], 25)
48 def test_no_max_set(self):
49 """
50 If SELECTABLE_MAX_LIMIT is not set but given then the form
51 will return the given limit.
52 """
54 with self.settings(SELECTABLE_MAX_LIMIT=None):
55 data = self.get_valid_data()
56 form = BaseLookupForm(data)
57 self.assertTrue(form.is_valid(), "%s" % form.errors)
58 if 'limit' in data:
59 self.assertTrue(form.cleaned_data['limit'], data['limit'])
61 def test_no_max_set_not_given(self):
62 """
63 If SELECTABLE_MAX_LIMIT is not set and not given then the form
64 will return no limit.
65 """
67 with self.settings(SELECTABLE_MAX_LIMIT=None):
68 data = self.get_valid_data()
69 if 'limit' in data:
70 del data['limit']
71 form = BaseLookupForm(data)
72 self.assertTrue(form.is_valid(), "%s" % form.errors)
73 self.assertFalse(form.cleaned_data.get('limit'))
75 def test_over_limit(self):
76 """
77 If SELECTABLE_MAX_LIMIT is set and limit given is greater then
78 the form will return SELECTABLE_MAX_LIMIT.
79 """
81 with self.settings(SELECTABLE_MAX_LIMIT=25):
82 data = self.get_valid_data()
83 data['limit'] = 125
84 form = BaseLookupForm(data)
85 self.assertTrue(form.is_valid(), "%s" % form.errors)
86 self.assertEqual(form.cleaned_data['limit'], 25)