]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tests.py
Better implement tags, with separate namespaces
[djsite.git] / quotes / tests.py
1 from django.test import TestCase, Client
2
3 # Create your tests here.
4 from .models import QuoteTag, 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="<p>Ich bin...</p>", work=w1)
13         self.q1 = q1
14
15     def test_one(self):
16         q = Quote.objects.filter(text__startswith="<p>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         self.assertFalse('<script>' in document)
34         return document
35
36     def setUp(self):
37         a1 = Author.objects.create(name="Author with notes",
38                                    notes="<script>Some notes for the author</script>")
39         w1 = Work.objects.create(name="Context with some notes",
40                                  author=a1,
41                                  notes="<p>Some notes for the work</p>")
42         q1 = Quote.objects.create(text="<p>Quote01, two tags</p>",
43                                   work=w1,
44                                   notes="<p>Some notes for the quote</p>")
45         t1 = QuoteTag.objects.create(tag="tag01-1")
46         t2 = QuoteTag.objects.create(tag="tag01-2")
47         q1.tags.add(t1, t2)
48
49         a2 = Author.objects.create(name="Author without notes")
50         w2 = Work.objects.create(name="Work without notes", author=a2)
51         q2 = Quote.objects.create(text="<p>Quote02, no tags</p>", work=w2)
52         self.assertSequenceEqual(q2.tags.all(), [])
53
54     def test_all(self):
55         content = self.getPage('all/')
56         for a in Author.objects.all():
57             self.assertIn(a.name, content)
58             self.assertIn(a.notes, content)
59         for w in Work.objects.all():
60             self.assertIn(w.name, content)
61             self.assertIn(w.notes, content)
62         for q in Quote.objects.all():
63             self.assertIn(q.text, content)
64             self.assertIn(q.notes, content)
65
66     def test_random(self):
67         seen = {}
68         for q in Quote.objects.all():
69             seen[q.id] = False
70
71         # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
72         permalinkre = re.compile(r'([0-9]+).*Permalink')
73
74         for i in range(100):
75             content = self.getPage('random')
76             m = permalinkre.search(content)
77             self.assertIsNotNone(m, content)
78             quoteid = int(m.group(1))
79             self.assertIn(quoteid, seen)
80             seen[quoteid] = True
81         for q in Quote.objects.all():
82             self.assertTrue(seen[q.id])
83
84     def test_views_all_data(self):
85         q = Quote.objects.filter(text__startswith="<p>Quote01")
86         self.assertEqual(q.count(), 1, "Couldn't find Quote01 after insertion")
87         q = q[0]
88
89         # check the individual quote page; each of the note type is displayed
90         quotepage = self.getPage('show/%s/' % q.id)
91         self.assertIn(q.text, quotepage)
92         self.assertIn("author_notes", quotepage)
93         self.assertIn("work_notes", quotepage)
94         self.assertIn("tag_link", quotepage)
95         self.assertEqual(quotepage.count("tag_link"), 2)
96
97         # check the work page; each of the note type is displayed, and
98         # the work notes are shown only once
99         workpage = self.getPage('work/%s/' % q.work.id)
100         self.assertIn(q.text, workpage)
101         self.assertIn("author_notes", workpage)
102         self.assertIn("work_notes", workpage)
103         self.assertEqual(workpage.count("work_notes"), 1)
104         self.assertIn("tag_link", workpage)
105
106         # check the author page; each of the note type is displayed, and
107         # the author notes are shown only once
108         authorpage = self.getPage('author/%s/' % q.work.author.id)
109         self.assertIn(q.text, authorpage)
110         self.assertIn("author_notes", authorpage)
111         self.assertEqual(authorpage.count("author_notes"), 1)
112         self.assertIn("work_notes", authorpage)
113         self.assertIn("tag_link", authorpage)
114
115         # check the tag page
116         for tag in q.tags.all():
117             tagpage = self.getPage('tag/%s/' % tag.id)
118             self.assertIn(q.text, tagpage)
119
120     def test_views_minimal_data(self):
121         q = Quote.objects.filter(text__startswith="<p>Quote02")
122         self.assertEqual(q.count(), 1,
123                          "Couldn't find Quote02 after insertion")
124         q = q[0]
125
126         quotepage = self.getPage('show/%s/' % q.id)
127         self.assertIn(q.text, quotepage)
128         self.assertNotIn("author_notes", quotepage)
129         self.assertNotIn("work_notes", quotepage)
130         self.assertNotIn("tag_link", quotepage)
131
132         workpage = self.getPage('work/%s/' % q.work.id)
133         self.assertIn(q.text, workpage)
134         self.assertNotIn("author_notes", quotepage)
135         self.assertNotIn("work_notes", quotepage)
136         self.assertNotIn("tag_link", quotepage)
137
138         authorpage = self.getPage('author/%s/' % q.work.author.id)
139         self.assertIn(q.text, authorpage)
140         self.assertNotIn("author_notes", authorpage)
141         self.assertNotIn("work_notes", authorpage)
142         self.assertNotIn("tag_link", authorpage)
143         self.assertNotIn('Quote01, two tags', authorpage)
144
145     def test_view_author_notes_once(self):
146         # check that on the per-author view, the author notes aren't display
147         # for every quote
148         a = Author.objects.filter(name="Author with notes")
149         self.assertEqual(a.count(), 1)
150         a = a[0]
151
152         authorpage = self.getPage('author/%s/' % a.id)
153         self.assertEqual(authorpage.count("Some notes for the author"), 1)