1 from django.test import TestCase, Client
3 # Create your tests here.
6 from .models import QuoteTag, Author, Work, Quote
10 class Test_QuoteTest():
11 @pytest.fixture(scope='function')
13 a1 = Author.objects.create(name="JFK")
14 w1 = Work.objects.create(name="Berlin speech", author=a1)
15 q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
18 @pytest.mark.django_db
19 def test_one(self, q1):
20 q = Quote.objects.filter(text__startswith="<p>Ich")
24 assert q.work.author.name == 'JFK'
26 class ViewsTest(TestCase):
27 def getPage(self, url, exp_status=200):
29 response = c.get('/quotes/' + url)
30 self.assertEqual(response.status_code, 200)
31 self.assertEqual(response.charset, 'utf-8')
32 document = response.content.decode(response.charset)
34 lxml.etree.fromstring(document)
35 except lxml.etree.XMLSyntaxError as e:
36 self.assertTrue(False, 'Errors in page at %s: %s' % (url, e))
37 self.assertFalse('<script>' in document)
41 a1 = Author.objects.create(name="Author with notes",
42 notes="<script>Some notes for the author</script>")
43 w1 = Work.objects.create(name="Context with some notes",
45 notes="<p>Some notes for the work</p>")
46 q1 = Quote.objects.create(text="<p>Quote01, two tags</p>",
48 notes="<p>Some notes for the quote</p>")
49 t1 = QuoteTag.objects.create(tag="tag01-1")
50 t2 = QuoteTag.objects.create(tag="tag01-2")
53 a2 = Author.objects.create(name="Author without notes")
54 w2 = Work.objects.create(name="Work without notes", author=a2)
55 q2 = Quote.objects.create(text="<p>Quote02, no tags</p>", work=w2)
56 self.assertSequenceEqual(q2.tags.all(), [])
59 content = self.getPage('all/')
60 for a in Author.objects.all():
61 self.assertIn(a.name, content)
62 self.assertIn(a.notes, content)
63 for w in Work.objects.all():
64 self.assertIn(w.name, content)
65 self.assertIn(w.notes, content)
66 for q in Quote.objects.all():
67 self.assertIn(q.text, content)
68 self.assertIn(q.notes, content)
70 def test_random(self):
72 for q in Quote.objects.all():
75 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
76 permalinkre = re.compile(r'([0-9]+).*Permalink')
79 content = self.getPage('random')
80 m = permalinkre.search(content)
81 self.assertIsNotNone(m, content)
82 quoteid = int(m.group(1))
83 self.assertIn(quoteid, seen)
85 for q in Quote.objects.all():
86 self.assertTrue(seen[q.id])
88 def test_views_all_data(self):
89 q = Quote.objects.filter(text__startswith="<p>Quote01")
90 self.assertEqual(q.count(), 1, "Couldn't find Quote01 after insertion")
93 # check the individual quote page; each of the note type is displayed
94 quotepage = self.getPage('show/%s/' % q.id)
95 self.assertIn(q.text, quotepage)
96 self.assertIn("author_notes", quotepage)
97 self.assertIn("work_notes", quotepage)
98 self.assertIn("tag_link", quotepage)
99 self.assertEqual(quotepage.count("tag_link"), 2)
101 # check the work page; each of the note type is displayed, and
102 # the work notes are shown only once
103 workpage = self.getPage('work/%s/' % q.work.id)
104 self.assertIn(q.text, workpage)
105 self.assertIn("author_notes", workpage)
106 self.assertIn("work_notes", workpage)
107 self.assertEqual(workpage.count("work_notes"), 1)
108 self.assertIn("tag_link", workpage)
110 # check the author page; each of the note type is displayed, and
111 # the author notes are shown only once
112 authorpage = self.getPage('author/%s/' % q.work.author.id)
113 self.assertIn(q.text, authorpage)
114 self.assertIn("author_notes", authorpage)
115 self.assertEqual(authorpage.count("author_notes"), 1)
116 self.assertIn("work_notes", authorpage)
117 self.assertIn("tag_link", authorpage)
120 for tag in q.tags.all():
121 tagpage = self.getPage('tag/%s/' % tag.id)
122 self.assertIn(q.text, tagpage)
124 def test_views_minimal_data(self):
125 q = Quote.objects.filter(text__startswith="<p>Quote02")
126 self.assertEqual(q.count(), 1,
127 "Couldn't find Quote02 after insertion")
130 quotepage = self.getPage('show/%s/' % q.id)
131 self.assertIn(q.text, quotepage)
132 self.assertNotIn("author_notes", quotepage)
133 self.assertNotIn("work_notes", quotepage)
134 self.assertNotIn("tag_link", quotepage)
136 workpage = self.getPage('work/%s/' % q.work.id)
137 self.assertIn(q.text, workpage)
138 self.assertNotIn("author_notes", quotepage)
139 self.assertNotIn("work_notes", quotepage)
140 self.assertNotIn("tag_link", quotepage)
142 authorpage = self.getPage('author/%s/' % q.work.author.id)
143 self.assertIn(q.text, authorpage)
144 self.assertNotIn("author_notes", authorpage)
145 self.assertNotIn("work_notes", authorpage)
146 self.assertNotIn("tag_link", authorpage)
147 self.assertNotIn('Quote01, two tags', authorpage)
149 def test_view_author_notes_once(self):
150 # check that on the per-author view, the author notes aren't display
152 a = Author.objects.filter(name="Author with notes")
153 self.assertEqual(a.count(), 1)
156 authorpage = self.getPage('author/%s/' % a.id)
157 self.assertEqual(authorpage.count("Some notes for the author"), 1)