Skip to content

Commit

Permalink
Test QueryDict against CSV filters (#937)
Browse files Browse the repository at this point in the history
* Refactor CSV filtering tests

* Add QueryDict to CSV filtering tests
  • Loading branch information
Ryan P Kilby authored and carltongibson committed Jul 13, 2018
1 parent 4d295a0 commit f81af87
Showing 1 changed file with 66 additions and 84 deletions.
150 changes: 66 additions & 84 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import datetime
import mock
import unittest
from operator import attrgetter

from django import forms
from django.http import QueryDict
from django.test import TestCase, override_settings
from django.utils import timezone
from django.utils.timezone import make_aware, now
Expand Down Expand Up @@ -1694,106 +1696,86 @@ class Meta:

def test_numeric_filtering(self):
F = self.user_filter
qs = User.objects.order_by('pk')

cases = [
(None, [1, 2, 3, 4]),
(QueryDict('status__in=1&status__in=2'), [2, 3]),
({'status__in': ''}, [1, 2, 3, 4]),
({'status__in': ','}, []),
({'status__in': '0'}, [4]),
({'status__in': '0,2'}, [2, 3, 4]),
({'status__in': '0,,1'}, [1, 4]),
({'status__in': '2'}, [2, 3]),
]

qs = User.objects.all()
f = F(queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'status__in': ''}, queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'status__in': ','}, queryset=qs)
self.assertEqual(f.qs.count(), 0)

f = F({'status__in': '0'}, queryset=qs)
self.assertEqual(f.qs.count(), 1)

f = F({'status__in': '0,2'}, queryset=qs)
self.assertEqual(f.qs.count(), 3)

f = F({'status__in': '0,,1'}, queryset=qs)
self.assertEqual(f.qs.count(), 2)

f = F({'status__in': '2'}, queryset=qs)
self.assertEqual(f.qs.count(), 2)
for params, expected in cases:
with self.subTest(params=params, expected=expected):
self.assertQuerysetEqual(F(params, queryset=qs).qs,
expected, attrgetter('pk'))

def test_string_filtering(self):
F = self.user_filter
qs = User.objects.order_by('pk')

cases = [
(None, [1, 2, 3, 4]),
(QueryDict('username__in=alex&username__in=aaron'), [3]),
({'username__in': ''}, [1, 2, 3, 4]),
({'username__in': ','}, []),
({'username__in': 'alex'}, [1]),
({'username__in': 'alex,aaron'}, [1, 3]),
({'username__in': 'alex,,aaron'}, [1, 3]),
({'username__in': 'alex,'}, [1]),
]

qs = User.objects.all()
f = F(queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'username__in': ''}, queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'username__in': ','}, queryset=qs)
self.assertEqual(f.qs.count(), 0)

f = F({'username__in': 'alex'}, queryset=qs)
self.assertEqual(f.qs.count(), 1)

f = F({'username__in': 'alex,aaron'}, queryset=qs)
self.assertEqual(f.qs.count(), 2)

f = F({'username__in': 'alex,,aaron'}, queryset=qs)
self.assertEqual(f.qs.count(), 2)

f = F({'username__in': 'alex,'}, queryset=qs)
self.assertEqual(f.qs.count(), 1)
for params, expected in cases:
with self.subTest(params=params, expected=expected):
self.assertQuerysetEqual(F(params, queryset=qs).qs,
expected, attrgetter('pk'))

def test_datetime_filtering(self):
F = self.article_filter
qs = Article.objects.order_by('pk')

after = self.after_5pm
before = self.before_5pm

qs = Article.objects.all()
f = F(queryset=qs)
self.assertEqual(len(f.qs), 4)
self.assertEqual(f.qs.count(), 4)

f = F({'published__in': ''}, queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'published__in': ','}, queryset=qs)
self.assertEqual(f.qs.count(), 0)

f = F({'published__in': '%s' % (after, )}, queryset=qs)
self.assertEqual(f.qs.count(), 2)

f = F({'published__in': '%s,%s' % (after, before, )}, queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'published__in': '%s,,%s' % (after, before, )}, queryset=qs)
self.assertEqual(f.qs.count(), 4)
cases = [
(None, [1, 2, 3, 4]),
(QueryDict('published__in=%s&published__in=%s' % (after, before)), [3, 4]),
({'published__in': ''}, [1, 2, 3, 4]),
({'published__in': ','}, []),
({'published__in': '%s' % (after, )}, [1, 2]),
({'published__in': '%s,%s' % (after, before, )}, [1, 2, 3, 4]),
({'published__in': '%s,,%s' % (after, before, )}, [1, 2, 3, 4]),
({'published__in': '%s,' % (after, )}, [1, 2]),
]

f = F({'published__in': '%s,' % (after, )}, queryset=qs)
self.assertEqual(f.qs.count(), 2)
for params, expected in cases:
with self.subTest(params=params, expected=expected):
self.assertQuerysetEqual(F(params, queryset=qs).qs,
expected, attrgetter('pk'))

def test_related_filtering(self):
F = self.article_filter
qs = Article.objects.order_by('pk')

cases = [
(None, [1, 2, 3, 4]),
(QueryDict('author__in=1&author__in=2'), [2, 4]),
({'author__in': ''}, [1, 2, 3, 4]),
({'author__in': ','}, []),
({'author__in': '1'}, [1, 3]),
({'author__in': '1,2'}, [1, 2, 3, 4]),
({'author__in': '1,,2'}, [1, 2, 3, 4]),
({'author__in': '1,'}, [1, 3]),
]

qs = Article.objects.all()
f = F(queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'author__in': ''}, queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'author__in': ','}, queryset=qs)
self.assertEqual(f.qs.count(), 0)

f = F({'author__in': '1'}, queryset=qs)
self.assertEqual(f.qs.count(), 2)

f = F({'author__in': '1,2'}, queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'author__in': '1,,2'}, queryset=qs)
self.assertEqual(f.qs.count(), 4)

f = F({'author__in': '1,'}, queryset=qs)
self.assertEqual(f.qs.count(), 2)
for params, expected in cases:
with self.subTest(params=params, expected=expected):
self.assertQuerysetEqual(F(params, queryset=qs).qs,
expected, attrgetter('pk'))


@override_settings(TIME_ZONE='UTC')
Expand Down

0 comments on commit f81af87

Please sign in to comment.