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