forked from chaoss/grimoirelab-perceval
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_rss.py
298 lines (226 loc) · 9.9 KB
/
test_rss.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2020 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Alvaro del Castillo <[email protected]>
# Santiago Dueñas <[email protected]>
# Stephan Barth <[email protected]>
# Valerio Cosentino <[email protected]>
# Miguel Ángel Fernández <[email protected]>
# Harshal Mittal <[email protected]>
#
import httpretty
import os
import unittest
from perceval.backend import BackendCommandArgumentParser
from perceval.backends.core.rss import RSS, RSSCommand, RSSClient
from base import TestCaseBackendArchive
RSS_FEED_URL = 'http://example.com/rss'
requests_http = []
def read_file(filename, mode='r'):
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), filename), mode) as f:
content = f.read()
return content
def configure_http_server():
bodies_entries_job = read_file('data/rss/rss_entries.xml')
http_requests = []
def request_callback(method, uri, headers):
last_request = httpretty.last_request()
if uri.startswith(RSS_FEED_URL):
body = bodies_entries_job
else:
body = ''
requests_http.append(httpretty.last_request())
http_requests.append(last_request)
return (200, headers, body)
httpretty.register_uri(httpretty.GET,
RSS_FEED_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
return http_requests
class TestRSSBackend(unittest.TestCase):
"""RSS backend tests"""
def test_initialization(self):
"""Test whether attributes are initializated"""
rss = RSS(RSS_FEED_URL, tag='test')
self.assertEqual(rss.url, RSS_FEED_URL)
self.assertEqual(rss.origin, RSS_FEED_URL)
self.assertEqual(rss.tag, 'test')
self.assertIsNone(rss.client)
self.assertTrue(rss.ssl_verify)
# When tag is empty or None it will be set to
# the value in url
rss = RSS(RSS_FEED_URL, ssl_verify=False)
self.assertEqual(rss.url, RSS_FEED_URL)
self.assertEqual(rss.origin, RSS_FEED_URL)
self.assertEqual(rss.tag, RSS_FEED_URL)
self.assertFalse(rss.ssl_verify)
rss = RSS(RSS_FEED_URL, tag='')
self.assertEqual(rss.url, RSS_FEED_URL)
self.assertEqual(rss.origin, RSS_FEED_URL)
self.assertEqual(rss.tag, RSS_FEED_URL)
def test_has_archiving(self):
"""Test if it returns True when has_archiving is called"""
self.assertEqual(RSS.has_archiving(), True)
def test_has_resuming(self):
"""Test if it returns False when has_resuming is called"""
self.assertEqual(RSS.has_resuming(), False)
@httpretty.activate
def test_fetch(self):
"""Test whether a list of entries is returned"""
http_requests = configure_http_server()
# Test fetch entries from feed
rss = RSS(RSS_FEED_URL)
entries = [entry for entry in rss.fetch()]
self.assertEqual(len(entries), 30)
self.assertEqual(len(http_requests), 1)
# Test metadata
expected = [('98572defb3a652afbfdfe96517edefb88a22dcfa', 1481044620.0,
'Connect 2016 Developer Workshop'),
('e3b0d7463fca0c47d82debc3ddc37d8906f77548', 1480955040.0,
'Create a URL Shortener with Node.js and Couchbase using N1QL'),
('28bca39353ff0825f53b157b69da319e2f49ab4d', 1480886040.0,
'ELT processing with Couchbase and N1QL')]
for x in range(len(expected)):
entry = entries[x]
self.assertEqual(entry['origin'], 'http://example.com/rss')
self.assertEqual(entry['uuid'], expected[x][0])
self.assertEqual(entry['updated_on'], expected[x][1])
self.assertEqual(entry['category'], 'entry')
self.assertEqual(entry['tag'], 'http://example.com/rss')
self.assertEqual(entry['data']['title'], expected[x][2])
@httpretty.activate
def test_search_fields(self):
"""Test whether the search_fields is properly set"""
configure_http_server()
rss = RSS(RSS_FEED_URL)
entries = [entry for entry in rss.fetch()]
for entry in entries:
self.assertEqual(rss.metadata_id(entry['data']), entry['search_fields']['item_id'])
@httpretty.activate
def test_fetch_empty(self):
"""Test whether it works when no entries are fetched"""
body = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0">
</rss>
"""
httpretty.register_uri(httpretty.GET,
RSS_FEED_URL,
body=body, status=200)
rss = RSS(RSS_FEED_URL)
entries = [entry for entry in rss.fetch()]
self.assertEqual(len(entries), 0)
@httpretty.activate
def test_parse(self):
"""Test whether the parser works """
xml_feed = read_file('data/rss/rss_entries.xml')
json_feed = RSS.parse_feed(xml_feed)['entries']
entry = json_feed[0]
""" rss version="2.0"
<entry><title>Connect 2016 Developer Workshop</title>
<link>http://blog.couchbase.com/2016/november/connect-2016-developer-workshop</link>
<description><p>For the first day ... </description>
<pubDate>Tue, 06 Dec 2016 17:17:00 +0000</pubDate>
<author>Matthew Groves</author>
<avatar>/content/gallery/speakers/speakersCouchbase/mebricks.jpg/mebricks.jpg/hippogallery:original</avatar>
</entry>
"""
self.assertEqual(entry['title'], 'Connect 2016 Developer Workshop')
self.assertEqual(entry['published'], 'Tue, 06 Dec 2016 17:17:00 +0000')
self.assertEqual(entry['avatar'],
'/content/gallery/speakers/speakersCouchbase/mebricks.jpg/mebricks.jpg/hippogallery:original')
self.assertEqual(entry['link'], 'http://blog.couchbase.com/2016/november/connect-2016-developer-workshop')
self.assertEqual(len(entry['summary']), 410)
self.assertEqual(entry['author'], 'Matthew Groves')
class TestRSSBackendArchive(TestCaseBackendArchive):
"""RSS backend tests using an archive"""
def setUp(self):
super().setUp()
self.backend_write_archive = RSS(RSS_FEED_URL, archive=self.archive)
self.backend_read_archive = RSS(RSS_FEED_URL, archive=self.archive)
@httpretty.activate
def test_fetch_from_archive(self):
"""Test whether a list of entries is returned from archive"""
configure_http_server()
self._test_fetch_from_archive()
@httpretty.activate
def test_fetch_empty_from_archive(self):
"""Test whether the method fetch from archive works when no entries are present"""
body = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0">
</rss>
"""
httpretty.register_uri(httpretty.GET,
RSS_FEED_URL,
body=body, status=200)
self._test_fetch_from_archive()
class TestRSSCommand(unittest.TestCase):
"""RSSCommand unit tests"""
def test_backend_class(self):
"""Test if the backend class is RSS"""
self.assertIs(RSSCommand.BACKEND, RSS)
def test_setup_cmd_parser(self):
"""Test if it parser object is correctly initialized"""
parser = RSSCommand.setup_cmd_parser()
self.assertIsInstance(parser, BackendCommandArgumentParser)
self.assertEqual(parser._backend, RSS)
args = ['--tag', 'test',
'--no-archive',
RSS_FEED_URL]
parsed_args = parser.parse(*args)
self.assertEqual(parsed_args.url, RSS_FEED_URL)
self.assertEqual(parsed_args.tag, 'test')
self.assertTrue(parsed_args.no_archive)
self.assertTrue(parsed_args.ssl_verify)
args = ['--tag', 'test',
'--no-archive',
'--no-ssl-verify',
RSS_FEED_URL]
parsed_args = parser.parse(*args)
self.assertEqual(parsed_args.url, RSS_FEED_URL)
self.assertEqual(parsed_args.tag, 'test')
self.assertTrue(parsed_args.no_archive)
self.assertFalse(parsed_args.ssl_verify)
class TestRSSClient(unittest.TestCase):
"""RSS API client tests
These tests not check the body of the response, only if the call
was well formed and if a response was obtained. Due to this, take
into account that the body returned on each request might not
match with the parameters from the request.
"""
@httpretty.activate
def test_init(self):
"""Test initialization"""
client = RSSClient(RSS_FEED_URL)
@httpretty.activate
def test_get_entries(self):
"""Test get_entries API call"""
# Set up a mock HTTP server
body = read_file('data/rss/rss_entries.xml')
httpretty.register_uri(httpretty.GET,
RSS_FEED_URL,
body=body, status=200)
client = RSSClient(RSS_FEED_URL)
response = client.get_entries()
self.assertEqual(response, body)
if __name__ == "__main__":
unittest.main(warnings='ignore')