X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=quotes%2Ftests.py;h=36dcc1d1cd1aa3d7cd84f40ad40d0a3e1571b424;hb=0b43566359216637a28247d7f9ab4121931843ed;hp=556e9252c3377477911b077da4e64315b8f067b9;hpb=af3f857eea25b939f927832741f377e4de401f9c;p=djsite.git diff --git a/quotes/tests.py b/quotes/tests.py index 556e925..36dcc1d 100644 --- a/quotes/tests.py +++ b/quotes/tests.py @@ -1,68 +1,153 @@ -import sys from django.test import TestCase, Client # Create your tests here. -from .models import Author, Tag, Quote +from .models import QuoteTag, Author, Work, Quote +import re +import lxml.etree class QuoteTest(TestCase): def setUp(self): a1 = Author.objects.create(name="JFK") - q1 = Quote.objects.create(text="Ich bin...", author=a1) + w1 = Work.objects.create(name="Berlin speech", author=a1) + q1 = Quote.objects.create(text="

Ich bin...

", work=w1) + self.q1 = q1 def test_one(self): - q = Quote.objects.filter(text__startswith="Ich") + q = Quote.objects.filter(text__startswith="

Ich") self.assertEqual(q.count(), 1) q = q[0] - self.assertEqual(q.author.name, "JFK") - + self.assertEqual(q, self.q1) + 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') + document = response.content.decode(response.charset) + try: + lxml.etree.fromstring(document) + except lxml.etree.XMLSyntaxError as e: + self.assertTrue(False, 'Errors in page at %s: %s' % (url, e)) + self.assertFalse('") + 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 = QuoteTag.objects.create(tag="tag01-1") + t2 = QuoteTag.objects.create(tag="tag01-2") q1.tags.add(t1, t2) a2 = Author.objects.create(name="Author without notes") - q2= Quote.objects.create(text="Quote02, no tags", author=a2) + w2 = Work.objects.create(name="Work without notes", author=a2) + q2 = Quote.objects.create(text="

Quote02, no tags

", work=w2) + self.assertSequenceEqual(q2.tags.all(), []) + + def test_all(self): + content = self.getPage('all/') + for a in Author.objects.all(): + self.assertIn(a.name, content) + self.assertIn(a.notes, content) + for w in Work.objects.all(): + self.assertIn(w.name, content) + self.assertIn(w.notes, content) + for q in Quote.objects.all(): + self.assertIn(q.text, content) + self.assertIn(q.notes, content) + + def test_random(self): + seen = {} + for q in Quote.objects.all(): + seen[q.id] = False + + # Permalink + permalinkre = re.compile(r'([0-9]+).*Permalink') + + for i in range(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 = Quote.objects.filter(text__startswith="

Quote01") + self.assertEqual(q.count(), 1, "Couldn't find Quote01 after insertion") q = q[0] - c = Client() - response = c.get('/quotes/show/%s/' % q.id) - self.assertEqual(response.status_code, 200) - self.assertTrue('Quote01, two tags' in response.content) - self.assertTrue("author_notes" in response.content) - self.assertEqual(response.content.count("tag_link"), 2) + # check the individual quote page; each of the note type is displayed + quotepage = self.getPage('show/%s/' % q.id) + self.assertIn(q.text, quotepage) + self.assertIn("author_notes", quotepage) + self.assertIn("work_notes", quotepage) + self.assertIn("tag_link", quotepage) + self.assertEqual(quotepage.count("tag_link"), 2) - response = c.get('/quotes/author/%s/' % q.author.id) - self.assertEqual(response.status_code, 200) - self.assertTrue('Quote01, two tags' in response.content) - self.assertTrue("author_notes" in response.content) + # check the work page; each of the note type is displayed, and + # the work notes are shown only once + workpage = self.getPage('work/%s/' % q.work.id) + self.assertIn(q.text, workpage) + self.assertIn("author_notes", workpage) + self.assertIn("work_notes", workpage) + self.assertEqual(workpage.count("work_notes"), 1) + self.assertIn("tag_link", workpage) + + # check the author page; each of the note type is displayed, and + # the author notes are shown only once + authorpage = self.getPage('author/%s/' % q.work.author.id) + self.assertIn(q.text, authorpage) + self.assertIn("author_notes", authorpage) + self.assertEqual(authorpage.count("author_notes"), 1) + self.assertIn("work_notes", authorpage) + self.assertIn("tag_link", authorpage) + # check the tag page for tag in q.tags.all(): - response = c.get('/quotes/tag/%s/' % tag.id) - self.assertEqual(response.status_code, 200) - self.assertTrue('Quote01, two tags' in response.content) + tagpage = self.getPage('tag/%s/' % tag.id) + self.assertIn(q.text, tagpage) def test_views_minimal_data(self): - q = Quote.objects.filter(text__startswith="Quote02") - self.assertEqual(q.count(), 1) + q = Quote.objects.filter(text__startswith="

Quote02") + self.assertEqual(q.count(), 1, + "Couldn't find Quote02 after insertion") q = q[0] - c = Client() - response = c.get('/quotes/show/%s/' % q.id) - self.assertEqual(response.status_code, 200) - self.assertTrue('Quote02' in response.content) - self.assertFalse("author_notes" in response.content) - self.assertEqual(response.content.count("tag_link"), 0) + quotepage = self.getPage('show/%s/' % q.id) + self.assertIn(q.text, quotepage) + self.assertNotIn("author_notes", quotepage) + self.assertNotIn("work_notes", quotepage) + self.assertNotIn("tag_link", quotepage) - response = c.get('/quotes/author/%s/' % q.author.id) - self.assertEqual(response.status_code, 200) - self.assertFalse('Quote01, two tags' in response.content) - self.assertTrue('Quote02' in response.content) + workpage = self.getPage('work/%s/' % q.work.id) + self.assertIn(q.text, workpage) + self.assertNotIn("author_notes", quotepage) + self.assertNotIn("work_notes", quotepage) + self.assertNotIn("tag_link", quotepage) + + authorpage = self.getPage('author/%s/' % q.work.author.id) + self.assertIn(q.text, authorpage) + self.assertNotIn("author_notes", authorpage) + self.assertNotIn("work_notes", authorpage) + self.assertNotIn("tag_link", authorpage) + self.assertNotIn('Quote01, two tags', 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)