]> git.scottworley.com Git - paperdoorknob/blame_incremental - paperdoorknob_test.py
FakeFetcher for faster tests
[paperdoorknob] / paperdoorknob_test.py
... / ...
CommitLineData
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
8from abc import ABC, abstractmethod
9import unittest
10import io
11import subprocess
12
13import paperdoorknob
14
15from testing.fakeserver import FakeGlowficServer
16from fetch import DirectFetcher, FakeFetcher, Fetcher
17from spec import Spec
18from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
19
20TIMEOUT = 8
21
22
23class BaseTestProcess(ABC):
24
25 @abstractmethod
26 def url(self) -> str:
27 raise NotImplementedError()
28
29 @abstractmethod
30 def fetcher(self) -> Fetcher:
31 raise NotImplementedError()
32
33 def testReplies(self) -> None:
34 replies = paperdoorknob.replies(
35 paperdoorknob.clean(
36 paperdoorknob.parse(
37 self.fetcher().fetch(
38 self.url()))))
39 assert [r.text.strip() for r in replies] == [
40 "This is glowfic",
41 "You sure?",
42 "Pretty sure."]
43
44 def testProcess(self) -> None:
45 buf = io.BytesIO()
46 spec = Spec(
47 self.url(),
48 self.fetcher(),
49 PandocTexifier('pandoc'),
50 buf)
51 paperdoorknob.process(spec)
52 assert buf.getvalue() == b'''\\documentclass{article}
53\\begin{document}
54This is glowfic
55You \\emph{sure}?
56Pretty sure.
57\\end{document}
58'''
59
60 def testDirectTexifier(self) -> None:
61 texifier = VerifyingTexifier(
62 PandocTexifier('pandoc'), DirectTexifier())
63 buf = io.BytesIO()
64 spec = Spec(self.url(), self.fetcher(), texifier, buf)
65 paperdoorknob.process(spec)
66
67 def testPDF(self) -> None:
68 with open("test.tex", 'wb') as out:
69 spec = Spec(
70 self.url(),
71 self.fetcher(),
72 PandocTexifier('pandoc'),
73 out)
74 paperdoorknob.process(spec)
75 subprocess.run(['pdflatex', 'test.tex'],
76 stdin=subprocess.DEVNULL, check=True)
77
78
79class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
80
81 def setUp(self) -> None:
82 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
83 self._server = self.enterContext(FakeGlowficServer())
84 self._port = self._server.port()
85
86 def url(self) -> str:
87 return f"http://localhost:{self._port}"
88
89 def fetcher(self) -> Fetcher:
90 return self._fetcher
91
92
93class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
94
95 def url(self) -> str:
96 return 'fic'
97
98 def fetcher(self) -> Fetcher:
99 with open('testdata/this-is-glowfic.html', 'rb') as f:
100 return FakeFetcher({'fic': f.read(9999)})
101
102
103if __name__ == '__main__':
104 unittest.main()