2 from django.test import TestCase, Client
4 # Create your tests here.
5 from .models import Author, Tag, Quote
7 class QuoteTest(TestCase):
9 a1 = Author.objects.create(name="JFK")
10 q1 = Quote.objects.create(text="Ich bin...", author=a1)
13 q = Quote.objects.filter(text__startswith="Ich")
14 self.assertEqual(q.count(), 1)
16 self.assertEqual(q.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",
30 t1 = Tag.objects.create(tag="tag01-1")
31 t2 = Tag.objects.create(tag="tag01-2")
32 q1 = Quote.objects.create(text="Quote01, two tags", author=a1)
35 a2 = Author.objects.create(name="Author without notes")
36 q2= Quote.objects.create(text="Quote02, no tags", author=a2)
39 content = self.getPage('all/')
40 for a in Author.objects.all():
41 self.assertTrue(a.name in content)
42 for q in Quote.objects.all():
43 self.assertTrue(q.text in content)
45 def test_views_all_data(self):
46 q = Quote.objects.filter(text__startswith="Quote01")
47 self.assertEqual(q.count(), 1)
50 # check the individual quote page
51 quotepage = self.getPage('show/%s/' % q.id)
52 self.assertTrue(q.text in quotepage)
53 self.assertTrue("author_notes" in quotepage)
54 self.assertEqual(quotepage.count("tag_link"), 2)
56 # check the author page
57 authorpage = self.getPage('author/%s/' % q.author.id)
58 self.assertTrue(q.text in authorpage)
59 self.assertTrue("author_notes" in authorpage)
62 for tag in q.tags.all():
63 tagpage = self.getPage('tag/%s/' % tag.id)
64 self.assertTrue(q.text in tagpage)
66 def test_views_minimal_data(self):
67 q = Quote.objects.filter(text__startswith="Quote02")
68 self.assertEqual(q.count(), 1)
71 quotepage = self.getPage('show/%s/' % q.id)
72 self.assertTrue(q.text in quotepage)
73 self.assertFalse("author_notes" in quotepage)
74 self.assertEqual(quotepage.count("tag_link"), 0)
76 authorpage = self.getPage('author/%s/' % q.author.id)
77 self.assertTrue(q.text in authorpage)
78 self.assertFalse("author_notes" in authorpage)
79 self.assertFalse('Quote01, two tags' in authorpage)
81 def test_view_author_notes_once(self):
82 # check that on the per-author view, the author notes aren't display
84 a = Author.objects.filter(name="Author with notes")
85 self.assertEqual(a.count(), 1)
88 authorpage = self.getPage('author/%s/' % a.id)
89 self.assertEqual(authorpage.count("Some notes"), 1)