/
test_kitsune.py
371 lines (290 loc) · 13.3 KB
/
test_kitsune.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors:
# Santiago Dueñas <sduenas@bitergia.com>
# Alvaro del Castillo <acs@bitergia.com>
#
import argparse
import json
import shutil
import sys
import tempfile
import unittest
import requests
import httpretty
if not '..' in sys.path:
sys.path.insert(0, '..')
from perceval.cache import Cache
from perceval.errors import CacheError
from perceval.backends.kitsune import Kitsune, KitsuneCommand, KitsuneClient
from perceval.utils import str_to_datetime
KITSUNE_SERVER_URL = 'http://example.com'
KITSUNE_API = KITSUNE_SERVER_URL + '/api/2/'
KITSUNE_API_QUESTION = KITSUNE_SERVER_URL + '/api/2/question/'
KITSUNE_API_ANSWER = KITSUNE_SERVER_URL + '/api/2/answer/'
KITSUNE_SERVER_FAIL_PAGE = 69
KITSUNE_ITEMS_PER_PAGE = 20
def read_file(filename, mode='r'):
with open(filename, mode) as f:
content = f.read()
return content
class HTTPServer():
requests_http = [] # requests done to the server
@classmethod
def routes(cls, empty=False):
"""Configure in http the routes to be served"""
mozilla_questions_1 = read_file('data/kitsune_questions_1_2.json')
mozilla_questions_2 = read_file('data/kitsune_questions_2_2.json')
mozilla_questions_empty = read_file('data/kitsune_questions_empty.json')
mozilla_question_answers_1 = read_file('data/kitsune_question_answers.json')
mozilla_question_answers_empty = read_file('data/kitsune_question_answers_empy.json')
if empty:
mozilla_questions_1 = mozilla_questions_empty
def request_callback(method, uri, headers):
page = uri.split("page=")[1].split("&")[0]
if page == "1":
if 'question/' in uri:
body = mozilla_questions_1
else:
question = uri.split("question=")[1].split("&")[0]
body = mozilla_question_answers_1
body_json = json.loads(body)['results']
if body_json[0]['question'] != int(question):
# The answers are not for this question
body = mozilla_question_answers_empty
elif page == "2":
if 'question/' in uri:
body = mozilla_questions_2
else:
body = mozilla_question_answers_1
elif page == str(KITSUNE_SERVER_FAIL_PAGE):
# To tests for Internal Server Error
return (500, headers, '')
elif page == str(KITSUNE_SERVER_FAIL_PAGE + 1):
# Next page to the server fail returns questions
return (200, headers, mozilla_questions_2)
else:
return (404, headers, '')
HTTPServer.requests_http.append(httpretty.last_request())
return (200, headers, body)
httpretty.register_uri(httpretty.GET,
KITSUNE_API_QUESTION,
responses=[
httpretty.Response(body=request_callback)
])
httpretty.register_uri(httpretty.GET,
KITSUNE_API_ANSWER,
responses=[
httpretty.Response(body=request_callback)
])
class TestKitsuneBackend(unittest.TestCase):
"""Kitsune backend tests"""
def test_initialization(self):
"""Test whether attributes are initializated"""
kitsune = Kitsune(KITSUNE_SERVER_URL, tag='test')
self.assertEqual(kitsune.url, KITSUNE_SERVER_URL)
self.assertEqual(kitsune.origin, KITSUNE_SERVER_URL)
self.assertEqual(kitsune.tag, 'test')
self.assertIsInstance(kitsune.client, KitsuneClient)
# When tag is empty or None it will be set to
# the value in url
kitsune = Kitsune(KITSUNE_SERVER_URL)
self.assertEqual(kitsune.url, KITSUNE_SERVER_URL)
self.assertEqual(kitsune.origin, KITSUNE_SERVER_URL)
self.assertEqual(kitsune.tag, KITSUNE_SERVER_URL)
kitsune = Kitsune(KITSUNE_SERVER_URL, tag='')
self.assertEqual(kitsune.url, KITSUNE_SERVER_URL)
self.assertEqual(kitsune.origin, KITSUNE_SERVER_URL)
self.assertEqual(kitsune.tag, KITSUNE_SERVER_URL)
def test_has_resuming(self):
"""Test if it returns True when has_resuming is called"""
self.assertEqual(Kitsune.has_resuming(), True)
def __check_questions_contents(self, questions):
self.assertEqual(questions[0]['data']['num_votes'], 2)
self.assertEqual(questions[0]['data']['num_answers'], 0)
self.assertEqual(questions[0]['data']['locale'], 'en-US')
self.assertEqual(questions[0]['origin'], KITSUNE_SERVER_URL)
self.assertEqual(questions[0]['uuid'], '8fa01e2aadf37c6f2fa300ce529fd1a23feac333')
self.assertEqual(questions[0]['updated_on'], 1467798846.0)
self.assertEqual(questions[0]['offset'], 0)
self.assertEqual(questions[0]['category'], 'question')
self.assertEqual(questions[0]['tag'], KITSUNE_SERVER_URL)
if len(questions) > 1:
self.assertEqual(questions[1]['data']['num_votes'], 1)
self.assertEqual(questions[1]['data']['num_answers'], 0)
self.assertEqual(questions[1]['data']['locale'], 'es')
self.assertEqual(questions[1]['origin'], KITSUNE_SERVER_URL)
self.assertEqual(questions[1]['uuid'], '7a3fbfb33cfaaa32b2f121994faf019a054a9a06')
self.assertEqual(questions[1]['updated_on'], 1467798439.0)
self.assertEqual(questions[1]['offset'], 1)
self.assertEqual(questions[1]['category'], 'question')
self.assertEqual(questions[1]['tag'], KITSUNE_SERVER_URL)
@httpretty.activate
def test_fetch(self):
"""Test whether the questions are returned"""
HTTPServer.routes()
# Test fetch questions with their reviews
kitsune = Kitsune(KITSUNE_SERVER_URL)
questions = [question for question in kitsune.fetch()]
self.assertEqual(len(questions), 4)
self.__check_questions_contents(questions)
@httpretty.activate
def test_fetch_offset(self):
"""Test whether the questions are returned offset"""
HTTPServer.routes()
# Test fetch questions with their reviews
kitsune = Kitsune(KITSUNE_SERVER_URL)
# Get all questions
offset = 0
questions = [question for question in kitsune.fetch(offset=offset)]
self.assertEqual(len(questions), 4)
self.__check_questions_contents(questions)
# 4 total questions minus 2, 2 questions returned
offset = 2
questions = [question for question in kitsune.fetch(offset=offset)]
self.assertEqual(len(questions), 2)
self.assertEqual(questions[0]['offset'], 2)
self.assertEqual(questions[1]['offset'], 3)
offset = 4
questions = [question for question in kitsune.fetch(offset=offset)]
self.assertEqual(len(questions), 0)
# Get no questions: we have two pages
offset = KitsuneClient.ITEMS_PER_PAGE*2
with self.assertRaises(requests.exceptions.HTTPError):
questions = [question for question in kitsune.fetch(offset=offset)]
self.assertEqual(len(questions), 0)
@httpretty.activate
def test_fetch_empty(self):
"""Test whether it works when no jobs are fetched"""
HTTPServer.routes(empty=True)
kitsune = Kitsune(KITSUNE_SERVER_URL)
questions = [event for event in kitsune.fetch()]
self.assertEqual(len(questions), 0)
@httpretty.activate
def test_fetch_server_error(self):
"""Test whether it works when the server fails"""
HTTPServer.routes(empty=True)
kitsune = Kitsune(KITSUNE_SERVER_URL)
offset = (KITSUNE_SERVER_FAIL_PAGE - 1) * KITSUNE_ITEMS_PER_PAGE
questions = [event for event in kitsune.fetch(offset=offset)]
# After the failing page there are a page with 2 questions
self.assertEqual(len(questions), 2)
class TestKitsuneBackendCache(unittest.TestCase):
"""Kitsune backend tests using a cache"""
def setUp(self):
self.tmp_path = tempfile.mkdtemp(prefix='perceval_')
def tearDown(self):
shutil.rmtree(self.tmp_path)
@httpretty.activate
def test_fetch_from_cache(self):
"""Test whether the cache works"""
HTTPServer.routes()
# First, we fetch the questions from the server, storing them
# in a cache
cache = Cache(self.tmp_path)
kitsune = Kitsune(KITSUNE_SERVER_URL, cache=cache)
questions = [event for event in kitsune.fetch()]
requests_done = len(HTTPServer.requests_http)
# Now, we get the questions from the cache.
# The contents should be the same and there won't be
# any new request to the server
cached_questions = [event for event in kitsune.fetch_from_cache()]
# No new requests to the server
self.assertEqual(len(HTTPServer.requests_http), requests_done)
# The contents should be the same
self.assertEqual(len(cached_questions), len(questions))
for i in range(0,len(questions)):
self.assertDictEqual(cached_questions[i]['data'], questions[i]['data'])
def test_fetch_from_empty_cache(self):
"""Test if there are not any questions returned when the cache is empty"""
cache = Cache(self.tmp_path)
kitsune = Kitsune(KITSUNE_SERVER_URL, cache=cache)
cached_questions = [event for event in kitsune.fetch_from_cache()]
self.assertEqual(len(cached_questions), 0)
def test_fetch_from_non_set_cache(self):
"""Test if a error is raised when the cache was not set"""
kitsune = Kitsune(KITSUNE_SERVER_URL)
with self.assertRaises(CacheError):
_ = [event for event in kitsune.fetch_from_cache()]
class TestKitsuneCommand(unittest.TestCase):
@httpretty.activate
def test_parsing_on_init(self):
"""Test if the class is initialized"""
args = ['--tag', 'test', KITSUNE_SERVER_URL]
cmd = KitsuneCommand(*args)
self.assertIsInstance(cmd.parsed_args, argparse.Namespace)
self.assertEqual(cmd.parsed_args.url, KITSUNE_SERVER_URL)
self.assertEqual(cmd.parsed_args.tag, 'test')
self.assertIsInstance(cmd.backend, Kitsune)
def test_argument_parser(self):
"""Test if it returns a argument parser object"""
parser = KitsuneCommand.create_argument_parser()
self.assertIsInstance(parser, argparse.ArgumentParser)
class TestKitsuneClient(unittest.TestCase):
"""Kitsune 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 = KitsuneClient(KITSUNE_SERVER_URL)
@httpretty.activate
def test_get_questions(self):
"""Test get_questions API call"""
HTTPServer.routes()
# Set up a mock HTTP server
body = read_file('data/kitsune_questions_1_2.json')
client = KitsuneClient(KITSUNE_SERVER_URL)
response = next(client.get_questions()) # first group of questions
req = HTTPServer.requests_http[-1]
self.assertEqual(response, body)
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, '/api/2/question/')
# Check request params
expected = {
'page' : ['1'],
'ordering' : ['updated'],
}
self.assertDictEqual(req.querystring, expected)
@httpretty.activate
def test_get_question_answers(self):
"""Test get_question_answers API call"""
HTTPServer.routes()
# Set up a mock HTTP server
body = read_file('data/kitsune_question_answers.json')
client = KitsuneClient(KITSUNE_SERVER_URL)
question_id = 1129949
response = next(client.get_question_answers(question_id))
req = HTTPServer.requests_http[-1]
self.assertEqual(response, body)
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, '/api/2/answer/')
# Check request params
expected = {
'question': ['1129949'],
'page' : ['1'],
'ordering' : ['updated']
}
self.assertDictEqual(req.querystring, expected)
if __name__ == "__main__":
unittest.main(warnings='ignore')