]> gitweb.fperrin.net Git - djsite.git/blobdiff - quotes/test_quotes.py
Convert to using pytest
[djsite.git] / quotes / test_quotes.py
index 250ff3005aad3ba84541d49891dacb8e20a33093..ac53979f936f3214cf15adacdcc13c30e682d4e3 100644 (file)
@@ -1,11 +1,7 @@
-from django.test import TestCase, Client
-
-# Create your tests here.
 import pytest
 
 from .models import QuoteTag, Author, Work, Quote
 import re
-import lxml.etree
 
 class Test_QuoteTest():
     @pytest.fixture(scope='function')
@@ -23,21 +19,9 @@ class Test_QuoteTest():
         assert q == q1
         assert 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):
+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",
@@ -53,21 +37,22 @@ class ViewsTest(TestCase):
         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 len(q2.tags.all()) == 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):
+    @pytest.mark.slow
+    def test_random(self, c):
         seen = {}
         for q in Quote.objects.all():
             seen[q.id] = False
@@ -76,82 +61,82 @@ class ViewsTest(TestCase):
         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