forked from carltongibson/django-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fields.py
239 lines (178 loc) · 7.71 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
from __future__ import absolute_import
from __future__ import unicode_literals
from datetime import datetime, time, timedelta, tzinfo
import decimal
from django import forms
from django.test import TestCase, override_settings
from django.utils.timezone import make_aware, get_default_timezone
from django_filters.widgets import BaseCSVWidget, CSVWidget, RangeWidget
from django_filters.fields import (
Lookup, LookupTypeField, BaseCSVField, BaseRangeField, RangeField,
DateRangeField, DateTimeRangeField, TimeRangeField, IsoDateTimeField
)
def to_d(float_value):
return decimal.Decimal('%.2f' % float_value)
class LookupBoolTests(TestCase):
def test_lookup_true(self):
self.assertTrue(Lookup(True, 'exact'))
self.assertTrue(Lookup(1, 'exact'))
self.assertTrue(Lookup('1', 'exact'))
self.assertTrue(Lookup(datetime.now(), 'exact'))
def test_lookup_false(self):
self.assertFalse(Lookup(False, 'exact'))
self.assertFalse(Lookup(0, 'exact'))
self.assertFalse(Lookup('', 'exact'))
self.assertFalse(Lookup(None, 'exact'))
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)
self.assertEqual(
f.clean(['12.34', '55']),
slice(to_d(12.34), to_d(55)))
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)
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)))
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 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 LookupTypeFieldTests(TestCase):
def test_field(self):
inner = forms.DecimalField()
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertEqual(len(f.fields), 2)
def test_clean(self):
inner = forms.DecimalField()
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertEqual(
f.clean(['12.34', 'lt']),
Lookup(to_d(12.34), 'lt'))
def test_render_used_html5(self):
inner = forms.DecimalField()
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertHTMLEqual(f.widget.render('price', ''), """
<input type="number" step="any" name="price_0" />
<select name="price_1">
<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_0" value="abc" />
<select name="price_1">
<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)
def test_datetime_string_is_parsed(self):
f = IsoDateTimeField()
d = f.strptime(self.reference_str + "", IsoDateTimeField.ISO_8601)
self.assertTrue(isinstance(d, datetime))
def test_datetime_string_with_timezone_is_parsed(self):
f = IsoDateTimeField()
d = f.strptime(self.reference_str + "+01:00", IsoDateTimeField.ISO_8601)
self.assertTrue(isinstance(d, datetime))
def test_datetime_zulu(self):
f = IsoDateTimeField()
d = f.strptime(self.reference_str + "Z", IsoDateTimeField.ISO_8601)
self.assertTrue(isinstance(d, datetime))
def test_datetime_timezone_awareness(self):
# parsed datetimes should obey USE_TZ
f = IsoDateTimeField()
r = make_aware(self.reference_dt, get_default_timezone())
d = f.strptime(self.reference_str + "+01:00", IsoDateTimeField.ISO_8601)
self.assertTrue(isinstance(d.tzinfo, tzinfo))
self.assertEqual(d, r + r.utcoffset() - d.utcoffset())
d = f.strptime(self.reference_str + "", IsoDateTimeField.ISO_8601)
self.assertTrue(isinstance(d.tzinfo, tzinfo))
self.assertEqual(d, r)
@override_settings(USE_TZ=False)
def test_datetime_timezone_naivety(self):
# parsed datetimes should obey USE_TZ
f = IsoDateTimeField()
r = self.reference_dt.replace()
d = f.strptime(self.reference_str + "+01:00", IsoDateTimeField.ISO_8601)
self.assertTrue(d.tzinfo is None)
self.assertEqual(d, r - timedelta(hours=1))
d = f.strptime(self.reference_str + "", IsoDateTimeField.ISO_8601)
self.assertTrue(d.tzinfo is None)
self.assertEqual(d, r)
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()
field = BaseCSVField(widget=widget)
self.assertIs(field.widget, widget)
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(['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([''])
with self.assertRaises(forms.ValidationError):
self.field.clean(['1'])
with self.assertRaises(forms.ValidationError):
self.field.clean(['1', '2', '3'])