]> gitweb.fperrin.net Git - djsite.git/commitdiff
Convert to using pytest
authorFrédéric Perrin <frederic.perrin@resel.fr>
Fri, 4 Nov 2016 19:35:37 +0000 (19:35 +0000)
committerFrédéric Perrin <frederic.perrin@resel.fr>
Fri, 4 Nov 2016 19:35:37 +0000 (19:35 +0000)
quotes/conftest.py [new file with mode: 0644]
quotes/test_pyflakes.py
quotes/test_quotes.py
quotes/test_search.py

diff --git a/quotes/conftest.py b/quotes/conftest.py
new file mode 100644 (file)
index 0000000..9e88dc0
--- /dev/null
@@ -0,0 +1,31 @@
+import pytest
+
+import lxml.etree
+
+class ValidatingClient(object):
+    def __init__(self, client):
+        self.client = client
+    
+    def request(self, url, method, exp_status=200, params={}):
+        if method == 'get':
+            response = self.client.get('/quotes/' + url)
+        elif method == 'post':
+            response = self.client.post('/quotes/' + url, params)
+        else:
+            raise RuntimeError('Unknown method %s for %s' % (method, url))
+        assert response.status_code == exp_status
+        assert response.charset == 'utf-8'
+        document = response.content.decode(response.charset)
+        lxml.etree.fromstring(document)
+        assert '<script>' not in document
+        return document
+
+    def getPage(self, url):
+        return self.request(url, 'get')
+
+    def postPage(self, url, params):
+        return self.request(url, 'post', params=params)
+
+@pytest.fixture
+def c(client):
+    return ValidatingClient(client)
index 535c13a07bc2255883a236d685407374610f7bcb..7082ee032756140e394d10721797d2d9effb6760 100644 (file)
@@ -1,12 +1,12 @@
-from django.test import TestCase
-
+import pytest
 import pyflakes.api
 import os
 
-class PyflakesTest(TestCase):
-    def setUp(self):
+class Test_Pyflakes(object):
+    @pytest.fixture
+    def pyfiles(self):
         topdir = os.path.dirname(os.path.realpath(__file__))
-        self.pythonfiles = []
+        pythonfiles = []
 
         for folder, subfolders, files in os.walk(topdir):
             if 'migrations' in subfolders:
@@ -15,8 +15,9 @@ class PyflakesTest(TestCase):
             for file in files:
                 if not file.endswith('.py'):
                     continue
-                self.pythonfiles += [os.path.join(folder, file)]
+                pythonfiles += [os.path.join(folder, file)]
+        return pythonfiles
     
-    def test_pyflakes(self):
-        for file in self.pythonfiles:
-            self.assertEqual(pyflakes.api.checkPath(file), 0)
+    def test_pyflakes(self, pyfiles):
+        for file in pyfiles:
+            assert pyflakes.api.checkPath(file) == 0
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
index 3a06d3e67b795c5874122abcf67fca393142917f..49b59daec8ec4ea82e927268a94d71a2cfd9c585 100644 (file)
@@ -1,10 +1,6 @@
-from django.test import Client
-
-# Create your tests here.
 import pytest
 
 from .models import Author, Work, Quote
-import lxml.etree
 
 class Test_Search():
     @pytest.fixture(scope='function')
@@ -14,19 +10,12 @@ class Test_Search():
         q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
         return q1
 
-    def postPage(self, url, params, exp_status=200):
-        c = Client()
-        response = c.post('/quotes/' + url, params)
-        assert response.status_code == 200
-        assert response.charset == 'utf-8'
-        document = response.content.decode(response.charset)
-        lxml.etree.fromstring(document)
-        assert '<script>' not in document
-        print document
-        return document
-
     @pytest.mark.django_db
-    def test_search(self, q1):
-        results = self.postPage('search/', {'q': 'Ich'})
+    def test_search(self, q1, c):
+        results = c.postPage('search/', {'q': 'Ich'})
 
         assert 'JFK' in results
+
+    @pytest.mark.django_db
+    def test_emptysearch(self, q1, c):
+        assert c.postPage('search/', {'q': 'something that matches nothing'})