]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tests.py
Add testing for the 'random' page
[djsite.git] / quotes / tests.py
1 from django.test import TestCase, Client
2
3 # Create your tests here.
4 from .models import Tag, Author, Work, Quote
5 import re
6
7 class QuoteTest(TestCase):
8     def setUp(self):
9         a1 = Author.objects.create(name="JFK")
10         w1 = Work.objects.create(name="Berlin speech", author=a1)
11         q1 = Quote.objects.create(text="Ich bin...", work=w1)
12
13     def test_one(self):
14         q = Quote.objects.filter(text__startswith="Ich")
15         self.assertEqual(q.count(), 1)
16         q = q[0]
17         self.assertEqual(q.work.author.name, "JFK")
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 for the author")
30         w1 = Work.objects.create(name="Context with some notes",
31                                  author=a1,
32                                  notes="Some notes for the work")
33         q1 = Quote.objects.create(text="Quote01, two tags",
34                                   work=w1,
35                                   notes="Some notes for the quote")
36         t1 = Tag.objects.create(tag="tag01-1")
37         t2 = Tag.objects.create(tag="tag01-2")
38         q1.tags.add(t1, t2)
39
40         a2 = Author.objects.create(name="Author without notes")
41         w2 = Work.objects.create(name="Work without notes", author=a2)
42         q2 = Quote.objects.create(text="Quote02, no tags", work=w2)
43
44     def test_all(self):
45         content = self.getPage('all/')
46         for a in Author.objects.all():
47             self.assertTrue(a.name in content, msg=content)
48             self.assertTrue(a.notes in content, msg=content)
49         for w in Work.objects.all():
50             self.assertTrue(w.name in content)
51             self.assertTrue(w.notes in content, content)
52         for q in Quote.objects.all():
53             self.assertTrue(q.text in content, content)
54             self.assertTrue(q.notes in content, content)
55
56     def test_random(self):
57         seen = {}
58         for q in Quote.objects.all():
59             seen[q.id] = False
60
61         # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
62         permalinkre = re.compile(r'([0-9]+).*Permalink')
63
64         for i in xrange(100):
65             content = self.getPage('random')
66             m = permalinkre.search(content)
67             self.assertIsNotNone(m, content)
68             quoteid = int(m.group(1))
69             self.assertIn(quoteid, seen)
70             seen[quoteid] = True
71         for q in Quote.objects.all():
72             self.assertTrue(seen[q.id])
73
74     def test_views_all_data(self):
75         q = Quote.objects.filter(text__startswith="Quote01")
76         self.assertEqual(q.count(), 1)
77         q = q[0]
78
79         # check the individual quote page
80         quotepage = self.getPage('show/%s/' % q.id)
81         self.assertTrue(q.text in quotepage)
82         self.assertTrue("author_notes" in quotepage)
83         self.assertEqual(quotepage.count("tag_link"), 2)
84
85         # check the work page
86         workpage = self.getPage('work/%s/' % q.work.id)
87         self.assertTrue(q.text in workpage)
88         self.assertTrue("work_notes" in workpage)
89         
90         # check the author page
91         authorpage = self.getPage('author/%s/' % q.work.author.id)
92         self.assertTrue(q.text in authorpage)
93         self.assertTrue("author_notes" in authorpage)
94
95         # check the tag page
96         for tag in q.tags.all():
97             tagpage = self.getPage('tag/%s/' % tag.id)
98             self.assertTrue(q.text in tagpage)
99
100     def test_views_minimal_data(self):
101         q = Quote.objects.filter(text__startswith="Quote02")
102         self.assertEqual(q.count(), 1)
103         q = q[0]
104
105         quotepage = self.getPage('show/%s/' % q.id)
106         self.assertTrue(q.text in quotepage)
107         self.assertFalse("author_notes" in quotepage)
108         self.assertEqual(quotepage.count("tag_link"), 0)
109
110         authorpage = self.getPage('author/%s/' % q.work.author.id)
111         self.assertTrue(q.text in authorpage)
112         self.assertFalse("author_notes" in authorpage)
113         self.assertFalse('Quote01, two tags' in authorpage)        
114
115     def test_view_author_notes_once(self):
116         # check that on the per-author view, the author notes aren't display
117         # for every quote
118         a = Author.objects.filter(name="Author with notes")
119         self.assertEqual(a.count(), 1)
120         a = a[0]
121
122         authorpage = self.getPage('author/%s/' % a.id)
123         self.assertEqual(authorpage.count("Some notes for the author"), 1)