]> git.scottworley.com Git - paperdoorknob/blobdiff - paperdoorknob_test.py
Rename: fetch → parse
[paperdoorknob] / paperdoorknob_test.py
index c8968108472b675bb127bd29a003064de83650c8..9676e286aaae11eb1959cb5868af85e6f3681d25 100644 (file)
@@ -8,70 +8,32 @@
 import unittest
 import io
 import subprocess
 import unittest
 import io
 import subprocess
-import requests
-import requests_cache
 import paperdoorknob
 from testing.fakeserver import FakeGlowficServer
 import paperdoorknob
 from testing.fakeserver import FakeGlowficServer
+from fetch import DirectFetcher
 
 TIMEOUT = 8
 
 
 
 TIMEOUT = 8
 
 
-class TestFetch(unittest.TestCase):
+class TestPaperDoorknob(unittest.TestCase):
     def setUp(self) -> None:
         self._server = self.enterContext(FakeGlowficServer())
         self._port = self._server.port()
 
     def setUp(self) -> None:
         self._server = self.enterContext(FakeGlowficServer())
         self._port = self._server.port()
 
-    def testFetch(self) -> None:
-        with requests.session() as s:
-            paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
-            self.assertEqual(self._server.request_count(), 1)
-            paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
-            self.assertEqual(self._server.request_count(), 2)
-
-    def testFetchCaching(self) -> None:
-        with requests_cache.CachedSession() as s:
-            paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
-            self.assertEqual(self._server.request_count(), 1)
-            paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
-            self.assertEqual(self._server.request_count(), 1)
-
-    def testFetchPersistentCaching(self) -> None:
-        with requests_cache.CachedSession() as s:
-            paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
-            self.assertEqual(self._server.request_count(), 1)
-        with requests_cache.CachedSession() as s:
-            paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
-            self.assertEqual(self._server.request_count(), 1)
-
     def testReplies(self) -> None:
     def testReplies(self) -> None:
-        with requests.session() as s:
+        with DirectFetcher(TIMEOUT) as f:
             replies = paperdoorknob.replies(
                 paperdoorknob.clean(
             replies = paperdoorknob.replies(
                 paperdoorknob.clean(
-                    paperdoorknob.fetch(
-                        f"http://localhost:{self._port}",
-                        s,
-                        TIMEOUT)))
+                    paperdoorknob.parse(
+                        f.fetch(f"http://localhost:{self._port}"))))
             self.assertEqual([r.text.strip() for r in replies],
                              ["This is glowfic", "You sure?", "Pretty sure."])
 
             self.assertEqual([r.text.strip() for r in replies],
                              ["This is glowfic", "You sure?", "Pretty sure."])
 
-    def testFetchErrors(self) -> None:
-        with requests.session() as s:
-            with self.assertRaises(requests.HTTPError):
-                paperdoorknob.fetch(
-                    f"http://localhost:{self._port}/not_found", s, TIMEOUT)
-            with self.assertRaises(requests.HTTPError):
-                paperdoorknob.fetch(
-                    f"http://localhost:{self._port}/server_error", s, TIMEOUT)
-
     def testProcess(self) -> None:
     def testProcess(self) -> None:
-        with requests.session() as s:
+        with DirectFetcher(TIMEOUT) as f:
             buf = io.BytesIO()
             paperdoorknob.process(
             buf = io.BytesIO()
             paperdoorknob.process(
-                f"http://localhost:{self._port}",
-                s,
-                TIMEOUT,
-                buf,
-                'pandoc')
+                f"http://localhost:{self._port}", f, buf, 'pandoc')
             self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
 \\begin{document}
 This is glowfic
             self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
 \\begin{document}
 This is glowfic
@@ -81,10 +43,10 @@ Pretty sure.
 ''')
 
     def testPDF(self) -> None:
 ''')
 
     def testPDF(self) -> None:
-        with requests.session() as s:
+        with DirectFetcher(TIMEOUT) as f:
             with open("test.tex", 'wb') as out:
                 paperdoorknob.process(
             with open("test.tex", 'wb') as out:
                 paperdoorknob.process(
-                    f"http://localhost:{self._port}", s, TIMEOUT, out, 'pandoc')
+                    f"http://localhost:{self._port}", f, out, 'pandoc')
             subprocess.run(['pdflatex', 'test.tex'],
                            stdin=subprocess.DEVNULL, check=True)
 
             subprocess.run(['pdflatex', 'test.tex'],
                            stdin=subprocess.DEVNULL, check=True)