]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tests.py
3836f4e5e829dce28360b52e25235f0b2215dc40
[djsite.git] / quotes / tests.py
1 import sys
2 from django.test import TestCase, Client
3
4 # Create your tests here.
5 from .models import Tag, Author, Work, Quote
6
7 class QuoteTest(TestCase):
8     def setUp(self):
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)
12
13     def test_one(self):
14         q = Quote.objects.filter(text__startswith="Ich")
15         self.assertEqual(q.count(), 1)
16         q = q[0]
17         self.assertEqual(q.work.author.name, "JFK")
18
19 class ViewsTest(TestCase):
20     def getPage(self, url, exp_status=200):
21         c = Client()
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)
26
27     def setUp(self):
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",
31                                  author=a1,
32                                  notes="Some notes for the work")
33         q1 = Quote.objects.create(text="Quote01, two tags",
34                                   work=w1,
35                                   notes="Some notes for the quote")
36         t1 = Tag.objects.create(tag="tag01-1")
37         t2 = Tag.objects.create(tag="tag01-2")
38         q1.tags.add(t1, t2)
39
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
44     def test_all(self):
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)
55
56     def test_views_all_data(self):
57         q = Quote.objects.filter(text__startswith="Quote01")
58         self.assertEqual(q.count(), 1)
59         q = q[0]
60
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)
66
67         # check the work page
68         workpage = self.getPage('work/%s/' % q.work.id)
69         self.assertTrue(q.text in workpage)
70         self.assertTrue("work_notes" in workpage)
71         
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)
76
77         # check the tag page
78         for tag in q.tags.all():
79             tagpage = self.getPage('tag/%s/' % tag.id)
80             self.assertTrue(q.text in tagpage)
81
82     def test_views_minimal_data(self):
83         q = Quote.objects.filter(text__startswith="Quote02")
84         self.assertEqual(q.count(), 1)
85         q = q[0]
86
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)
91
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)        
96
97     def test_view_author_notes_once(self):
98         # check that on the per-author view, the author notes aren't display
99         # for every quote
100         a = Author.objects.filter(name="Author with notes")
101         self.assertEqual(a.count(), 1)
102         a = a[0]
103
104         authorpage = self.getPage('author/%s/' % a.id)
105         self.assertEqual(authorpage.count("Some notes for the author"), 1)