]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tests.py
Add a pyflakes check, and fix the warnings
[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         self.q1 = q1
13
14     def test_one(self):
15         q = Quote.objects.filter(text__startswith="Ich")
16         self.assertEqual(q.count(), 1)
17         q = q[0]
18         self.assertEqual(q, self.q1)
19         self.assertEqual(q.work.author.name, "JFK")
20
21 class ViewsTest(TestCase):
22     def getPage(self, url, exp_status=200):
23         c = Client()
24         response = c.get('/quotes/' + url)
25         self.assertEqual(response.status_code, 200)
26         self.assertEqual(response.charset, 'utf-8')
27         return response.content.decode(response.charset)
28
29     def setUp(self):
30         a1 = Author.objects.create(name="Author with notes",
31                                    notes="Some notes for the author")
32         w1 = Work.objects.create(name="Context with some notes",
33                                  author=a1,
34                                  notes="Some notes for the work")
35         q1 = Quote.objects.create(text="Quote01, two tags",
36                                   work=w1,
37                                   notes="Some notes for the quote")
38         t1 = Tag.objects.create(tag="tag01-1")
39         t2 = Tag.objects.create(tag="tag01-2")
40         q1.tags.add(t1, t2)
41
42         a2 = Author.objects.create(name="Author without notes")
43         w2 = Work.objects.create(name="Work without notes", author=a2)
44         q2 = Quote.objects.create(text="Quote02, no tags", work=w2)
45         self.assertSequenceEqual(q2.tags.all(), [])
46
47     def test_all(self):
48         content = self.getPage('all/')
49         for a in Author.objects.all():
50             self.assertIn(a.name, content)
51             self.assertIn(a.notes, content)
52         for w in Work.objects.all():
53             self.assertIn(w.name, content)
54             self.assertIn(w.notes, content)
55         for q in Quote.objects.all():
56             self.assertIn(q.text, content)
57             self.assertIn(q.notes, content)
58
59     def test_random(self):
60         seen = {}
61         for q in Quote.objects.all():
62             seen[q.id] = False
63
64         # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
65         permalinkre = re.compile(r'([0-9]+).*Permalink')
66
67         for i in xrange(100):
68             content = self.getPage('random')
69             m = permalinkre.search(content)
70             self.assertIsNotNone(m, content)
71             quoteid = int(m.group(1))
72             self.assertIn(quoteid, seen)
73             seen[quoteid] = True
74         for q in Quote.objects.all():
75             self.assertTrue(seen[q.id])
76
77     def test_views_all_data(self):
78         q = Quote.objects.filter(text__startswith="Quote01")
79         self.assertEqual(q.count(), 1)
80         q = q[0]
81
82         # check the individual quote page; each of the note type is displayed
83         quotepage = self.getPage('show/%s/' % q.id)
84         self.assertIn(q.text, quotepage)
85         self.assertIn("author_notes", quotepage)
86         self.assertIn("work_notes", quotepage)
87         self.assertIn("tag_link", quotepage)
88         self.assertEqual(quotepage.count("tag_link"), 2)
89
90         # check the work page; each of the note type is displayed, and
91         # the work notes are shown only once
92         workpage = self.getPage('work/%s/' % q.work.id)
93         self.assertIn(q.text, workpage)
94         self.assertIn("author_notes", workpage)
95         self.assertIn("work_notes", workpage)
96         self.assertEqual(workpage.count("work_notes"), 1)
97         self.assertIn("tag_link", workpage)
98
99         # check the author page; each of the note type is displayed, and
100         # the author notes are shown only once
101         authorpage = self.getPage('author/%s/' % q.work.author.id)
102         self.assertIn(q.text, authorpage)
103         self.assertIn("author_notes", authorpage)
104         self.assertEqual(authorpage.count("author_notes"), 1)
105         self.assertIn("work_notes", authorpage)
106         self.assertIn("tag_link", authorpage)
107
108         # check the tag page
109         for tag in q.tags.all():
110             tagpage = self.getPage('tag/%s/' % tag.id)
111             self.assertIn(q.text, tagpage)
112
113     def test_views_minimal_data(self):
114         q = Quote.objects.filter(text__startswith="Quote02")
115         self.assertEqual(q.count(), 1)
116         q = q[0]
117
118         quotepage = self.getPage('show/%s/' % q.id)
119         self.assertIn(q.text, quotepage)
120         self.assertNotIn("author_notes", quotepage)
121         self.assertNotIn("work_notes", quotepage)
122         self.assertNotIn("tag_link", quotepage)
123
124         workpage = self.getPage('work/%s/' % q.work.id)
125         self.assertIn(q.text, workpage)
126         self.assertNotIn("author_notes", quotepage)
127         self.assertNotIn("work_notes", quotepage)
128         self.assertNotIn("tag_link", quotepage)
129
130         authorpage = self.getPage('author/%s/' % q.work.author.id)
131         self.assertIn(q.text, authorpage)
132         self.assertNotIn("author_notes", authorpage)
133         self.assertNotIn("work_notes", authorpage)
134         self.assertNotIn("tag_link", authorpage)
135         self.assertNotIn('Quote01, two tags', authorpage)
136
137     def test_view_author_notes_once(self):
138         # check that on the per-author view, the author notes aren't display
139         # for every quote
140         a = Author.objects.filter(name="Author with notes")
141         self.assertEqual(a.count(), 1)
142         a = a[0]
143
144         authorpage = self.getPage('author/%s/' % a.id)
145         self.assertEqual(authorpage.count("Some notes for the author"), 1)