]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tests.py
fe7e0a80456ac99edd9adbe959ecb4dce352e7a5
[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         self.assertSequenceEqual(q2.tags.all(), [])
44
45     def test_all(self):
46         content = self.getPage('all/')
47         for a in Author.objects.all():
48             self.assertIn(a.name, content)
49             self.assertIn(a.notes, content)
50         for w in Work.objects.all():
51             self.assertIn(w.name, content)
52             self.assertIn(w.notes, content)
53         for q in Quote.objects.all():
54             self.assertIn(q.text, content)
55             self.assertIn(q.notes, content)
56
57     def test_random(self):
58         seen = {}
59         for q in Quote.objects.all():
60             seen[q.id] = False
61
62         # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
63         permalinkre = re.compile(r'([0-9]+).*Permalink')
64
65         for i in xrange(100):
66             content = self.getPage('random')
67             m = permalinkre.search(content)
68             self.assertIsNotNone(m, content)
69             quoteid = int(m.group(1))
70             self.assertIn(quoteid, seen)
71             seen[quoteid] = True
72         for q in Quote.objects.all():
73             self.assertTrue(seen[q.id])
74
75     def test_views_all_data(self):
76         q = Quote.objects.filter(text__startswith="Quote01")
77         self.assertEqual(q.count(), 1)
78         q = q[0]
79
80         # check the individual quote page; each of the note type is displayed
81         quotepage = self.getPage('show/%s/' % q.id)
82         self.assertIn(q.text, quotepage)
83         self.assertIn("author_notes", quotepage)
84         self.assertIn("work_notes", quotepage)
85         self.assertIn("tag_link", quotepage)
86         self.assertEqual(quotepage.count("tag_link"), 2)
87
88         # check the work page; each of the note type is displayed, and
89         # the work notes are shown only once
90         workpage = self.getPage('work/%s/' % q.work.id)
91         self.assertIn(q.text, workpage)
92         self.assertIn("author_notes", workpage)
93         self.assertIn("work_notes", workpage)
94         self.assertEqual(workpage.count("work_notes"), 1)
95         self.assertIn("tag_link", workpage)
96
97         # check the author page; each of the note type is displayed, and
98         # the author notes are shown only once
99         authorpage = self.getPage('author/%s/' % q.work.author.id)
100         self.assertIn(q.text, authorpage)
101         self.assertIn("author_notes", authorpage)
102         self.assertEqual(authorpage.count("author_notes"), 1)
103         self.assertIn("work_notes", authorpage)
104         self.assertIn("tag_link", authorpage)
105
106         # check the tag page
107         for tag in q.tags.all():
108             tagpage = self.getPage('tag/%s/' % tag.id)
109             self.assertIn(q.text, tagpage)
110
111     def test_views_minimal_data(self):
112         q = Quote.objects.filter(text__startswith="Quote02")
113         self.assertEqual(q.count(), 1)
114         q = q[0]
115
116         quotepage = self.getPage('show/%s/' % q.id)
117         self.assertIn(q.text, quotepage)
118         self.assertNotIn("author_notes", quotepage)
119         self.assertNotIn("work_notes", quotepage)
120         self.assertNotIn("tag_link", quotepage)
121
122         workpage = self.getPage('work/%s/' % q.work.id)
123         self.assertIn(q.text, workpage)
124         self.assertNotIn("author_notes", quotepage)
125         self.assertNotIn("work_notes", quotepage)
126         self.assertNotIn("tag_link", quotepage)
127
128         authorpage = self.getPage('author/%s/' % q.work.author.id)
129         self.assertIn(q.text, authorpage)
130         self.assertNotIn("author_notes", authorpage)
131         self.assertNotIn("work_notes", authorpage)
132         self.assertNotIn("tag_link", authorpage)
133         self.assertNotIn('Quote01, two tags', authorpage)
134
135     def test_view_author_notes_once(self):
136         # check that on the per-author view, the author notes aren't display
137         # for every quote
138         a = Author.objects.filter(name="Author with notes")
139         self.assertEqual(a.count(), 1)
140         a = a[0]
141
142         authorpage = self.getPage('author/%s/' % a.id)
143         self.assertEqual(authorpage.count("Some notes for the author"), 1)