]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Cleaner Fetcher interface
[paperdoorknob] / paperdoorknob_test.py
1 # paperdoorknob: Print glowfic
2 #
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation, version 3.
6
7
8 import unittest
9 import io
10 import subprocess
11 import requests
12 import requests_cache
13 import paperdoorknob
14 from testing.fakeserver import FakeGlowficServer
15
16 TIMEOUT = 8
17
18
19 class TestFetch(unittest.TestCase):
20 def setUp(self) -> None:
21 self._server = self.enterContext(FakeGlowficServer())
22 self._port = self._server.port()
23
24 def testFetch(self) -> None:
25 with requests.session() as s:
26 paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
27 self.assertEqual(self._server.request_count(), 1)
28 paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
29 self.assertEqual(self._server.request_count(), 2)
30
31 def testFetchCaching(self) -> None:
32 with requests_cache.CachedSession() as s:
33 paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
34 self.assertEqual(self._server.request_count(), 1)
35 paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
36 self.assertEqual(self._server.request_count(), 1)
37
38 def testFetchPersistentCaching(self) -> None:
39 with requests_cache.CachedSession() as s:
40 paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
41 self.assertEqual(self._server.request_count(), 1)
42 with requests_cache.CachedSession() as s:
43 paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT)
44 self.assertEqual(self._server.request_count(), 1)
45
46 def testReplies(self) -> None:
47 with requests.session() as s:
48 replies = paperdoorknob.replies(
49 paperdoorknob.clean(
50 paperdoorknob.fetch(
51 f"http://localhost:{self._port}",
52 s,
53 TIMEOUT)))
54 self.assertEqual([r.text.strip() for r in replies],
55 ["This is glowfic", "You sure?", "Pretty sure."])
56
57 def testFetchErrors(self) -> None:
58 with requests.session() as s:
59 with self.assertRaises(requests.HTTPError):
60 paperdoorknob.fetch(
61 f"http://localhost:{self._port}/not_found", s, TIMEOUT)
62 with self.assertRaises(requests.HTTPError):
63 paperdoorknob.fetch(
64 f"http://localhost:{self._port}/server_error", s, TIMEOUT)
65
66 def testProcess(self) -> None:
67 with requests.session() as s:
68 buf = io.BytesIO()
69 paperdoorknob.process(
70 f"http://localhost:{self._port}",
71 s,
72 TIMEOUT,
73 buf,
74 'pandoc')
75 self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
76 \\begin{document}
77 This is glowfic
78 You \\emph{sure}?
79 Pretty sure.
80 \\end{document}
81 ''')
82
83 def testPDF(self) -> None:
84 with requests.session() as s:
85 with open("test.tex", 'wb') as out:
86 paperdoorknob.process(
87 f"http://localhost:{self._port}", s, TIMEOUT, out, 'pandoc')
88 subprocess.run(['pdflatex', 'test.tex'],
89 stdin=subprocess.DEVNULL, check=True)
90
91
92 if __name__ == '__main__':
93 unittest.main()