mirror of https://github.com/django/django.git
130 lines
6.1 KiB
Python
130 lines
6.1 KiB
Python
|
from django.core.exceptions import ImproperlyConfigured
|
||
|
from django.test import TestCase
|
||
|
|
||
|
from regressiontests.generic_views.models import Author
|
||
|
|
||
|
|
||
|
class ListViewTests(TestCase):
|
||
|
fixtures = ['generic-views-test-data.json']
|
||
|
urls = 'regressiontests.generic_views.urls'
|
||
|
|
||
|
def test_items(self):
|
||
|
res = self.client.get('/list/dict/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertTemplateUsed(res, 'generic_views/list.html')
|
||
|
self.assertEqual(res.context['object_list'][0]['first'], 'John')
|
||
|
|
||
|
def test_queryset(self):
|
||
|
res = self.client.get('/list/authors/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_list.html')
|
||
|
self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
|
||
|
self.assertEqual(list(res.context['authors']), list(Author.objects.all()))
|
||
|
self.assertEqual(res.context['paginator'], None)
|
||
|
self.assertEqual(res.context['page_obj'], None)
|
||
|
self.assertEqual(res.context['is_paginated'], False)
|
||
|
|
||
|
def test_paginated_queryset(self):
|
||
|
self._make_authors(100)
|
||
|
res = self.client.get('/list/authors/paginated/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_list.html')
|
||
|
self.assertEqual(len(res.context['authors']), 30)
|
||
|
self.assertEqual(res.context['is_paginated'], True)
|
||
|
self.assertEqual(res.context['page_obj'].number, 1)
|
||
|
self.assertEqual(res.context['paginator'].num_pages, 4)
|
||
|
self.assertEqual(res.context['authors'][0].name, 'Author 00')
|
||
|
self.assertEqual(list(res.context['authors'])[-1].name, 'Author 29')
|
||
|
|
||
|
def test_paginated_queryset_shortdata(self):
|
||
|
res = self.client.get('/list/authors/paginated/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_list.html')
|
||
|
self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
|
||
|
self.assertEqual(list(res.context['authors']), list(Author.objects.all()))
|
||
|
self.assertEqual(res.context['paginator'], None)
|
||
|
self.assertEqual(res.context['page_obj'], None)
|
||
|
self.assertEqual(res.context['is_paginated'], False)
|
||
|
|
||
|
def test_paginated_get_page_by_query_string(self):
|
||
|
self._make_authors(100)
|
||
|
res = self.client.get('/list/authors/paginated/', {'page': '2'})
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_list.html')
|
||
|
self.assertEqual(len(res.context['authors']), 30)
|
||
|
self.assertEqual(res.context['authors'][0].name, 'Author 30')
|
||
|
self.assertEqual(res.context['page_obj'].number, 2)
|
||
|
|
||
|
def test_paginated_get_last_page_by_query_string(self):
|
||
|
self._make_authors(100)
|
||
|
res = self.client.get('/list/authors/paginated/', {'page': 'last'})
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertEqual(len(res.context['authors']), 10)
|
||
|
self.assertEqual(res.context['authors'][0].name, 'Author 90')
|
||
|
self.assertEqual(res.context['page_obj'].number, 4)
|
||
|
|
||
|
def test_paginated_get_page_by_urlvar(self):
|
||
|
self._make_authors(100)
|
||
|
res = self.client.get('/list/authors/paginated/3/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_list.html')
|
||
|
self.assertEqual(len(res.context['authors']), 30)
|
||
|
self.assertEqual(res.context['authors'][0].name, 'Author 60')
|
||
|
self.assertEqual(res.context['page_obj'].number, 3)
|
||
|
|
||
|
def test_paginated_page_out_of_range(self):
|
||
|
self._make_authors(100)
|
||
|
res = self.client.get('/list/authors/paginated/42/')
|
||
|
self.assertEqual(res.status_code, 404)
|
||
|
|
||
|
def test_paginated_invalid_page(self):
|
||
|
self._make_authors(100)
|
||
|
res = self.client.get('/list/authors/paginated/?page=frog')
|
||
|
self.assertEqual(res.status_code, 404)
|
||
|
|
||
|
def test_allow_empty_false(self):
|
||
|
res = self.client.get('/list/authors/notempty/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
Author.objects.all().delete()
|
||
|
res = self.client.get('/list/authors/notempty/')
|
||
|
self.assertEqual(res.status_code, 404)
|
||
|
|
||
|
def test_template_name(self):
|
||
|
res = self.client.get('/list/authors/template_name/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
|
||
|
self.assertEqual(list(res.context['authors']), list(Author.objects.all()))
|
||
|
self.assertTemplateUsed(res, 'generic_views/list.html')
|
||
|
|
||
|
def test_template_name_suffix(self):
|
||
|
res = self.client.get('/list/authors/template_name_suffix/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
|
||
|
self.assertEqual(list(res.context['authors']), list(Author.objects.all()))
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_objects.html')
|
||
|
|
||
|
def test_context_object_name(self):
|
||
|
res = self.client.get('/list/authors/context_object_name/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
|
||
|
self.assertEqual(list(res.context['author_list']), list(Author.objects.all()))
|
||
|
self.assertFalse('authors' in res.context)
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_list.html')
|
||
|
|
||
|
def test_duplicate_context_object_name(self):
|
||
|
res = self.client.get('/list/authors/dupe_context_object_name/')
|
||
|
self.assertEqual(res.status_code, 200)
|
||
|
self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
|
||
|
self.assertFalse('author_list' in res.context)
|
||
|
self.assertFalse('authors' in res.context)
|
||
|
self.assertTemplateUsed(res, 'generic_views/author_list.html')
|
||
|
|
||
|
def test_missing_items(self):
|
||
|
self.assertRaises(ImproperlyConfigured, self.client.get, '/list/authors/invalid/')
|
||
|
|
||
|
def _make_authors(self, n):
|
||
|
Author.objects.all().delete()
|
||
|
for i in range(n):
|
||
|
Author.objects.create(name='Author %02i' % i, slug='a%s' % i)
|
||
|
|