Skip to content

Commit

Permalink
Update CSV range field tests, add filtering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby authored and carltongibson committed Jul 13, 2018
1 parent da2a491 commit b290d2c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
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
60 changes: 60 additions & 0 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1817,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

0 comments on commit b290d2c

Please sign in to comment.