1 from django.test import TestCase, Client
3 # Create your tests here.
4 from .models import QuoteTag, Author, Work, Quote
8 class QuoteTest(TestCase):
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)
16 q = Quote.objects.filter(text__startswith="<p>Ich")
17 self.assertEqual(q.count(), 1)
19 self.assertEqual(q, self.q1)
20 self.assertEqual(q.work.author.name, "JFK")
22 class ViewsTest(TestCase):
23 def getPage(self, url, exp_status=200):
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)
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)
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",
41 notes="<p>Some notes for the work</p>")
42 q1 = Quote.objects.create(text="<p>Quote01, two tags</p>",
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")
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(), [])
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)
66 def test_random(self):
68 for q in Quote.objects.all():
71 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
72 permalinkre = re.compile(r'([0-9]+).*Permalink')
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)
81 for q in Quote.objects.all():
82 self.assertTrue(seen[q.id])
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")
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)
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)
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)
116 for tag in q.tags.all():
117 tagpage = self.getPage('tag/%s/' % tag.id)
118 self.assertIn(q.text, tagpage)
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")
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)
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)
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)
145 def test_view_author_notes_once(self):
146 # check that on the per-author view, the author notes aren't display
148 a = Author.objects.filter(name="Author with notes")
149 self.assertEqual(a.count(), 1)
152 authorpage = self.getPage('author/%s/' % a.id)
153 self.assertEqual(authorpage.count("Some notes for the author"), 1)