2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2013-12-14 23:15:35 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
2014-04-05 06:04:46 +00:00
|
|
|
from django.test import TestCase, override_settings
|
2012-08-18 13:46:17 +00:00
|
|
|
from django.views.generic.base import View
|
2010-10-18 13:34:47 +00:00
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
from .models import Artist, Author, Page
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
2010-10-18 13:34:47 +00:00
|
|
|
class DetailViewTest(TestCase):
|
|
|
|
fixtures = ['generic-views-test-data.json']
|
|
|
|
|
|
|
|
def test_simple_object(self):
|
|
|
|
res = self.client.get('/detail/obj/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], {'foo': 'bar'})
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(res.context['view'], View)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/detail.html')
|
|
|
|
|
|
|
|
def test_detail_by_pk(self):
|
|
|
|
res = self.client.get('/detail/author/1/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=1))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(pk=1))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
|
|
|
|
2013-12-14 23:15:35 +00:00
|
|
|
def test_detail_missing_object(self):
|
|
|
|
res = self.client.get('/detail/author/500/')
|
|
|
|
self.assertEqual(res.status_code, 404)
|
|
|
|
|
|
|
|
def test_detail_object_does_not_exist(self):
|
|
|
|
self.assertRaises(ObjectDoesNotExist, self.client.get, '/detail/doesnotexist/1/')
|
|
|
|
|
2011-07-29 09:41:00 +00:00
|
|
|
def test_detail_by_custom_pk(self):
|
|
|
|
res = self.client.get('/detail/author/bycustompk/1/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=1))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(pk=1))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
def test_detail_by_slug(self):
|
|
|
|
res = self.client.get('/detail/author/byslug/scott-rosenberg/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(slug='scott-rosenberg'))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(slug='scott-rosenberg'))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
|
|
|
|
2011-07-29 09:41:00 +00:00
|
|
|
def test_detail_by_custom_slug(self):
|
|
|
|
res = self.client.get('/detail/author/bycustomslug/scott-rosenberg/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(slug='scott-rosenberg'))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(slug='scott-rosenberg'))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
|
|
|
|
2011-01-03 13:15:58 +00:00
|
|
|
def test_verbose_name(self):
|
|
|
|
res = self.client.get('/detail/artist/1/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Artist.objects.get(pk=1))
|
2011-02-14 13:04:51 +00:00
|
|
|
self.assertEqual(res.context['artist'], Artist.objects.get(pk=1))
|
2011-01-03 13:15:58 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/artist_detail.html')
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
def test_template_name(self):
|
|
|
|
res = self.client.get('/detail/author/1/template_name/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=1))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(pk=1))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/about.html')
|
|
|
|
|
|
|
|
def test_template_name_suffix(self):
|
|
|
|
res = self.client.get('/detail/author/1/template_name_suffix/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=1))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(pk=1))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_view.html')
|
|
|
|
|
|
|
|
def test_template_name_field(self):
|
|
|
|
res = self.client.get('/detail/page/1/field/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Page.objects.get(pk=1))
|
|
|
|
self.assertEqual(res.context['page'], Page.objects.get(pk=1))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/page_template.html')
|
|
|
|
|
|
|
|
def test_context_object_name(self):
|
|
|
|
res = self.client.get('/detail/author/1/context_object_name/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=1))
|
|
|
|
self.assertEqual(res.context['thingy'], Author.objects.get(pk=1))
|
|
|
|
self.assertFalse('author' in res.context)
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
|
|
|
|
|
|
|
def test_duplicated_context_object_name(self):
|
|
|
|
res = self.client.get('/detail/author/1/dupe_context_object_name/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=1))
|
|
|
|
self.assertFalse('author' in res.context)
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
|
|
|
|
|
|
|
def test_invalid_url(self):
|
|
|
|
self.assertRaises(AttributeError, self.client.get, '/detail/author/invalid/url/')
|
|
|
|
|
|
|
|
def test_invalid_queryset(self):
|
|
|
|
self.assertRaises(ImproperlyConfigured, self.client.get, '/detail/author/invalid/qs/')
|
2012-06-08 22:12:14 +00:00
|
|
|
|
|
|
|
def test_non_model_object_with_meta(self):
|
|
|
|
res = self.client.get('/detail/nonmodel/1/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context['object'].id, "non_model_1")
|