Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty value check for CSV range #854

Merged
merged 3 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion django_filters/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ class BaseRangeField(BaseCSVField):
def clean(self, value):
value = super().clean(value)

if value is not None and len(value) != 2:
assert value is None or isinstance(value, list)

if value and len(value) != 2:
raise forms.ValidationError(
self.error_messages['invalid_values'],
code='invalid_values')
Expand Down
5 changes: 2 additions & 3 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,11 @@ class DecimalRangeField(BaseRangeField, forms.DecimalField):

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([''])

Expand Down
72 changes: 64 additions & 8 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,13 +1592,11 @@ class Meta:
self.assertQuerysetEqual(f.qs, [2], lambda o: o.pk)


# use naive datetimes, as pytz is required to perform
# date lookups when timezones are involved.
@override_settings(USE_TZ=False)
@override_settings(TIME_ZONE='UTC')
class TransformedQueryExpressionFilterTests(TestCase):

def test_filtering(self):
now_dt = datetime.datetime.now()
now_dt = now()
after_5pm = now_dt.replace(hour=18)
before_5pm = now_dt.replace(hour=16)

Expand Down Expand Up @@ -1675,9 +1673,7 @@ def test_lookup_omitted(self):
})


# use naive datetimes, as pytz is required to perform
# date lookups when timezones are involved.
@override_settings(USE_TZ=False)
@override_settings(TIME_ZONE='UTC')
class CSVFilterTests(TestCase):

def setUp(self):
Expand All @@ -1686,7 +1682,7 @@ def setUp(self):
User.objects.create(username='aaron', status=2)
User.objects.create(username='carl', status=0)

now_dt = datetime.datetime.now()
now_dt = now()
after_5pm = now_dt.replace(hour=18)
before_5pm = now_dt.replace(hour=16)

Expand Down Expand Up @@ -1821,6 +1817,66 @@ def test_related_filtering(self):
self.assertEqual(f.qs.count(), 2)


@override_settings(TIME_ZONE='UTC')
class CSVRangeFilterTests(TestCase):

class ArticleFilter(FilterSet):
class Meta:
model = Article
fields = {
'published': ['range'],
}

@classmethod
def setUpTestData(cls):
u1 = User.objects.create(username='alex', status=1)
u2 = User.objects.create(username='jacob', status=2)
User.objects.create(username='aaron', status=2)
User.objects.create(username='carl', status=0)

now_dt = now()
after_5pm = now_dt.replace(hour=18)
around_5pm = now_dt.replace(hour=17)
before_5pm = now_dt.replace(hour=16)

Article.objects.create(author=u1, published=after_5pm)
Article.objects.create(author=u2, published=around_5pm)
Article.objects.create(author=u1, published=around_5pm)
Article.objects.create(author=u2, published=before_5pm)

cls.after_5pm = after_5pm.strftime('%Y-%m-%d %H:%M:%S.%f')
cls.around_5pm = around_5pm.strftime('%Y-%m-%d %H:%M:%S.%f')
cls.before_5pm = before_5pm.strftime('%Y-%m-%d %H:%M:%S.%f')

def test_filtering(self):
F = self.ArticleFilter

f = F()
self.assertEqual(f.qs.count(), 4)

# empty value is a noop
f = F({'published__range': ''})
self.assertTrue(f.is_valid())
self.assertEqual(f.qs.count(), 4)

# empty values are interpreted as None types
f = F({'published__range': ','})
self.assertEqual(f.qs.count(), 0)

f = F({'published__range': '%s' % (self.before_5pm, )})
self.assertFalse(f.is_valid())

f = F({'published__range': '%s,%s' % (self.before_5pm, self.around_5pm, )})
self.assertEqual(f.qs.count(), 3)

f = F({'published__range': '%s,,%s' % (self.before_5pm, self.after_5pm, )})
self.assertFalse(f.is_valid())

# empty value is interpreted as None type
f = F({'published__range': '%s,' % (self.before_5pm, )})
self.assertEqual(f.qs.count(), 0)


class OrderingFilterTests(TestCase):

def setUp(self):
Expand Down