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)
15 q = Quote.objects.filter(text__startswith="Ich")
16 self.assertEqual(q.count(), 1)
18 self.assertEqual(q, self.q1)
19 self.assertEqual(q.work.author.name, "JFK")
21 class ViewsTest(TestCase):
22 def getPage(self, url, exp_status=200):
24 response = c.get('/quotes/' + url)
25 self.assertEqual(response.status_code, 200)
26 self.assertEqual(response.charset, 'utf-8')
27 return response.content.decode(response.charset)
30 a1 = Author.objects.create(name="Author with notes",
31 notes="Some notes for the author")
32 w1 = Work.objects.create(name="Context with some notes",
34 notes="Some notes for the work")
35 q1 = Quote.objects.create(text="Quote01, two tags",
37 notes="Some notes for the quote")
38 t1 = Tag.objects.create(tag="tag01-1")
39 t2 = Tag.objects.create(tag="tag01-2")
42 a2 = Author.objects.create(name="Author without notes")
43 w2 = Work.objects.create(name="Work without notes", author=a2)
44 q2 = Quote.objects.create(text="Quote02, no tags", work=w2)
45 self.assertSequenceEqual(q2.tags.all(), [])
48 content = self.getPage('all/')
49 for a in Author.objects.all():
50 self.assertIn(a.name, content)
51 self.assertIn(a.notes, content)
52 for w in Work.objects.all():
53 self.assertIn(w.name, content)
54 self.assertIn(w.notes, content)
55 for q in Quote.objects.all():
56 self.assertIn(q.text, content)
57 self.assertIn(q.notes, content)
59 def test_random(self):
61 for q in Quote.objects.all():
64 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
65 permalinkre = re.compile(r'([0-9]+).*Permalink')
68 content = self.getPage('random')
69 m = permalinkre.search(content)
70 self.assertIsNotNone(m, content)
71 quoteid = int(m.group(1))
72 self.assertIn(quoteid, seen)
74 for q in Quote.objects.all():
75 self.assertTrue(seen[q.id])
77 def test_views_all_data(self):
78 q = Quote.objects.filter(text__startswith="Quote01")
79 self.assertEqual(q.count(), 1)
82 # check the individual quote page; each of the note type is displayed
83 quotepage = self.getPage('show/%s/' % q.id)
84 self.assertIn(q.text, quotepage)
85 self.assertIn("author_notes", quotepage)
86 self.assertIn("work_notes", quotepage)
87 self.assertIn("tag_link", quotepage)
88 self.assertEqual(quotepage.count("tag_link"), 2)
90 # check the work page; each of the note type is displayed, and
91 # the work notes are shown only once
92 workpage = self.getPage('work/%s/' % q.work.id)
93 self.assertIn(q.text, workpage)
94 self.assertIn("author_notes", workpage)
95 self.assertIn("work_notes", workpage)
96 self.assertEqual(workpage.count("work_notes"), 1)
97 self.assertIn("tag_link", workpage)
99 # check the author page; each of the note type is displayed, and
100 # the author notes are shown only once
101 authorpage = self.getPage('author/%s/' % q.work.author.id)
102 self.assertIn(q.text, authorpage)
103 self.assertIn("author_notes", authorpage)
104 self.assertEqual(authorpage.count("author_notes"), 1)
105 self.assertIn("work_notes", authorpage)
106 self.assertIn("tag_link", authorpage)
109 for tag in q.tags.all():
110 tagpage = self.getPage('tag/%s/' % tag.id)
111 self.assertIn(q.text, tagpage)
113 def test_views_minimal_data(self):
114 q = Quote.objects.filter(text__startswith="Quote02")
115 self.assertEqual(q.count(), 1)
118 quotepage = self.getPage('show/%s/' % q.id)
119 self.assertIn(q.text, quotepage)
120 self.assertNotIn("author_notes", quotepage)
121 self.assertNotIn("work_notes", quotepage)
122 self.assertNotIn("tag_link", quotepage)
124 workpage = self.getPage('work/%s/' % q.work.id)
125 self.assertIn(q.text, workpage)
126 self.assertNotIn("author_notes", quotepage)
127 self.assertNotIn("work_notes", quotepage)
128 self.assertNotIn("tag_link", quotepage)
130 authorpage = self.getPage('author/%s/' % q.work.author.id)
131 self.assertIn(q.text, authorpage)
132 self.assertNotIn("author_notes", authorpage)
133 self.assertNotIn("work_notes", authorpage)
134 self.assertNotIn("tag_link", authorpage)
135 self.assertNotIn('Quote01, two tags', authorpage)
137 def test_view_author_notes_once(self):
138 # check that on the per-author view, the author notes aren't display
140 a = Author.objects.filter(name="Author with notes")
141 self.assertEqual(a.count(), 1)
144 authorpage = self.getPage('author/%s/' % a.id)
145 self.assertEqual(authorpage.count("Some notes for the author"), 1)