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)
45 content = self.getPage('all/')
46 for a in Author.objects.all():
47 self.assertTrue(a.name in content, msg=content)
48 self.assertTrue(a.notes in content, msg=content)
49 for w in Work.objects.all():
50 self.assertTrue(w.name in content)
51 self.assertTrue(w.notes in content, content)
52 for q in Quote.objects.all():
53 self.assertTrue(q.text in content, content)
54 self.assertTrue(q.notes in content, content)
56 def test_random(self):
58 for q in Quote.objects.all():
61 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
62 permalinkre = re.compile(r'([0-9]+).*Permalink')
65 content = self.getPage('random')
66 m = permalinkre.search(content)
67 self.assertIsNotNone(m, content)
68 quoteid = int(m.group(1))
69 self.assertIn(quoteid, seen)
71 for q in Quote.objects.all():
72 self.assertTrue(seen[q.id])
74 def test_views_all_data(self):
75 q = Quote.objects.filter(text__startswith="Quote01")
76 self.assertEqual(q.count(), 1)
79 # check the individual quote page
80 quotepage = self.getPage('show/%s/' % q.id)
81 self.assertTrue(q.text in quotepage)
82 self.assertTrue("author_notes" in quotepage)
83 self.assertEqual(quotepage.count("tag_link"), 2)
86 workpage = self.getPage('work/%s/' % q.work.id)
87 self.assertTrue(q.text in workpage)
88 self.assertTrue("work_notes" in workpage)
90 # check the author page
91 authorpage = self.getPage('author/%s/' % q.work.author.id)
92 self.assertTrue(q.text in authorpage)
93 self.assertTrue("author_notes" in authorpage)
96 for tag in q.tags.all():
97 tagpage = self.getPage('tag/%s/' % tag.id)
98 self.assertTrue(q.text in tagpage)
100 def test_views_minimal_data(self):
101 q = Quote.objects.filter(text__startswith="Quote02")
102 self.assertEqual(q.count(), 1)
105 quotepage = self.getPage('show/%s/' % q.id)
106 self.assertTrue(q.text in quotepage)
107 self.assertFalse("author_notes" in quotepage)
108 self.assertEqual(quotepage.count("tag_link"), 0)
110 authorpage = self.getPage('author/%s/' % q.work.author.id)
111 self.assertTrue(q.text in authorpage)
112 self.assertFalse("author_notes" in authorpage)
113 self.assertFalse('Quote01, two tags' in authorpage)
115 def test_view_author_notes_once(self):
116 # check that on the per-author view, the author notes aren't display
118 a = Author.objects.filter(name="Author with notes")
119 self.assertEqual(a.count(), 1)
122 authorpage = self.getPage('author/%s/' % a.id)
123 self.assertEqual(authorpage.count("Some notes for the author"), 1)