-from django.test import TestCase, Client
+# -*- encoding: utf-8 -*-
+
+import pytest
-# Create your tests here.
from .models import QuoteTag, Author, Work, Quote
import re
-import lxml.etree
-class QuoteTest(TestCase):
- def setUp(self):
+class Test_QuoteTest():
+ @pytest.fixture(scope='function')
+ def q1(self, db):
a1 = Author.objects.create(name="JFK")
w1 = Work.objects.create(name="Berlin speech", author=a1)
q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
- self.q1 = q1
+ return q1
- def test_one(self):
+ @pytest.mark.django_db
+ def test_one(self, q1):
q = Quote.objects.filter(text__startswith="<p>Ich")
- self.assertEqual(q.count(), 1)
+ assert q.count() == 1
q = q[0]
- 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('<script>' in document)
- return document
-
- def setUp(self):
+ assert q == q1
+ assert q.work.author.name == 'JFK'
+
+class Test_Views():
+ @pytest.fixture(autouse=True, scope='function')
+ def quotes(self, db):
a1 = Author.objects.create(name="Author with notes",
notes="<script>Some notes for the author</script>")
w1 = Work.objects.create(name="Context with some notes",
a2 = Author.objects.create(name="Author without notes")
w2 = Work.objects.create(name="Work without notes", author=a2)
q2 = Quote.objects.create(text="<p>Quote02, no tags</p>", work=w2)
- self.assertSequenceEqual(q2.tags.all(), [])
+ assert q2.tags.all().count() == 0
- def test_all(self):
- content = self.getPage('all/')
+ def test_all(self, c):
+ content = c.getPage('all/')
for a in Author.objects.all():
- self.assertIn(a.name, content)
- self.assertIn(a.notes, content)
+ assert a.name in content
+ assert a.notes in content
for w in Work.objects.all():
- self.assertIn(w.name, content)
- self.assertIn(w.notes, content)
+ assert w.name in content
+ assert w.notes in content
for q in Quote.objects.all():
- self.assertIn(q.text, content)
- self.assertIn(q.notes, content)
+ assert q.text in content
+ assert q.notes in content
- def test_random(self):
+ c.checkAllLinks('all/', content)
+
+ @pytest.mark.slow
+ def test_random(self, c):
seen = {}
for q in Quote.objects.all():
seen[q.id] = False
permalinkre = re.compile(r'([0-9]+).*Permalink')
for i in range(100):
- content = self.getPage('random')
+ content = c.getPage('random')
m = permalinkre.search(content)
- self.assertIsNotNone(m, content)
+ assert m is not None, "Did not find the permalink in: "+content
quoteid = int(m.group(1))
- self.assertIn(quoteid, seen)
+ assert quoteid in seen
seen[quoteid] = True
for q in Quote.objects.all():
- self.assertTrue(seen[q.id])
+ assert seen[q.id]
- def test_views_all_data(self):
+ def test_views_all_data(self, c):
q = Quote.objects.filter(text__startswith="<p>Quote01")
- self.assertEqual(q.count(), 1, "Couldn't find Quote01 after insertion")
+ assert q.count() == 1, "Couldn't find Quote01 after insertion"
q = q[0]
# 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)
+ quotepage = c.getPage('show/%s/' % q.id)
+ assert q.text in quotepage
+ assert "author_notes" in quotepage
+ assert "work_notes" in quotepage
+ assert "tag_link" in quotepage
+ assert quotepage.count("tag_link") == 2
# 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)
+ workpage = c.getPage('work/%s/' % q.work.id)
+ assert q.text in workpage
+ assert "author_notes" in workpage
+ assert "work_notes" in workpage
+ assert workpage.count("work_notes") == 1
+ assert "tag_link" in 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)
+ authorpage = c.getPage('author/%s/' % q.work.author.id)
+ assert q.text in authorpage
+ assert "author_notes" in authorpage
+ assert authorpage.count("author_notes") == 1
+ assert "work_notes" in authorpage
+ assert "tag_link" in authorpage
# check the tag page
for tag in q.tags.all():
- tagpage = self.getPage('tag/%s/' % tag.id)
- self.assertIn(q.text, tagpage)
+ tagpage = c.getPage('tag/%s/' % tag.id)
+ assert q.text in tagpage
- def test_views_minimal_data(self):
+ def test_views_minimal_data(self, c):
q = Quote.objects.filter(text__startswith="<p>Quote02")
- self.assertEqual(q.count(), 1,
- "Couldn't find Quote02 after insertion")
+ assert q.count() == 1, \
+ "Couldn't find Quote02 after insertion"
q = q[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)
-
- 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):
+ quotepage = c.getPage('show/%s/' % q.id)
+ assert q.text in quotepage
+ assert "author_notes" not in quotepage
+ assert "work_notes" not in quotepage
+ assert "tag_link" not in quotepage
+
+ workpage = c.getPage('work/%s/' % q.work.id)
+ assert q.text in workpage
+ assert "author_notes" not in quotepage
+ assert "work_notes" not in quotepage
+ assert "tag_link" not in quotepage
+
+ authorpage = c.getPage('author/%s/' % q.work.author.id)
+ assert q.text in authorpage
+ assert "author_notes" not in authorpage
+ assert "work_notes" not in authorpage
+ assert "tag_link" not in authorpage
+ assert 'Quote01, two tags' not in authorpage
+
+ def test_view_author_notes_once(self, c):
# 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)
+ assert a.count() == 1
a = a[0]
- authorpage = self.getPage('author/%s/' % a.id)
- self.assertEqual(authorpage.count("Some notes for the author"), 1)
+ authorpage = c.getPage('author/%s/' % a.id)
+ assert authorpage.count("Some notes for the author") == 1
+
+class Test_Unicode():
+ def test_unicode(self, db):
+ a = Author.objects.create(name="ê è “smart ''quotes,”")
+ w = Work.objects.create(name="¿who?If you’re creati'' ng a", author=a)
+ q = Quote.objects.create(text="µqwer If you’r'' ¨ë ẽ « or » e ", work=w)
+ t = QuoteTag.objects.create(tag=", “s tag 'a’ ß")
+
+ for t in QuoteTag.objects.all():
+ print t
+ for w in Work.objects.all():
+ print w
+ for a in Author.objects.all():
+ print a
+ for q in Quote.objects.all():
+ print q