]> git.scottworley.com Git - paperdoorknob/blame - glowfic_test.py
Fetch the non-flat view to get the next-thread link
[paperdoorknob] / glowfic_test.py
CommitLineData
e6adf6ce
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
19ed28f3 8from sys import stderr
e6adf6ce
SW
9import unittest
10
19ed28f3 11from fetch import FakeFetcher
aa060d9b 12from images import FakeImageStore
94027099 13from glowfic import makeChunk, Thread
19ed28f3 14from spec import Spec
62043b2b 15from texify import PandocTexifier
e6adf6ce
SW
16
17
19ed28f3
SW
18def spec_for_testing(html: bytes) -> Spec:
19 return Spec('test',
53f396b4 20 FakeFetcher({'test': html, 'test?view=flat': html}),
19ed28f3
SW
21 FakeImageStore(),
22 lambda x: x,
23 lambda x: None,
24 PandocTexifier('pandoc'),
25 lambda x: x,
26 20,
27 b'',
28 None,
29 stderr.buffer,
30 lambda x: None)
31
32
e6adf6ce
SW
33class TestSplit(unittest.TestCase):
34
35 def testSplit1(self) -> None:
19ed28f3 36 t = Thread(spec_for_testing(b'''
e6adf6ce
SW
37 <html><body><div class="post-container post-post">
38 The "post"
19ed28f3 39 </div></body></html>'''))
94027099 40 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
e6adf6ce
SW
41 [['The "post"']])
42
43 def testSplit2(self) -> None:
19ed28f3 44 t = Thread(spec_for_testing(b'''
e6adf6ce
SW
45 <html><body>
46 <div class="post-container post-post">The "post"</div>
47 <div class="flat-post-replies">
48 <div class="post-container post-reply">The "reply"</div>
49 </div>
19ed28f3 50 </body></html>'''))
94027099 51 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
e6adf6ce
SW
52 [['The "post"'], ['The "reply"']])
53
54 def testSplit3(self) -> None:
19ed28f3 55 t = Thread(spec_for_testing(b'''
e6adf6ce
SW
56 <html><body>
57 <div class="post-container post-post">The "post"</div>
58 <div class="flat-post-replies">
59 <div class="post-container post-reply">1st reply</div>
60 <div class="post-container post-reply">2nd reply</div>
61 </div>
19ed28f3 62 </body></html>'''))
94027099 63 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
e6adf6ce
SW
64 [['The "post"'], ['1st reply'], ['2nd reply']])
65
66
aa060d9b
SW
67class TestMakeChunk(unittest.TestCase):
68
69 def testEmptyContent(self) -> None:
70 with open('testdata/empty-content.html', 'rb') as f:
19ed28f3 71 t = Thread(spec_for_testing(f.read((9999))))
94027099 72 c = makeChunk(next(iter(t.chunkDOMs())), FakeImageStore())
aa060d9b
SW
73 self.assertEqual(
74 c.icon,
75 'stored:https://d1anwqy6ci9o1i.cloudfront.net/' +
76 'users%2F366%2Ficons%2Fxqmypqvflgdy28aorw9ml_shock.png')
37c47bc2
SW
77 assert c.character
78 assert c.screen_name
79 assert c.author
80 self.assertEqual(list(c.character.stripped_strings), ['Keltham'])
81 self.assertEqual(
82 list(c.screen_name.stripped_strings), ['lawful chaotic'])
83 self.assertEqual(list(c.author.stripped_strings), ['Iarwain'])
aa060d9b
SW
84 self.assertEqual(str(c.content),
85 '<div class="post-content"><p></p></div>')
86
62043b2b
SW
87 self.assertEqual(
88 PandocTexifier("pandoc").texify(c.character), b'{Keltham}\n')
89
aa060d9b 90
21e82200
SW
91class TestThread(unittest.TestCase):
92
93 def testTitle(self) -> None:
19ed28f3 94 t = Thread(spec_for_testing(b'''
21e82200
SW
95 <html><body>
96 <div class="content-header">
97 <span id="post-title">
98 <a href="/posts/1234">Teh Story!</a>
99 </span>
100 </div>
101 <div class="post-container post-post">The "post"</div>
19ed28f3 102 </body></html>'''))
21e82200
SW
103 self.assertEqual(t.title(), 'Teh Story!')
104
53f396b4
SW
105 def testNextThread(self) -> None:
106 t = Thread(spec_for_testing(b'''
107 <html><body>
108 <div class="post-navheader">
109 <a class="view-button-link" href="/posts/4567"><div class="view-button">Next Post &raquo;</div>
110 </a><a class="view-button-link" href="/posts/4321"><div class="view-button float-none">&laquo; Previous Post</div>
111 </a>
112 </div>
113 <div class="post-container post-post">The "post"</div>
114 </body></html>'''))
115 self.assertEqual(t.next_thread(), '/posts/4567')
116
21e82200 117
e6adf6ce
SW
118if __name__ == '__main__':
119 unittest.main()