]>
git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
c8968108472b675bb127bd29a003064de83650c8
1 # paperdoorknob: Print glowfic
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.
14 from testing
.fakeserver
import FakeGlowficServer
19 class TestFetch(unittest
.TestCase
):
20 def setUp(self
) -> None:
21 self
._server
= self
.enterContext(FakeGlowficServer())
22 self
._port
= self
._server
.port()
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)
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)
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)
46 def testReplies(self
) -> None:
47 with requests
.session() as s
:
48 replies
= paperdoorknob
.replies(
51 f
"http://localhost:{self._port}",
54 self
.assertEqual([r
.text
.strip() for r
in replies
],
55 ["This is glowfic", "You sure?", "Pretty sure."])
57 def testFetchErrors(self
) -> None:
58 with requests
.session() as s
:
59 with self
.assertRaises(requests
.HTTPError
):
61 f
"http://localhost:{self._port}/not_found", s
, TIMEOUT
)
62 with self
.assertRaises(requests
.HTTPError
):
64 f
"http://localhost:{self._port}/server_error", s
, TIMEOUT
)
66 def testProcess(self
) -> None:
67 with requests
.session() as s
:
69 paperdoorknob
.process(
70 f
"http://localhost:{self._port}",
75 self
.assertEqual(buf
.getvalue(), b
'''\\documentclass{article}
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)
92 if __name__
== '__main__':