1 from django.test import TestCase, Client
3 # Create your tests here.
4 from .models import Tag, Author, Work, Quote
7 class QuoteTest(TestCase):
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)
14 q = Quote.objects.filter(text__startswith="Ich")
15 self.assertEqual(q.count(), 1)
17 self.assertEqual(q.work.author.name, "JFK")
19 class ViewsTest(TestCase):
20 def getPage(self, url, exp_status=200):
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)
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",
32 notes="Some notes for the work")
33 q1 = Quote.objects.create(text="Quote01, two tags",
35 notes="Some notes for the quote")
36 t1 = Tag.objects.create(tag="tag01-1")
37 t2 = Tag.objects.create(tag="tag01-2")
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(), [])
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)
57 def test_random(self):
59 for q in Quote.objects.all():
62 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
63 permalinkre = re.compile(r'([0-9]+).*Permalink')
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)
72 for q in Quote.objects.all():
73 self.assertTrue(seen[q.id])
75 def test_views_all_data(self):
76 q = Quote.objects.filter(text__startswith="Quote01")
77 self.assertEqual(q.count(), 1)
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)
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)
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)
107 for tag in q.tags.all():
108 tagpage = self.getPage('tag/%s/' % tag.id)
109 self.assertIn(q.text, tagpage)
111 def test_views_minimal_data(self):
112 q = Quote.objects.filter(text__startswith="Quote02")
113 self.assertEqual(q.count(), 1)
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)
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)
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)
135 def test_view_author_notes_once(self):
136 # check that on the per-author view, the author notes aren't display
138 a = Author.objects.filter(name="Author with notes")
139 self.assertEqual(a.count(), 1)
142 authorpage = self.getPage('author/%s/' % a.id)
143 self.assertEqual(authorpage.count("Some notes for the author"), 1)