Skip to content

Commit

Permalink
Increase tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Jan 21, 2019
1 parent 4015f45 commit 8ac70cd
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions daterangefilter/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def setUp(self):

self.user = User.objects.create_user(username='test', password='top_secret')


def get_changelist(self, request, model, modeladmin):
if getattr(modeladmin, 'get_changelist_instance', None):
return modeladmin.get_changelist_instance(request)
Expand All @@ -68,6 +69,19 @@ def get_changelist(self, request, model, modeladmin):
modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin,
)


def test_get_template(self):
self.request_factory = RequestFactory()
modeladmin = MyModelAdmin(MyModel, site)

request = self.request_factory.get('/')
request.user = self.user

changelist = self.get_changelist(request, MyModel, modeladmin)
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(filterspec.template, 'daterangefilter/daterangefilter.html')


def test_datefilter(self):
self.request_factory = RequestFactory()
modeladmin = MyModelAdmin(MyModel, site)
Expand All @@ -83,6 +97,7 @@ def test_datefilter(self):
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_text(filterspec.title), 'created at')


def test_datefilter_filtered(self):
self.request_factory = RequestFactory()
modeladmin = MyModelAdmin(MyModel, site)
Expand All @@ -102,3 +117,24 @@ def test_datefilter_filtered(self):
choice = select_by(filterspec.choices(changelist))
self.assertEqual(choice['query_string'], '?')
self.assertEqual(choice['field_name'], 'created_at')


def test_datefilter_empty_values(self):
self.request_factory = RequestFactory()
modeladmin = MyModelAdmin(MyModel, site)

request = self.request_factory.get('/', {'created_at__gte': '',
'created_at__lte': ''})
request.user = self.user

changelist = self.get_changelist(request, MyModel, modeladmin)

queryset = changelist.get_queryset(request)

self.assertEqual(list(queryset), [self.object_two, self.object_one])
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_text(filterspec.title), 'created at')

choice = select_by(filterspec.choices(changelist))
self.assertEqual(choice['query_string'], '?')
self.assertEqual(choice['field_name'], 'created_at')

0 comments on commit 8ac70cd

Please sign in to comment.