forked from carltongibson/django-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_filters.py
474 lines (382 loc) · 16.1 KB
/
test_filters.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
from __future__ import absolute_import
from __future__ import unicode_literals
import mock
import sys
if sys.version_info >= (2, 7):
import unittest
else: # pragma: nocover
from django.utils import unittest # noqa
from django import forms
from django.test import TestCase
from django_filters.fields import Lookup
from django_filters.fields import RangeField
from django_filters.fields import LookupTypeField
from django_filters.filters import Filter
from django_filters.filters import CharFilter
from django_filters.filters import BooleanFilter
from django_filters.filters import ChoiceFilter
from django_filters.filters import MultipleChoiceFilter
from django_filters.filters import DateFilter
from django_filters.filters import DateTimeFilter
from django_filters.filters import TimeFilter
from django_filters.filters import ModelChoiceFilter
from django_filters.filters import ModelMultipleChoiceFilter
from django_filters.filters import NumberFilter
from django_filters.filters import RangeFilter
from django_filters.filters import DateRangeFilter
from django_filters.filters import AllValuesFilter
from django_filters.filters import LOOKUP_TYPES
class FilterTests(TestCase):
def test_creation(self):
f = Filter()
self.assertEqual(f.lookup_type, 'exact')
self.assertEqual(f.exclude, False)
def test_creation_order(self):
f = Filter()
f2 = Filter()
self.assertTrue(f2.creation_counter > f.creation_counter)
def test_default_field(self):
f = Filter()
field = f.field
self.assertIsInstance(field, forms.Field)
self.assertEqual(field.help_text, '')
def test_field_with_exclusion(self):
f = Filter(exclude=True)
field = f.field
self.assertIsInstance(field, forms.Field)
self.assertEqual(field.help_text, 'This is an exclusion filter')
def test_field_with_single_lookup_type(self):
f = Filter(lookup_type='iexact')
field = f.field
self.assertIsInstance(field, forms.Field)
def test_field_with_none_lookup_type(self):
f = Filter(lookup_type=None)
field = f.field
self.assertIsInstance(field, LookupTypeField)
choice_field = field.fields[1]
self.assertEqual(len(choice_field.choices), len(LOOKUP_TYPES))
def test_field_with_lookup_type_and_exlusion(self):
f = Filter(lookup_type=None, exclude=True)
field = f.field
self.assertIsInstance(field, LookupTypeField)
self.assertEqual(field.help_text, 'This is an exclusion filter')
def test_field_with_list_lookup_type(self):
f = Filter(lookup_type=('istartswith', 'iendswith'))
field = f.field
self.assertIsInstance(field, LookupTypeField)
choice_field = field.fields[1]
self.assertEqual(len(choice_field.choices), 2)
def test_field_params(self):
with mock.patch.object(Filter, 'field_class',
spec=['__call__']) as mocked:
f = Filter(name='somefield', label='somelabel',
widget='somewidget')
f.field
mocked.assert_called_once_with(required=False,
label='somelabel', widget='somewidget', help_text=mock.ANY)
def test_field_extra_params(self):
with mock.patch.object(Filter, 'field_class',
spec=['__call__']) as mocked:
f = Filter(someattr='someattr')
f.field
mocked.assert_called_once_with(required=mock.ANY,
label=mock.ANY, widget=mock.ANY, help_text=mock.ANY,
someattr='someattr')
def test_field_with_required_filter(self):
with mock.patch.object(Filter, 'field_class',
spec=['__call__']) as mocked:
f = Filter(required=True)
f.field
mocked.assert_called_once_with(required=True,
label=mock.ANY, widget=mock.ANY, help_text=mock.ANY)
def test_filtering(self):
qs = mock.Mock(spec=['filter'])
f = Filter()
result = f.filter(qs, 'value')
qs.filter.assert_called_once_with(None__exact='value')
self.assertNotEqual(qs, result)
def test_filtering_exclude(self):
qs = mock.Mock(spec=['filter', 'exclude'])
f = Filter(exclude=True)
result = f.filter(qs, 'value')
qs.exclude.assert_called_once_with(None__exact='value')
self.assertNotEqual(qs, result)
def test_filtering_uses_name(self):
qs = mock.Mock(spec=['filter'])
f = Filter(name='somefield')
f.filter(qs, 'value')
result = qs.filter.assert_called_once_with(somefield__exact='value')
self.assertNotEqual(qs, result)
def test_filtering_skipped_with_blank_value(self):
qs = mock.Mock()
f = Filter()
result = f.filter(qs, '')
self.assertListEqual(qs.method_calls, [])
self.assertEqual(qs, result)
def test_filtering_skipped_with_none_value(self):
qs = mock.Mock()
f = Filter()
result = f.filter(qs, None)
self.assertListEqual(qs.method_calls, [])
self.assertEqual(qs, result)
def test_filtering_with_list_value(self):
qs = mock.Mock(spec=['filter'])
f = Filter(name='somefield', lookup_type=['some_lookup_type'])
result = f.filter(qs, Lookup('value', 'some_lookup_type'))
qs.filter.assert_called_once_with(somefield__some_lookup_type='value')
self.assertNotEqual(qs, result)
def test_filtering_skipped_with_list_value_with_blank(self):
qs = mock.Mock()
f = Filter(name='somefield', lookup_type=['some_lookup_type'])
result = f.filter(qs, Lookup('', 'some_lookup_type'))
self.assertListEqual(qs.method_calls, [])
self.assertEqual(qs, result)
def test_filtering_skipped_with_list_value_with_blank_lookup(self):
return # Now field is required to provide valid lookup_type if it provides any
qs = mock.Mock(spec=['filter'])
f = Filter(name='somefield', lookup_type=None)
result = f.filter(qs, Lookup('value', ''))
qs.filter.assert_called_once_with(somefield__exact='value')
self.assertNotEqual(qs, result)
def test_filter_using_action(self):
qs = mock.NonCallableMock(spec=[])
action = mock.Mock(spec=['filter'])
f = Filter(action=action)
result = f.filter(qs, 'value')
action.assert_called_once_with(qs, 'value')
self.assertNotEqual(qs, result)
def test_filtering_uses_distinct(self):
qs = mock.Mock(spec=['filter', 'distinct'])
f = Filter(name='somefield', distinct=True)
f.filter(qs, 'value')
result = qs.distinct.assert_called_once()
self.assertNotEqual(qs, result)
class CharFilterTests(TestCase):
def test_default_field(self):
f = CharFilter()
field = f.field
self.assertIsInstance(field, forms.CharField)
class BooleanFilterTests(TestCase):
def test_default_field(self):
f = BooleanFilter()
field = f.field
self.assertIsInstance(field, forms.NullBooleanField)
def test_filtering(self):
qs = mock.Mock(spec=['filter'])
f = BooleanFilter(name='somefield')
result = f.filter(qs, True)
qs.filter.assert_called_once_with(somefield=True)
self.assertNotEqual(qs, result)
@unittest.expectedFailure
def test_filtering_skipped_with_blank_value(self):
qs = mock.Mock()
f = BooleanFilter(name='somefield')
result = f.filter(qs, '')
self.assertListEqual(qs.method_calls, [])
self.assertEqual(qs, result)
def test_filtering_skipped_with_none_value(self):
qs = mock.Mock()
f = BooleanFilter(name='somefield')
result = f.filter(qs, None)
self.assertListEqual(qs.method_calls, [])
self.assertEqual(qs, result)
class ChoiceFilterTests(TestCase):
def test_default_field(self):
f = ChoiceFilter()
field = f.field
self.assertIsInstance(field, forms.ChoiceField)
class MultipleChoiceFilterTests(TestCase):
def test_default_field(self):
f = MultipleChoiceFilter()
field = f.field
self.assertIsInstance(field, forms.MultipleChoiceField)
def test_filtering_requires_name(self):
qs = mock.Mock(spec=['filter'])
f = MultipleChoiceFilter()
with self.assertRaises(TypeError):
f.filter(qs, ['value'])
def test_filtering(self):
qs = mock.Mock(spec=['filter'])
f = MultipleChoiceFilter(name='somefield')
with mock.patch('django_filters.filters.Q') as mockQclass:
mockQ1, mockQ2 = mock.MagicMock(), mock.MagicMock()
mockQclass.side_effect = [mockQ1, mockQ2]
f.filter(qs, ['value'])
self.assertEqual(mockQclass.call_args_list,
[mock.call(), mock.call(somefield='value')])
mockQ1.__ior__.assert_called_once_with(mockQ2)
qs.filter.assert_called_once_with(mockQ1.__ior__.return_value)
qs.filter.return_value.distinct.assert_called_once_with()
def test_filtering_on_required_skipped_when_len_of_value_is_len_of_field_choices(self):
qs = mock.Mock(spec=[])
f = MultipleChoiceFilter(name='somefield', required=True)
f.always_filter = False
result = f.filter(qs, [])
self.assertEqual(len(f.field.choices), 0)
self.assertEqual(qs, result)
f.field.choices = ['some', 'values', 'here']
result = f.filter(qs, ['some', 'values', 'here'])
self.assertEqual(qs, result)
result = f.filter(qs, ['other', 'values', 'there'])
self.assertEqual(qs, result)
@unittest.expectedFailure
def test_filtering_skipped_with_empty_list_value_and_some_choices(self):
qs = mock.Mock(spec=[])
f = MultipleChoiceFilter(name='somefield')
f.field.choices = ['some', 'values', 'here']
result = f.filter(qs, [])
self.assertEqual(qs, result)
class DateFilterTests(TestCase):
def test_default_field(self):
f = DateFilter()
field = f.field
self.assertIsInstance(field, forms.DateField)
class DateTimeFilterTests(TestCase):
def test_default_field(self):
f = DateTimeFilter()
field = f.field
self.assertIsInstance(field, forms.DateTimeField)
class TimeFilterTests(TestCase):
def test_default_field(self):
f = TimeFilter()
field = f.field
self.assertIsInstance(field, forms.TimeField)
class ModelChoiceFilterTests(TestCase):
def test_default_field_without_queryset(self):
f = ModelChoiceFilter()
with self.assertRaises(TypeError):
f.field
def test_default_field_with_queryset(self):
qs = mock.NonCallableMock(spec=[])
f = ModelChoiceFilter(queryset=qs)
field = f.field
self.assertIsInstance(field, forms.ModelChoiceField)
self.assertEqual(field.queryset, qs)
class ModelMultipleChoiceFilterTests(TestCase):
def test_default_field_without_queryset(self):
f = ModelMultipleChoiceFilter()
with self.assertRaises(TypeError):
f.field
def test_default_field_with_queryset(self):
qs = mock.NonCallableMock(spec=[])
f = ModelMultipleChoiceFilter(queryset=qs)
field = f.field
self.assertIsInstance(field, forms.ModelMultipleChoiceField)
self.assertEqual(field.queryset, qs)
class NumberFilterTests(TestCase):
def test_default_field(self):
f = NumberFilter()
field = f.field
self.assertIsInstance(field, forms.DecimalField)
def test_filtering(self):
qs = mock.Mock(spec=['filter'])
f = NumberFilter()
f.filter(qs, 1)
qs.filter.assert_called_once_with(None__exact=1)
# Also test 0 as it once had a bug
qs.reset_mock()
f.filter(qs, 0)
qs.filter.assert_called_once_with(None__exact=0)
class RangeFilterTests(TestCase):
def test_default_field(self):
f = RangeFilter()
field = f.field
self.assertIsInstance(field, RangeField)
def test_filtering(self):
qs = mock.Mock(spec=['filter'])
value = mock.Mock(start=20, stop=30)
f = RangeFilter()
f.filter(qs, value)
qs.filter.assert_called_once_with(None__range=(20, 30))
def test_filtering_skipped_with_none_value(self):
qs = mock.Mock(spec=['filter'])
f = RangeFilter()
result = f.filter(qs, None)
self.assertEqual(qs, result)
def test_filtering_ignores_lookup_type(self):
qs = mock.Mock()
value = mock.Mock(start=20, stop=30)
f = RangeFilter(lookup_type='gte')
f.filter(qs, value)
qs.filter.assert_called_once_with(None__range=(20, 30))
class DateRangeFilterTests(TestCase):
def test_creating(self):
f = DateRangeFilter()
self.assertIn('choices', f.extra)
self.assertEqual(len(DateRangeFilter.options), len(f.extra['choices']))
def test_default_field(self):
f = DateRangeFilter()
field = f.field
self.assertIsInstance(field, forms.ChoiceField)
def test_filtering(self):
qs = mock.Mock(spec=['all'])
f = DateRangeFilter()
f.filter(qs, '')
qs.all.assert_called_once_with()
# the correct behavior fails right now
@unittest.expectedFailure
def test_filtering_skipped_with_blank_value(self):
qs = mock.Mock(spec=[])
f = DateRangeFilter()
result = f.filter(qs, '')
self.assertEqual(qs, result)
@unittest.expectedFailure
def test_filtering_skipped_with_out_of_range_value(self):
qs = mock.Mock(spec=[])
f = DateRangeFilter()
result = f.filter(qs, 999)
self.assertEqual(qs, result)
def test_filtering_for_this_year(self):
qs = mock.Mock(spec=['filter'])
with mock.patch('django_filters.filters.now') as mock_now:
now_dt = mock_now.return_value
f = DateRangeFilter()
f.filter(qs, '4')
qs.filter.assert_called_once_with(
None__year=now_dt.year)
def test_filtering_for_this_month(self):
qs = mock.Mock(spec=['filter'])
with mock.patch('django_filters.filters.now') as mock_now:
now_dt = mock_now.return_value
f = DateRangeFilter()
f.filter(qs, '3')
qs.filter.assert_called_once_with(
None__year=now_dt.year, None__month=now_dt.month)
def test_filtering_for_7_days(self):
qs = mock.Mock(spec=['filter'])
with mock.patch('django_filters.filters.now'):
with mock.patch('django_filters.filters.timedelta') as mock_td:
with mock.patch(
'django_filters.filters._truncate') as mock_truncate:
mock_dt1, mock_dt2 = mock.MagicMock(), mock.MagicMock()
mock_truncate.side_effect = [mock_dt1, mock_dt2]
f = DateRangeFilter()
f.filter(qs, '2')
self.assertEqual(mock_td.call_args_list,
[mock.call(days=7), mock.call(days=1)])
qs.filter.assert_called_once_with(
None__lt=mock_dt2, None__gte=mock_dt1)
def test_filtering_for_today(self):
qs = mock.Mock(spec=['filter'])
with mock.patch('django_filters.filters.now') as mock_now:
now_dt = mock_now.return_value
f = DateRangeFilter()
f.filter(qs, '1')
qs.filter.assert_called_once_with(
None__year=now_dt.year,
None__month=now_dt.month,
None__day=now_dt.day)
class AllValuesFilterTests(TestCase):
def test_default_field_without_assigning_model(self):
f = AllValuesFilter()
with self.assertRaises(AttributeError):
f.field
def test_default_field_with_assigning_model(self):
mocked = mock.Mock()
chained_call = '.'.join(['_default_manager', 'distinct.return_value',
'order_by.return_value', 'values_list.return_value'])
mocked.configure_mock(**{chained_call: iter([])})
f = AllValuesFilter()
f.model = mocked
field = f.field
self.assertIsInstance(field, forms.ChoiceField)