1 from django.test import TestCase, Client
3 # Create your tests here.
4 from .models import Tag, 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="Ich bin...", work=w1)
16 q = Quote.objects.filter(text__startswith="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))
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",
40 notes="Some notes for the work")
41 q1 = Quote.objects.create(text="Quote01, two tags",
43 notes="Some notes for the quote")
44 t1 = Tag.objects.create(tag="tag01-1")
45 t2 = Tag.objects.create(tag="tag01-2")
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(), [])
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)
65 def test_random(self):
67 for q in Quote.objects.all():
70 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
71 permalinkre = re.compile(r'([0-9]+).*Permalink')
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)
80 for q in Quote.objects.all():
81 self.assertTrue(seen[q.id])
83 def test_views_all_data(self):
84 q = Quote.objects.filter(text__startswith="Quote01")
85 self.assertEqual(q.count(), 1)
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)
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)
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)
115 for tag in q.tags.all():
116 tagpage = self.getPage('tag/%s/' % tag.id)
117 self.assertIn(q.text, tagpage)
119 def test_views_minimal_data(self):
120 q = Quote.objects.filter(text__startswith="Quote02")
121 self.assertEqual(q.count(), 1)
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)
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)
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)
143 def test_view_author_notes_once(self):
144 # check that on the per-author view, the author notes aren't display
146 a = Author.objects.filter(name="Author with notes")
147 self.assertEqual(a.count(), 1)
150 authorpage = self.getPage('author/%s/' % a.id)
151 self.assertEqual(authorpage.count("Some notes for the author"), 1)