]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
FakeFetcher for faster tests
[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
38621839 16from fetch import DirectFetcher, FakeFetcher, Fetcher
23f31879 17from spec import Spec
f1dec720 18from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
b25a2f90 19
b25a2f90
SW
20TIMEOUT = 8
21
22
38621839
SW
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()
b25a2f90 32
a2d42468 33 def testReplies(self) -> None:
38621839
SW
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."]
136277e3 43
36ae1d5f 44 def testProcess(self) -> None:
38621839
SW
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}
07f9b178
SW
53\\begin{document}
54This is glowfic
55You \\emph{sure}?
56Pretty sure.
57\\end{document}
38621839 58'''
07f9b178 59
f1dec720
SW
60 def testDirectTexifier(self) -> None:
61 texifier = VerifyingTexifier(
62 PandocTexifier('pandoc'), DirectTexifier())
38621839
SW
63 buf = io.BytesIO()
64 spec = Spec(self.url(), self.fetcher(), texifier, buf)
65 paperdoorknob.process(spec)
f1dec720 66
07f9b178 67 def testPDF(self) -> None:
38621839
SW
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)
23f31879
SW
75 subprocess.run(['pdflatex', 'test.tex'],
76 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 77
b25a2f90 78
38621839
SW
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
b25a2f90
SW
103if __name__ == '__main__':
104 unittest.main()