]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tests.py
Repair the /all/ page
[djsite.git] / quotes / tests.py
1 import sys
2 from django.test import TestCase, Client
3
4 # Create your tests here.
5 from .models import Author, Tag, Quote
6
7 class QuoteTest(TestCase):
8     def setUp(self):
9         a1 = Author.objects.create(name="JFK")
10         q1 = Quote.objects.create(text="Ich bin...", author=a1)
11
12     def test_one(self):
13         q = Quote.objects.filter(text__startswith="Ich")
14         self.assertEqual(q.count(), 1)
15         q = q[0]
16         self.assertEqual(q.author.name, "JFK")
17
18
19 class ViewsTest(TestCase):
20     def getPage(self, url, exp_status=200):
21         c = Client()
22         response = c.get('/quotes/' + url)
23         self.assertEqual(response.status_code, 200)
24         self.assertEqual(response.charset, 'utf-8')
25         return response.content.decode(response.charset)
26
27     def setUp(self):
28         a1 = Author.objects.create(name="Author with notes",
29                                    notes="Some notes")
30         t1 = Tag.objects.create(tag="tag01-1")
31         t2 = Tag.objects.create(tag="tag01-2")
32         q1 = Quote.objects.create(text="Quote01, two tags", author=a1)
33         q1.tags.add(t1, t2)
34
35         a2 = Author.objects.create(name="Author without notes")
36         q2= Quote.objects.create(text="Quote02, no tags", author=a2)
37
38     def test_all(self):
39         content = self.getPage('all/')
40         for a in Author.objects.all():
41             self.assertTrue(a.name in content)
42         for q in Quote.objects.all():
43             self.assertTrue(q.text in content)
44
45     def test_views_all_data(self):
46         q = Quote.objects.filter(text__startswith="Quote01")
47         self.assertEqual(q.count(), 1)
48         q = q[0]
49
50         # check the individual quote page
51         quotepage = self.getPage('show/%s/' % q.id)
52         self.assertTrue(q.text in quotepage)
53         self.assertTrue("author_notes" in quotepage)
54         self.assertEqual(quotepage.count("tag_link"), 2)
55
56         # check the author page
57         authorpage = self.getPage('author/%s/' % q.author.id)
58         self.assertTrue(q.text in authorpage)
59         self.assertTrue("author_notes" in authorpage)
60
61         # check the tag page
62         for tag in q.tags.all():
63             tagpage = self.getPage('tag/%s/' % tag.id)
64             self.assertTrue(q.text in tagpage)
65
66     def test_views_minimal_data(self):
67         q = Quote.objects.filter(text__startswith="Quote02")
68         self.assertEqual(q.count(), 1)
69         q = q[0]
70
71         quotepage = self.getPage('show/%s/' % q.id)
72         self.assertTrue(q.text in quotepage)
73         self.assertFalse("author_notes" in quotepage)
74         self.assertEqual(quotepage.count("tag_link"), 0)
75
76         authorpage = self.getPage('author/%s/' % q.author.id)
77         self.assertTrue(q.text in authorpage)
78         self.assertFalse("author_notes" in authorpage)
79         self.assertFalse('Quote01, two tags' in authorpage)        
80
81     def test_view_author_notes_once(self):
82         # check that on the per-author view, the author notes aren't display
83         # for every quote
84         a = Author.objects.filter(name="Author with notes")
85         self.assertEqual(a.count(), 1)
86         a = a[0]
87
88         authorpage = self.getPage('author/%s/' % a.id)
89         self.assertEqual(authorpage.count("Some notes"), 1)