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