PageRenderTime 29ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/regressiontests/generic_views/detail.py

https://code.google.com/p/mango-py/
Python | 78 lines | 64 code | 14 blank | 0 comment | 2 complexity | 185ca129d43a6006d42d2ea47e50cd83 MD5 | raw file
 1from django.core.exceptions import ImproperlyConfigured
 2from django.test import TestCase
 3
 4from regressiontests.generic_views.models import Artist, Author, Page
 5
 6
 7class DetailViewTest(TestCase):
 8    fixtures = ['generic-views-test-data.json']
 9    urls = 'regressiontests.generic_views.urls'
10
11    def test_simple_object(self):
12        res = self.client.get('/detail/obj/')
13        self.assertEqual(res.status_code, 200)
14        self.assertEqual(res.context['object'], {'foo': 'bar'})
15        self.assertTemplateUsed(res, 'generic_views/detail.html')
16
17    def test_detail_by_pk(self):
18        res = self.client.get('/detail/author/1/')
19        self.assertEqual(res.status_code, 200)
20        self.assertEqual(res.context['object'], Author.objects.get(pk=1))
21        self.assertEqual(res.context['author'], Author.objects.get(pk=1))
22        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
23
24    def test_detail_by_slug(self):
25        res = self.client.get('/detail/author/byslug/scott-rosenberg/')
26        self.assertEqual(res.status_code, 200)
27        self.assertEqual(res.context['object'], Author.objects.get(slug='scott-rosenberg'))
28        self.assertEqual(res.context['author'], Author.objects.get(slug='scott-rosenberg'))
29        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
30
31    def test_verbose_name(self):
32        res = self.client.get('/detail/artist/1/')
33        self.assertEqual(res.status_code, 200)
34        self.assertEqual(res.context['object'], Artist.objects.get(pk=1))
35        self.assertEqual(res.context['artist'], Artist.objects.get(pk=1))
36        self.assertTemplateUsed(res, 'generic_views/artist_detail.html')
37
38    def test_template_name(self):
39        res = self.client.get('/detail/author/1/template_name/')
40        self.assertEqual(res.status_code, 200)
41        self.assertEqual(res.context['object'], Author.objects.get(pk=1))
42        self.assertEqual(res.context['author'], Author.objects.get(pk=1))
43        self.assertTemplateUsed(res, 'generic_views/about.html')
44
45    def test_template_name_suffix(self):
46        res = self.client.get('/detail/author/1/template_name_suffix/')
47        self.assertEqual(res.status_code, 200)
48        self.assertEqual(res.context['object'], Author.objects.get(pk=1))
49        self.assertEqual(res.context['author'], Author.objects.get(pk=1))
50        self.assertTemplateUsed(res, 'generic_views/author_view.html')
51
52    def test_template_name_field(self):
53        res = self.client.get('/detail/page/1/field/')
54        self.assertEqual(res.status_code, 200)
55        self.assertEqual(res.context['object'], Page.objects.get(pk=1))
56        self.assertEqual(res.context['page'], Page.objects.get(pk=1))
57        self.assertTemplateUsed(res, 'generic_views/page_template.html')
58
59    def test_context_object_name(self):
60        res = self.client.get('/detail/author/1/context_object_name/')
61        self.assertEqual(res.status_code, 200)
62        self.assertEqual(res.context['object'], Author.objects.get(pk=1))
63        self.assertEqual(res.context['thingy'], Author.objects.get(pk=1))
64        self.assertFalse('author' in res.context)
65        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
66
67    def test_duplicated_context_object_name(self):
68        res = self.client.get('/detail/author/1/dupe_context_object_name/')
69        self.assertEqual(res.status_code, 200)
70        self.assertEqual(res.context['object'], Author.objects.get(pk=1))
71        self.assertFalse('author' in res.context)
72        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
73
74    def test_invalid_url(self):
75        self.assertRaises(AttributeError, self.client.get, '/detail/author/invalid/url/')
76
77    def test_invalid_queryset(self):
78        self.assertRaises(ImproperlyConfigured, self.client.get, '/detail/author/invalid/qs/')