forked from E-P-T/Homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
337 lines (304 loc) · 14.7 KB
/
tests.py
File metadata and controls
337 lines (304 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import unittest
import rss_reader
import sys
import datetime
import bs4
class TestParseArgs(unittest.TestCase):
def setUp(self):
self.fake_exit_status = None
self.orig_sys_exit = sys.exit
sys.exit = self._fake_sys_exit
def _fake_sys_exit(self, status=0):
self.fake_exit_status = status
def tearDown(self):
sys.exit = self.orig_sys_exit
def test_empty(self):
rss_reader.parse_args([])
self.assertNotEqual(self.fake_exit_status, None)
def test_only_url(self):
args = rss_reader.parse_args(['mockurl'])
self.assertFalse(args.json)
self.assertFalse(args.verbose)
self.assertEqual(args.source, 'mockurl')
self.assertEqual(args.limit, None)
def test_version(self):
rss_reader.parse_args(['--version'])
self.assertNotEqual(self.fake_exit_status, None)
def test_json(self):
args = rss_reader.parse_args(['--json', 'mockurl2'])
self.assertTrue(args.json)
self.assertFalse(args.verbose)
self.assertEqual(args.source, 'mockurl2')
self.assertEqual(args.limit, None)
def test_verbose(self):
args = rss_reader.parse_args(['--verbose', 'mockurl3'])
self.assertFalse(args.json)
self.assertTrue(args.verbose)
self.assertEqual(args.source, 'mockurl3')
self.assertEqual(args.limit, None)
def test_limit(self):
args = rss_reader.parse_args(['--limit', '10', 'mockurl4'])
self.assertFalse(args.json)
self.assertFalse(args.verbose)
self.assertEqual(args.source, 'mockurl4')
self.assertEqual(args.limit, 10)
class TestParseFeed(unittest.TestCase):
SAMPLE_2_0 = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Weblog Editor 2.0</generator>
<managingEditor>editor@example.com</managingEditor>
<webMaster>webmaster@example.com</webMaster>
<item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description><img src="https://example.com/images/logo.png">How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
</item>
<item>
<description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm">partial eclipse of the Sun</a> on Saturday, May 31st.</description>
<pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
</item>
<item>
<title>The Engine That Does More</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
<description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description>
<pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
</item>
<item>
<title>Astronauts' Dirty Laundry</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
<description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.</description>
<pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
</item>
</channel>
</rss>"""
def test_sample20(self):
content = self.SAMPLE_2_0
feed = rss_reader.parse_feed(content)
self.assertEqual(feed['title'], 'Liftoff News')
self.assertEqual(feed['link'], 'http://liftoff.msfc.nasa.gov/')
self.assertEqual(feed['description'], 'Liftoff to Space Exploration.')
self.assertEqual(len(feed['items']), 4)
self.assertEqual(feed['items'][0]['title'], 'Star City')
self.assertEqual(feed['items'][0]['link'], 'http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp')
self.assertEqual(feed['items'][0]['pubDate'], datetime.datetime(2003, 6, 3, 9, 39, 21, tzinfo=datetime.timezone.utc))
self.assertEqual(feed['items'][0]['description'], '[image 2]How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia\'s Star City[3].')
self.assertEqual(len(feed['items'][0]['links']), 3)
self.assertEqual(feed['items'][0]['links'][0], ('http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp', 'link'))
self.assertEqual(feed['items'][0]['links'][1], ('https://example.com/images/logo.png', 'image'))
self.assertEqual(feed['items'][0]['links'][2], ('http://howe.iki.rssi.ru/GCTC/gctc_e.htm', 'link'))
def test_illformed(self):
with self.assertRaises(ValueError):
content = "Invalid RSS"
rss_reader.parse_feed(content)
def test_limit(self):
content = self.SAMPLE_2_0
feed = rss_reader.parse_feed(content)
rss_reader.limit_feed(feed, 1)
self.assertEqual(len(feed['items']), 1)
def test_overlimit(self):
content = self.SAMPLE_2_0
feed = rss_reader.parse_feed(content)
rss_reader.limit_feed(feed, 5)
self.assertEqual(len(feed['items']), 4)
def test_text(self):
content = self.SAMPLE_2_0
feed = rss_reader.parse_feed(content)
rss_reader.limit_feed(feed, 1)
text = rss_reader.format_text(feed)
self.assertEqual(text,
"Feed: Liftoff News\n\nTitle: Star City\n"
"Date: Tue, 03 Jun 2003 09:39:21 +0000\n"
"Link: http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp\n\n"
"[image 2]How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia\'s Star City[3].\n\n"
"Links:\n[1]: http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp (link)\n"
"[2]: https://example.com/images/logo.png (image)\n"
"[3]: http://howe.iki.rssi.ru/GCTC/gctc_e.htm (link)\n")
def test_json(self):
content = self.SAMPLE_2_0
feed = rss_reader.parse_feed(content)
rss_reader.limit_feed(feed, 1)
json = rss_reader.format_json(feed)
self.maxDiff = None
self.assertEqual(json,
'{\n "title": "Liftoff News",\n "link": "http://liftoff.msfc.nasa.gov/",\n'
' "description": "Liftoff to Space Exploration.",\n "items": [\n'
' {\n "title": "Star City",\n "pubDate": "Tue, 03 Jun 2003 09:39:21 +0000",\n'
' "link": "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp",\n'
' "description": "<img src=\\"https://example.com/images/logo.png\\">How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia\'s <a href=\\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\\">Star City</a>.",\n'
' "links": [\n'
' [\n "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp",\n "link"\n ],\n'
' [\n "https://example.com/images/logo.png",\n "image"\n ],\n'
' [\n "http://howe.iki.rssi.ru/GCTC/gctc_e.htm",\n "link"\n ]\n'
' ]\n }\n ]\n}')
class TestAuxiliary(unittest.TestCase):
def test_get_link(self):
elem = bs4.BeautifulSoup('<enclosure url="http://www.scripting.com/mp3s/weatherReportSuite.mp3" length="12216320" type="audio/mpeg" />', 'lxml-xml').enclosure
link = rss_reader.get_link(elem)
self.assertEqual(link, ('http://www.scripting.com/mp3s/weatherReportSuite.mp3', 'audio'))
def test_get_text(self):
self.assertEqual(rss_reader.get_text(None), '')
class TextCache(unittest.TestCase):
def test_merge_items(self):
self.maxDiff = None
alist = [
{
'title': 'A',
'link': 'http://example.com/A',
'description': 'Describe A',
'pubDate': datetime.datetime.fromisoformat('2022-03-01T10:11:23+03:00')
},
{
'title': 'B',
'link': 'http://example.com/B',
'description': 'Describe B',
'pubDate': datetime.datetime.fromisoformat('2022-02-02T10:11:23+03:00')
},
]
blist = [
{
'title': 'B',
'link': 'http://example.com/B',
'description': 'Describe B',
'pubDate': datetime.datetime.fromisoformat('2022-02-02T10:11:23+03:00')
},
{
'title': 'C',
'link': 'http://example.com/C',
'description': 'Describe C',
'pubDate': datetime.datetime.fromisoformat('2022-01-02T10:11:23+03:00')
},
]
rlist = [
{
'title': 'A',
'link': 'http://example.com/A',
'description': 'Describe A',
'pubDate': datetime.datetime.fromisoformat('2022-03-01T10:11:23+03:00')
},
{
'title': 'B',
'link': 'http://example.com/B',
'description': 'Describe B',
'pubDate': datetime.datetime.fromisoformat('2022-02-02T10:11:23+03:00')
},
{
'title': 'C',
'link': 'http://example.com/C',
'description': 'Describe C',
'pubDate': datetime.datetime.fromisoformat('2022-01-02T10:11:23+03:00')
},
]
self.assertEqual(rss_reader.merge_items(alist, blist), rlist)
def test_update_cache(self):
cache = {
'http://example.com/feedA': {
'title': 'feed A',
'description': 'Describe A',
'link': 'http://example.com/feedA',
'items': []
}
}
feed = {
'title': 'feed B',
'description': 'Describe B',
'link': 'http://example.com/feedB',
'items': []
}
new_cache = {
'http://example.com/feedA': {
'title': 'feed A',
'description': 'Describe A',
'link': 'http://example.com/feedA',
'items': []
},
'http://example.com/feedB': {
'title': 'feed B',
'description': 'Describe B',
'link': 'http://example.com/feedB',
'items': []
}
}
rss_reader.update_cache(cache, 'http://example.com/feedB', feed)
self.assertEqual(cache, new_cache)
def test_lookup_cache(self):
cache = {
'http://example.com/feedA': {
'title': 'feed A',
'description': 'Describe A',
'link': 'http://example.com/feedA',
'items': []
},
'http://example.com/feedB': {
'title': 'feed B',
'description': 'Describe B',
'link': 'http://example.com/feedB',
'items': []
}
}
self.assertEqual(
rss_reader.lookup_cache(
cache, 'http://example.com/feedA',
datetime.datetime.fromisoformat('2022-06-01T01:00+03:00')),
{
'title': 'feed A',
'description': 'Describe A',
'link': 'http://example.com/feedA',
'items': []
}
)
class TestFormatters(unittest.TestCase):
def test_get_cached_image(self):
feed = {
'title': 'Feed A',
'description': 'Describe A',
'link': 'http://example.com/feedA',
'items': [
{
'title': 'A01',
'link': 'http://example.com/feedA',
'description': 'Describe A01',
'pubDate': datetime.datetime.fromisoformat('2022-01-02T10:11:23+03:00'),
'links': [('http://example.com/feedA', 'link'), ('http://example.com/image01.png', 'image')],
'images': {'http://example.com/image01.png': b'IMAGEDATA'}
}
]
}
formatter = rss_reader.Formatters(feed)
self.assertEqual(formatter._get_cached_image('http://example.com/image01.png').getvalue(), b'IMAGEDATA')
def test_to_html(self):
feed = {
'title': 'Feed A',
'description': 'Describe A',
'link': 'http://example.com/feedA',
'items': [
{
'title': 'A01',
'link': 'http://example.com/feedA',
'description': 'Describe A01',
'description_raw': 'Describe A01',
'pubDate': datetime.datetime.fromisoformat('2022-01-02T10:11:23+03:00'),
'links': [('http://example.com/feedA', 'link'), ('http://example.com/image01.png', 'image')],
'images': {'http://example.com/image01.png': b'IMAGEDATA'}
}
]
}
formatter = rss_reader.Formatters(feed)
self.assertEqual(formatter.to_html,
b'<!DOCTYPE html>\n<html><head><meta charset="utf-8"/><title>Feed A</title></head>'
b'<body><h1>Feed A</h1><div>Describe A</div><h2>A01</h2><p>Sun, 02 Jan 2022 10:11:23 +0300</p>'
b'<div>Describe A01</div><ol><li><a href="http://example.com/feedA">link</a></li>'
b'<li><img height="100" src="http://example.com/image01.png" width="160"/></li></ol>'
b'</body></html>')