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