-from django.test import TestCase
+from django.test import TestCase, Client
# Create your tests here.
+from .models import Tag, Author, Work, Quote
+import re
+
+class QuoteTest(TestCase):
+ def setUp(self):
+ a1 = Author.objects.create(name="JFK")
+ w1 = Work.objects.create(name="Berlin speech", author=a1)
+ q1 = Quote.objects.create(text="Ich bin...", work=w1)
+
+ def test_one(self):
+ q = Quote.objects.filter(text__startswith="Ich")
+ self.assertEqual(q.count(), 1)
+ q = q[0]
+ self.assertEqual(q.work.author.name, "JFK")
+
+class ViewsTest(TestCase):
+ def getPage(self, url, exp_status=200):
+ c = Client()
+ response = c.get('/quotes/' + url)
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.charset, 'utf-8')
+ return response.content.decode(response.charset)
+
+ def setUp(self):
+ a1 = Author.objects.create(name="Author with notes",
+ notes="Some notes for the author")
+ w1 = Work.objects.create(name="Context with some notes",
+ author=a1,
+ notes="Some notes for the work")
+ q1 = Quote.objects.create(text="Quote01, two tags",
+ work=w1,
+ notes="Some notes for the quote")
+ t1 = Tag.objects.create(tag="tag01-1")
+ t2 = Tag.objects.create(tag="tag01-2")
+ q1.tags.add(t1, t2)
+
+ a2 = Author.objects.create(name="Author without notes")
+ w2 = Work.objects.create(name="Work without notes", author=a2)
+ q2 = Quote.objects.create(text="Quote02, no tags", work=w2)
+
+ def test_all(self):
+ content = self.getPage('all/')
+ for a in Author.objects.all():
+ self.assertTrue(a.name in content, msg=content)
+ self.assertTrue(a.notes in content, msg=content)
+ for w in Work.objects.all():
+ self.assertTrue(w.name in content)
+ self.assertTrue(w.notes in content, content)
+ for q in Quote.objects.all():
+ self.assertTrue(q.text in content, content)
+ self.assertTrue(q.notes in content, content)
+
+ def test_random(self):
+ seen = {}
+ for q in Quote.objects.all():
+ seen[q.id] = False
+
+ # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
+ permalinkre = re.compile(r'([0-9]+).*Permalink')
+
+ for i in xrange(100):
+ content = self.getPage('random')
+ m = permalinkre.search(content)
+ self.assertIsNotNone(m, content)
+ quoteid = int(m.group(1))
+ self.assertIn(quoteid, seen)
+ seen[quoteid] = True
+ for q in Quote.objects.all():
+ self.assertTrue(seen[q.id])
+
+ def test_views_all_data(self):
+ q = Quote.objects.filter(text__startswith="Quote01")
+ self.assertEqual(q.count(), 1)
+ q = q[0]
+
+ # check the individual quote page
+ quotepage = self.getPage('show/%s/' % q.id)
+ self.assertTrue(q.text in quotepage)
+ self.assertTrue("author_notes" in quotepage)
+ self.assertEqual(quotepage.count("tag_link"), 2)
+
+ # check the work page
+ workpage = self.getPage('work/%s/' % q.work.id)
+ self.assertTrue(q.text in workpage)
+ self.assertTrue("work_notes" in workpage)
+
+ # check the author page
+ authorpage = self.getPage('author/%s/' % q.work.author.id)
+ self.assertTrue(q.text in authorpage)
+ self.assertTrue("author_notes" in authorpage)
+
+ # check the tag page
+ for tag in q.tags.all():
+ tagpage = self.getPage('tag/%s/' % tag.id)
+ self.assertTrue(q.text in tagpage)
+
+ def test_views_minimal_data(self):
+ q = Quote.objects.filter(text__startswith="Quote02")
+ self.assertEqual(q.count(), 1)
+ q = q[0]
+
+ quotepage = self.getPage('show/%s/' % q.id)
+ self.assertTrue(q.text in quotepage)
+ self.assertFalse("author_notes" in quotepage)
+ self.assertEqual(quotepage.count("tag_link"), 0)
+
+ authorpage = self.getPage('author/%s/' % q.work.author.id)
+ self.assertTrue(q.text in authorpage)
+ self.assertFalse("author_notes" in authorpage)
+ self.assertFalse('Quote01, two tags' in authorpage)
+
+ def test_view_author_notes_once(self):
+ # check that on the per-author view, the author notes aren't display
+ # for every quote
+ a = Author.objects.filter(name="Author with notes")
+ self.assertEqual(a.count(), 1)
+ a = a[0]
+
+ authorpage = self.getPage('author/%s/' % a.id)
+ self.assertEqual(authorpage.count("Some notes for the author"), 1)