forked from carltongibson/django-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fields.py
292 lines (222 loc) · 9.4 KB
/
test_fields.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import decimal
from datetime import datetime, time, timedelta, tzinfo
import pytz
from django import forms
from django.test import TestCase, override_settings
from django.utils import timezone
from django_filters.fields import (
BaseCSVField,
BaseRangeField,
DateRangeField,
DateTimeRangeField,
IsoDateTimeField,
IsoDateTimeRangeField,
Lookup,
LookupChoiceField,
RangeField,
TimeRangeField
)
from django_filters.widgets import BaseCSVWidget, CSVWidget, RangeWidget
def to_d(float_value):
return decimal.Decimal('%.2f' % float_value)
class LookupTests(TestCase):
def test_empty_attrs(self):
with self.assertRaisesMessage(ValueError, ''):
Lookup(None, None)
with self.assertRaisesMessage(ValueError, ''):
Lookup('', '')
def test_empty_value(self):
with self.assertRaisesMessage(ValueError, ''):
Lookup('', 'exact')
def test_empty_lookup_expr(self):
with self.assertRaisesMessage(ValueError, ''):
Lookup('Value', '')
class RangeFieldTests(TestCase):
def test_field(self):
f = RangeField()
self.assertEqual(len(f.fields), 2)
def test_clean(self):
w = RangeWidget()
f = RangeField(widget=w, required=False)
self.assertEqual(
f.clean(['12.34', '55']),
slice(to_d(12.34), to_d(55)))
self.assertIsNone(f.clean([]))
class DateRangeFieldTests(TestCase):
def test_field(self):
f = DateRangeField()
self.assertEqual(len(f.fields), 2)
@override_settings(USE_TZ=False)
def test_clean(self):
w = RangeWidget()
f = DateRangeField(widget=w, required=False)
self.assertEqual(
f.clean(['2015-01-01', '2015-01-10']),
slice(datetime(2015, 1, 1, 0, 0, 0),
datetime(2015, 1, 10, 23, 59, 59, 999999)))
self.assertIsNone(f.clean([]))
class DateTimeRangeFieldTests(TestCase):
def test_field(self):
f = DateTimeRangeField()
self.assertEqual(len(f.fields), 2)
@override_settings(USE_TZ=False)
def test_clean(self):
w = RangeWidget()
f = DateTimeRangeField(widget=w)
self.assertEqual(
f.clean(['2015-01-01 10:30', '2015-01-10 8:45']),
slice(datetime(2015, 1, 1, 10, 30, 0),
datetime(2015, 1, 10, 8, 45, 0)))
class IsoDateTimeRangeFieldTests(TestCase):
def test_field(self):
f = IsoDateTimeRangeField()
self.assertEqual(len(f.fields), 2)
@override_settings(USE_TZ=False)
def test_clean(self):
w = RangeWidget()
f = IsoDateTimeRangeField(widget=w)
self.assertEqual(
f.clean(['2015-01-01T10:30:01.123000+01:00', '2015-01-10T08:45:02.345000+01:00']),
slice(datetime(2015, 1, 1, 9, 30, 1, 123000),
datetime(2015, 1, 10, 7, 45, 2, 345000)))
class TimeRangeFieldTests(TestCase):
def test_field(self):
f = DateRangeField()
self.assertEqual(len(f.fields), 2)
def test_clean(self):
w = RangeWidget()
f = TimeRangeField(widget=w)
self.assertEqual(
f.clean(['10:15', '12:30']),
slice(time(10, 15, 0), time(12, 30, 0)))
class LookupChoiceFieldTests(TestCase):
def test_field(self):
inner = forms.DecimalField()
f = LookupChoiceField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertEqual(len(f.fields), 2)
def test_clean(self):
inner = forms.DecimalField()
f = LookupChoiceField(inner, [('gt', 'gt'), ('lt', 'lt')], required=False)
self.assertEqual(
f.clean(['12.34', 'lt']),
Lookup(to_d(12.34), 'lt'))
self.assertEqual(
f.clean([]),
None)
with self.assertRaisesMessage(forms.ValidationError, 'Select a lookup.'):
f.clean(['12.34', ''])
def test_render_used_html5(self):
inner = forms.DecimalField()
f = LookupChoiceField(inner, [('gt', 'gt'), ('lt', 'lt')], empty_label=None)
self.assertHTMLEqual(f.widget.render('price', ''), """
<input type="number" step="any" name="price" />
<select name="price_lookup">
<option value="gt">gt</option>
<option value="lt">lt</option>
</select>""")
self.assertHTMLEqual(f.widget.render('price', ['abc', 'lt']), """
<input type="number" step="any" name="price" value="abc" />
<select name="price_lookup">
<option value="gt">gt</option>
<option selected="selected" value="lt">lt</option>
</select>""")
class IsoDateTimeFieldTests(TestCase):
reference_str = "2015-07-19T13:34:51.759"
reference_dt = datetime(2015, 7, 19, 13, 34, 51, 759000)
field = IsoDateTimeField()
def parse_input(self, value):
return self.field.strptime(value, IsoDateTimeField.ISO_8601)
def test_datetime_string_is_parsed(self):
d = self.parse_input(self.reference_str)
self.assertTrue(isinstance(d, datetime))
def test_datetime_string_with_timezone_is_parsed(self):
d = self.parse_input(self.reference_str + "+01:00")
self.assertTrue(isinstance(d, datetime))
def test_datetime_zulu(self):
d = self.parse_input(self.reference_str + "Z")
self.assertTrue(isinstance(d, datetime))
@override_settings(TIME_ZONE='UTC')
def test_datetime_timezone_awareness(self):
utc, tokyo = pytz.timezone('UTC'), pytz.timezone('Asia/Tokyo')
# by default, use the server timezone
reference = utc.localize(self.reference_dt)
parsed = self.parse_input(self.reference_str)
self.assertIsInstance(parsed.tzinfo, tzinfo)
self.assertEqual(parsed, reference)
# if set, use the active timezone
reference = tokyo.localize(self.reference_dt)
with timezone.override(tokyo):
parsed = self.parse_input(self.reference_str)
self.assertIsInstance(parsed.tzinfo, tzinfo)
self.assertEqual(parsed.tzinfo.zone, tokyo.zone)
self.assertEqual(parsed, reference)
# if provided, utc offset should have precedence
reference = utc.localize(self.reference_dt - timedelta(hours=1))
parsed = self.parse_input(self.reference_str + "+01:00")
self.assertIsInstance(parsed.tzinfo, tzinfo)
self.assertEqual(parsed, reference)
@override_settings(USE_TZ=False)
def test_datetime_timezone_naivety(self):
reference = self.reference_dt.replace()
parsed = self.parse_input(self.reference_str + "+01:00")
self.assertIsNone(parsed.tzinfo)
self.assertEqual(parsed, reference - timedelta(hours=1))
parsed = self.parse_input(self.reference_str)
self.assertIsNone(parsed.tzinfo)
self.assertEqual(parsed, reference)
def test_datetime_non_iso_format(self):
f = IsoDateTimeField()
parsed = f.strptime('19-07-2015T51:34:13.759', '%d-%m-%YT%S:%M:%H.%f')
self.assertTrue(isinstance(parsed, datetime))
self.assertEqual(parsed, self.reference_dt)
def test_datetime_wrong_format(self):
with self.assertRaises(ValueError):
self.parse_input('19-07-2015T51:34:13.759')
class BaseCSVFieldTests(TestCase):
def setUp(self):
class DecimalCSVField(BaseCSVField, forms.DecimalField):
pass
self.field = DecimalCSVField()
def test_clean(self):
self.assertEqual(self.field.clean(None), None)
self.assertEqual(self.field.clean(''), [])
self.assertEqual(self.field.clean(['1']), [1])
self.assertEqual(self.field.clean(['1', '2']), [1, 2])
self.assertEqual(self.field.clean(['1', '2', '3']), [1, 2, 3])
def test_validation_error(self):
with self.assertRaises(forms.ValidationError):
self.field.clean([''])
with self.assertRaises(forms.ValidationError):
self.field.clean(['a', 'b', 'c'])
def test_derived_widget(self):
with self.assertRaises(AssertionError) as excinfo:
BaseCSVField(widget=RangeWidget())
msg = str(excinfo.exception)
self.assertIn("'BaseCSVField.widget' must be a widget class", msg)
self.assertIn("RangeWidget", msg)
widget = CSVWidget(attrs={'class': 'class'})
field = BaseCSVField(widget=widget)
self.assertIsInstance(field.widget, CSVWidget)
self.assertEqual(field.widget.attrs, {'class': 'class'})
field = BaseCSVField(widget=CSVWidget)
self.assertIsInstance(field.widget, CSVWidget)
field = BaseCSVField(widget=forms.Select)
self.assertIsInstance(field.widget, forms.Select)
self.assertIsInstance(field.widget, BaseCSVWidget)
class BaseRangeFieldTests(TestCase):
def setUp(self):
class DecimalRangeField(BaseRangeField, forms.DecimalField):
pass
self.field = DecimalRangeField()
def test_clean(self):
self.assertEqual(self.field.clean(None), None)
self.assertEqual(self.field.clean(''), [])
self.assertEqual(self.field.clean([]), [])
self.assertEqual(self.field.clean(['1', '2']), [1, 2])
def test_validation_error(self):
with self.assertRaises(forms.ValidationError):
self.field.clean([''])
with self.assertRaises(forms.ValidationError):
self.field.clean(['1'])
with self.assertRaises(forms.ValidationError):
self.field.clean(['1', '2', '3'])