forked from carltongibson/django-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_tests.py
137 lines (113 loc) · 4.57 KB
/
widget_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from __future__ import absolute_import
from __future__ import unicode_literals
from django.test import TestCase
from django.forms import TextInput, Select
from django_filters.widgets import RangeWidget
from django_filters.widgets import LinkWidget
from django_filters.widgets import LookupTypeWidget
class LookupTypeWidgetTests(TestCase):
def test_widget_requires_field(self):
with self.assertRaises(TypeError):
LookupTypeWidget()
def test_widget_render(self):
widgets = [TextInput(), Select(choices=(('a', 'a'), ('b', 'b')))]
w = LookupTypeWidget(widgets)
self.assertHTMLEqual(w.render('price', ''), """
<input name="price_0" type="text" />
<select name="price_1">
<option value="a">a</option>
<option value="b">b</option>
</select>""")
self.assertHTMLEqual(w.render('price', None), """
<input name="price_0" type="text" />
<select name="price_1">
<option value="a">a</option>
<option value="b">b</option>
</select>""")
self.assertHTMLEqual(w.render('price', ['2', 'a']), """
<input name="price_0" type="text" value="2" />
<select name="price_1">
<option selected="selected" value="a">a</option>
<option value="b">b</option>
</select>""")
class LinkWidgetTests(TestCase):
def test_widget_without_choices(self):
w = LinkWidget()
self.assertEqual(len(w.choices), 0)
self.assertHTMLEqual(w.render('price', ''), """<ul />""")
def test_widget(self):
choices = (
('test-val1', 'test-label1'),
('test-val2', 'test-label2'),
)
w = LinkWidget(choices=choices)
self.assertEqual(len(w.choices), 2)
self.assertHTMLEqual(w.render('price', ''), """
<ul>
<li><a href="?price=test-val1">test-label1</a></li>
<li><a href="?price=test-val2">test-label2</a></li>
</ul>""")
self.assertHTMLEqual(w.render('price', None), """
<ul>
<li><a href="?price=test-val1">test-label1</a></li>
<li><a href="?price=test-val2">test-label2</a></li>
</ul>""")
self.assertHTMLEqual(w.render('price', 'test-val1'), """
<ul>
<li><a class="selected"
href="?price=test-val1">test-label1</a></li>
<li><a href="?price=test-val2">test-label2</a></li>
</ul>""")
def test_widget_with_option_groups(self):
choices = (
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
),
('unknown', 'Unknown'),
)
w = LinkWidget(choices=choices)
self.assertHTMLEqual(w.render('media', ''), """
<ul>
<li><a href="?media=vinyl">Vinyl</a></li>
<li><a href="?media=cd">CD</a></li>
<li><a href="?media=vhs">VHS Tape</a></li>
<li><a href="?media=dvd">DVD</a></li>
<li><a href="?media=unknown">Unknown</a></li>
</ul>""")
def test_widget_with_blank_choice(self):
choices = (
('', '---------'),
('test-val1', 'test-label1'),
('test-val2', 'test-label2'),
)
w = LinkWidget(choices=choices)
self.assertHTMLEqual(w.render('price', ''), """
<ul>
<li><a class="selected" href="?price=">All</a></li>
<li><a href="?price=test-val1">test-label1</a></li>
<li><a href="?price=test-val2">test-label2</a></li>
</ul>""")
def test_widget_value_from_datadict(self):
w = LinkWidget()
data = {'price': 'test-val1'}
result = w.value_from_datadict(data, {}, 'price')
self.assertEqual(result, 'test-val1')
class RangeWidgetTests(TestCase):
def test_widget(self):
w = RangeWidget()
self.assertEqual(len(w.widgets), 2)
self.assertHTMLEqual(w.render('price', ''), """
<input type="text" name="price_0" />
-
<input type="text" name="price_1" />""")
self.assertHTMLEqual(w.render('price', slice(5.99, 9.99)), """
<input type="text" name="price_0" value="5.99" />
-
<input type="text" name="price_1" value="9.99" />""")