]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Fix flaky test: Increment request count before responding
[paperdoorknob] / paperdoorknob_test.py
CommitLineData
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
38621839 8from abc import ABC, abstractmethod
b25a2f90 9import unittest
36ae1d5f 10import io
07f9b178 11import subprocess
23f31879 12
b25a2f90 13import paperdoorknob
23f31879 14
41b11505 15from testing.fakeserver import FakeGlowficServer
8be20b9d 16from domfilter import ApplyDOMFilters
38621839 17from fetch import DirectFetcher, FakeFetcher, Fetcher
d2a41ff4 18from glowfic import ContentOnlyLayout, BelowIconLayout
4b7d905e 19from images import FakeImageStore
23f31879 20from spec import Spec
f1dec720 21from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
b25a2f90 22
b25a2f90
SW
23TIMEOUT = 8
24
25
38621839
SW
26class BaseTestProcess(ABC):
27
28 @abstractmethod
29 def url(self) -> str:
30 raise NotImplementedError()
31
32 @abstractmethod
33 def fetcher(self) -> Fetcher:
34 raise NotImplementedError()
b25a2f90 35
36ae1d5f 36 def testProcess(self) -> None:
38621839
SW
37 buf = io.BytesIO()
38 spec = Spec(
39 self.url(),
40 self.fetcher(),
4b7d905e 41 FakeImageStore(),
929db576 42 lambda x: x,
8be20b9d 43 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
d2a41ff4 44 ContentOnlyLayout(PandocTexifier('pandoc')),
e10b5b6f 45 'margin=20mm',
38621839
SW
46 buf)
47 paperdoorknob.process(spec)
48 assert buf.getvalue() == b'''\\documentclass{article}
d2a41ff4 49\\usepackage{graphicx}
8d583d5b 50\\usepackage{wrapfig}
e10b5b6f 51\\usepackage[margin=20mm]{geometry}
07f9b178
SW
52\\begin{document}
53This is glowfic
54You \\emph{sure}?
55Pretty sure.
56\\end{document}
38621839 57'''
07f9b178 58
f1dec720
SW
59 def testDirectTexifier(self) -> None:
60 texifier = VerifyingTexifier(
61 PandocTexifier('pandoc'), DirectTexifier())
38621839 62 buf = io.BytesIO()
8be20b9d
SW
63 spec = Spec(
64 self.url(),
65 self.fetcher(),
4b7d905e 66 FakeImageStore(),
8be20b9d
SW
67 lambda x: x,
68 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
d2a41ff4 69 ContentOnlyLayout(texifier),
e10b5b6f 70 None,
8be20b9d 71 buf)
38621839 72 paperdoorknob.process(spec)
f1dec720 73
07f9b178 74 def testPDF(self) -> None:
38621839
SW
75 with open("test.tex", 'wb') as out:
76 spec = Spec(
77 self.url(),
78 self.fetcher(),
4b7d905e 79 FakeImageStore(),
929db576 80 lambda x: x,
8be20b9d 81 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
c62e8d40 82 BelowIconLayout(PandocTexifier('pandoc'), 20),
e10b5b6f 83 None,
38621839
SW
84 out)
85 paperdoorknob.process(spec)
23f31879
SW
86 subprocess.run(['pdflatex', 'test.tex'],
87 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 88
b25a2f90 89
38621839
SW
90class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
91
92 def setUp(self) -> None:
93 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
94 self._server = self.enterContext(FakeGlowficServer())
95 self._port = self._server.port()
96
97 def url(self) -> str:
98 return f"http://localhost:{self._port}"
99
100 def fetcher(self) -> Fetcher:
101 return self._fetcher
102
103
104class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
105
106 def url(self) -> str:
107 return 'fic'
108
109 def fetcher(self) -> Fetcher:
110 with open('testdata/this-is-glowfic.html', 'rb') as f:
111 return FakeFetcher({'fic': f.read(9999)})
112
113
b25a2f90
SW
114if __name__ == '__main__':
115 unittest.main()