2 from django.test import TestCase, Client
4 # Create your tests here.
5 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_views_all_data(self):
57 q = Quote.objects.filter(text__startswith="Quote01")
58 self.assertEqual(q.count(), 1)
61 # check the individual quote page
62 quotepage = self.getPage('show/%s/' % q.id)
63 self.assertTrue(q.text in quotepage)
64 self.assertTrue("author_notes" in quotepage)
65 self.assertEqual(quotepage.count("tag_link"), 2)
68 workpage = self.getPage('work/%s/' % q.work.id)
69 self.assertTrue(q.text in workpage)
70 self.assertTrue("work_notes" in workpage)
72 # check the author page
73 authorpage = self.getPage('author/%s/' % q.work.author.id)
74 self.assertTrue(q.text in authorpage)
75 self.assertTrue("author_notes" in authorpage)
78 for tag in q.tags.all():
79 tagpage = self.getPage('tag/%s/' % tag.id)
80 self.assertTrue(q.text in tagpage)
82 def test_views_minimal_data(self):
83 q = Quote.objects.filter(text__startswith="Quote02")
84 self.assertEqual(q.count(), 1)
87 quotepage = self.getPage('show/%s/' % q.id)
88 self.assertTrue(q.text in quotepage)
89 self.assertFalse("author_notes" in quotepage)
90 self.assertEqual(quotepage.count("tag_link"), 0)
92 authorpage = self.getPage('author/%s/' % q.work.author.id)
93 self.assertTrue(q.text in authorpage)
94 self.assertFalse("author_notes" in authorpage)
95 self.assertFalse('Quote01, two tags' in authorpage)
97 def test_view_author_notes_once(self):
98 # check that on the per-author view, the author notes aren't display
100 a = Author.objects.filter(name="Author with notes")
101 self.assertEqual(a.count(), 1)
104 authorpage = self.getPage('author/%s/' % a.id)
105 self.assertEqual(authorpage.count("Some notes for the author"), 1)