def test_empty_list(self): view = ListView.as_view(model=Example) response = self.get(view) self.assertEqual(response.status_code, 200) self.assertEqual(response.template_name, ["vanilla/example_list.html"]) self.assertContext( response, { "object_list": [], "example_list": [], "view": InstanceOf(View), "page_obj": None, "paginator": None, "is_paginated": False, }, )
def test_paginated_list_last_page_specified(self): create_instance(quantity=30) view = ListView.as_view(model=Example, paginate_by=10) response = self.get(view, page="last") self.assertEqual(response.status_code, 200) self.assertEqual(response.template_name, ["vanilla/example_list.html"]) self.assertContext( response, { "object_list": Example.objects.all()[20:], "example_list": Example.objects.all()[20:], "view": InstanceOf(View), "page_obj": InstanceOf(Page), "paginator": InstanceOf(Paginator), "is_paginated": True, }, )
def test_queryset_override(self): create_instance(text="abc", quantity=3) create_instance(text="def", quantity=3) view = ListView.as_view( model=Example, queryset=Example.objects.filter(text="abc"), ) response = self.get(view) self.assertEqual(response.status_code, 200) self.assertEqual(response.template_name, ["vanilla/example_list.html"]) self.assertContext( response, { "object_list": Example.objects.filter(text="abc"), "example_list": Example.objects.filter(text="abc"), "view": InstanceOf(View), "page_obj": None, "paginator": None, "is_paginated": False, }, )
from django.conf.urls import url from vanilla import ListView from .models import Test from .views import PassTestView, UserTestResultListView urlpatterns = [ url(r'^$', ListView.as_view(model=Test), name='test-list'), url(r'^test_(?P<pk>\d+)/$', PassTestView.as_view(), name='test-pass'), url(r'^results/$', UserTestResultListView.as_view(), name='usertestresult-list') ]
def test_paginated_list_non_integer_page_specified(self): create_instance(quantity=30) view = ListView.as_view(model=Example, paginate_by=10) self.assertRaises(Http404, self.get, view, page="null")
def test_empty_list_not_found(self): view = ListView.as_view(model=Example, allow_empty=False) self.assertRaises(Http404, self.get, view, pk=999)