]>
Commit | Line | Data |
---|---|---|
b25a2f90 SW |
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 | |
36ae1d5f | 9 | import io |
07f9b178 | 10 | import subprocess |
6fdb8f01 | 11 | import requests |
b34a368f | 12 | import requests_cache |
b25a2f90 | 13 | import paperdoorknob |
41b11505 | 14 | from testing.fakeserver import FakeGlowficServer |
b25a2f90 | 15 | |
b25a2f90 SW |
16 | TIMEOUT = 8 |
17 | ||
18 | ||
b25a2f90 SW |
19 | class TestFetch(unittest.TestCase): |
20 | def setUp(self) -> None: | |
41b11505 SW |
21 | self._server = self.enterContext(FakeGlowficServer()) |
22 | self._port = self._server.port() | |
b25a2f90 SW |
23 | |
24 | def testFetch(self) -> None: | |
e138a9b4 | 25 | with requests.session() as s: |
41b11505 SW |
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) | |
b25a2f90 | 30 | |
b34a368f SW |
31 | def testFetchCaching(self) -> None: |
32 | with requests_cache.CachedSession() as s: | |
41b11505 SW |
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) | |
b34a368f | 37 | |
de7251fc SW |
38 | def testFetchPersistentCaching(self) -> None: |
39 | with requests_cache.CachedSession() as s: | |
41b11505 SW |
40 | paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT) |
41 | self.assertEqual(self._server.request_count(), 1) | |
de7251fc | 42 | with requests_cache.CachedSession() as s: |
41b11505 SW |
43 | paperdoorknob.fetch(f"http://localhost:{self._port}", s, TIMEOUT) |
44 | self.assertEqual(self._server.request_count(), 1) | |
de7251fc | 45 | |
a2d42468 | 46 | def testReplies(self) -> None: |
136277e3 | 47 | with requests.session() as s: |
a2d42468 SW |
48 | replies = paperdoorknob.replies( |
49 | paperdoorknob.clean( | |
50 | paperdoorknob.fetch( | |
41b11505 | 51 | f"http://localhost:{self._port}", |
a2d42468 SW |
52 | s, |
53 | TIMEOUT))) | |
47cfa3cd | 54 | self.assertEqual([r.text.strip() for r in replies], |
55958ec0 | 55 | ["This is glowfic", "You sure?", "Pretty sure."]) |
136277e3 | 56 | |
6fdb8f01 | 57 | def testFetchErrors(self) -> None: |
e138a9b4 SW |
58 | with requests.session() as s: |
59 | with self.assertRaises(requests.HTTPError): | |
60 | paperdoorknob.fetch( | |
41b11505 | 61 | f"http://localhost:{self._port}/not_found", s, TIMEOUT) |
e138a9b4 SW |
62 | with self.assertRaises(requests.HTTPError): |
63 | paperdoorknob.fetch( | |
41b11505 | 64 | f"http://localhost:{self._port}/server_error", s, TIMEOUT) |
6fdb8f01 | 65 | |
36ae1d5f SW |
66 | def testProcess(self) -> None: |
67 | with requests.session() as s: | |
68 | buf = io.BytesIO() | |
69 | paperdoorknob.process( | |
41b11505 | 70 | f"http://localhost:{self._port}", |
36ae1d5f SW |
71 | s, |
72 | TIMEOUT, | |
73 | buf, | |
74 | 'pandoc') | |
07f9b178 SW |
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( | |
41b11505 | 87 | f"http://localhost:{self._port}", s, TIMEOUT, out, 'pandoc') |
07f9b178 SW |
88 | subprocess.run(['pdflatex', 'test.tex'], |
89 | stdin=subprocess.DEVNULL, check=True) | |
36ae1d5f | 90 | |
b25a2f90 SW |
91 | |
92 | if __name__ == '__main__': | |
93 | unittest.main() |