forked from chaoss/grimoirelab-perceval
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_askbot.py
744 lines (554 loc) · 28.5 KB
/
test_askbot.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#!/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:
# Alberto Martín <[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 datetime
import json
import os
import shutil
import unittest
import bs4
import httpretty
import requests
from perceval.backend import BackendCommandArgumentParser
from perceval.backends.core.askbot import (Askbot,
AskbotClient,
AskbotParser,
AskbotCommand)
from perceval.utils import DEFAULT_DATETIME
from base import TestCaseBackendArchive
ASKBOT_URL = 'http://example.com'
ASKBOT_QUESTIONS_API_URL = ASKBOT_URL + '/api/v1/questions'
ASKBOT_QUESTION_2481_URL = ASKBOT_URL + '/question/2481'
ASKBOT_QUESTION_2488_URL = ASKBOT_URL + '/question/2488'
ASKBOT_QUESTION_24396_URL = ASKBOT_URL + '/question/24396'
ASKBOT_QUESTION_EMPTY_URL = ASKBOT_URL + '/question/0'
ASKBOT_COMMENTS_API_URL = ASKBOT_URL + '/s/post_comments'
ASKBOT_COMMENTS_API_URL_OLD = ASKBOT_URL + '/post_comments'
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
class TestAskbotParser(unittest.TestCase):
"""Askbot parser tests"""
def test_parse_question_container(self):
"""Test parse question container.
This tests the full case when a question is, apart from
created, edited by another user.
"""
abparser = AskbotParser()
page = read_file('data/askbot/html_26830_comments_question_openstack.html')
html_question = [page]
container_info = abparser.parse_question_container(html_question[0])
expected_container = {
'author': {
'badges': 'Ignacio Mulas has 4 gold badges, 6 silver badges and 9 bronze badges',
'reputation': '111',
'username': 'Ignacio Mulas',
'id': '5000'
},
'updated_by': {
'website': 'http://maffulli.net/',
'badges': 'smaffulli has 36 gold badges, 67 silver badges and 100 bronze badges',
'reputation': '6898',
'username': 'smaffulli',
'id': '9'
}
}
self.assertEqual(container_info, expected_container)
def test_parse_answers(self):
"""Given a question, parse all the answers available (pagination included)."""
abparser = AskbotParser()
page = read_file('data/askbot/html_24396_multipage_openstack.html')
html_question = [page]
parsed_answers = abparser.parse_answers(html_question[0])
self.assertEqual(len(parsed_answers), 10)
self.assertEqual(parsed_answers[0]['id'], '24427')
self.assertEqual(parsed_answers[0]['score'], '0')
self.assertEqual(parsed_answers[0]['added_at'], '1372894082.0')
self.assertEqual(parsed_answers[1]['id'], '24426')
self.assertEqual(parsed_answers[1]['score'], '0')
self.assertEqual(parsed_answers[1]['added_at'], '1372475606.0')
self.assertEqual(parsed_answers[2]['id'], '24425')
self.assertEqual(parsed_answers[2]['score'], '0')
self.assertEqual(parsed_answers[2]['added_at'], '1365772426.0')
self.assertEqual(parsed_answers[3]['id'], '24424')
self.assertEqual(parsed_answers[3]['score'], '0')
self.assertEqual(parsed_answers[3]['added_at'], '1365766666.0')
self.assertEqual(parsed_answers[4]['id'], '24423')
self.assertEqual(parsed_answers[4]['score'], '0')
self.assertEqual(parsed_answers[4]['added_at'], '1365762818.0')
self.assertEqual(parsed_answers[5]['id'], '24419')
self.assertEqual(parsed_answers[5]['score'], '0')
self.assertEqual(parsed_answers[5]['added_at'], '1365715423.0')
self.assertEqual(parsed_answers[6]['id'], '24418')
self.assertEqual(parsed_answers[6]['score'], '0')
self.assertEqual(parsed_answers[6]['added_at'], '1365687337.0')
self.assertEqual(parsed_answers[7]['id'], '24417')
self.assertEqual(parsed_answers[7]['score'], '0')
self.assertEqual(parsed_answers[7]['added_at'], '1364970027.0')
self.assertEqual(parsed_answers[8]['id'], '24416')
self.assertEqual(parsed_answers[8]['score'], '0')
self.assertEqual(parsed_answers[8]['added_at'], '1364965468.0')
self.assertEqual(parsed_answers[9]['id'], '24414')
self.assertEqual(parsed_answers[9]['score'], '0')
self.assertEqual(parsed_answers[9]['added_at'], '1364453025.0')
def test_parse_number_of_html_pages(self):
"""Get the number of html needed to retrieve all the answers of a given page."""
page = read_file('data/askbot/html_24396_multipage_openstack.html')
html_question = [page]
pages = AskbotParser.parse_number_of_html_pages(html_question[0])
self.assertEqual(pages, 4)
def test_parse_user_info(self):
"""Test user info parsing.
User info can be a wiki post or a user. When a user, some additional information
can be added like country or website when available.
"""
page = read_file('data/askbot/askbot_question_multipage_1.html')
html_question = [page]
bs_question = bs4.BeautifulSoup(html_question[0], "html.parser")
# Test the user_info from the question which is a wiki post and not updated
question = bs_question.select("div.js-question")
container = question[0].select("div.post-update-info")
created = container[0]
author = AskbotParser.parse_user_info(created)
self.assertEqual(author, {})
# Test the user_info from an item with country and website
page = read_file('data/askbot/html_country_and_website.html')
html_question = [page]
bs_question = bs4.BeautifulSoup(html_question[0], "html.parser")
bs_answers = bs_question.select("div.answer")
body = bs_answers[0].select("div.post-body")
update_info = body[0].select("div.post-update-info")
author = AskbotParser.parse_user_info(update_info[0])
self.assertEqual(author['id'], "1")
self.assertEqual(author['badges'], "Evgeny has 56 gold badges, 98 silver badges and 212 bronze badges")
self.assertEqual(author['reputation'], "14023")
self.assertEqual(author['username'], "Evgeny")
self.assertEqual(author['website'], "http://askbot.org/")
self.assertEqual(author['country'], "Chile")
class TestAskbotClient(unittest.TestCase):
"""Askbot client unit tests.
These tests do not check the body of the response, only if the call
was well formed and if a response was obtained.
"""
def test_init(self):
"""Test initialization parameters"""
ab = AskbotClient(ASKBOT_URL)
self.assertEqual(ab.base_url, ASKBOT_URL)
self.assertTrue(ab.ssl_verify)
ab = AskbotClient(ASKBOT_URL, ssl_verify=False)
self.assertEqual(ab.base_url, ASKBOT_URL)
self.assertFalse(ab.ssl_verify)
@httpretty.activate
def test_get_api_questions(self):
"""Test if API Questions call works"""
body = read_file('data/askbot/askbot_api_questions.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=body, status=200)
client = AskbotClient(ASKBOT_URL)
result = next(client.get_api_questions('api/v1/questions'))
self.assertEqual(result, json.loads(body))
expected = {
'page': ['1'],
'sort': ['activity-asc']
}
req = httpretty.last_request()
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, '/api/v1/questions')
self.assertDictEqual(req.querystring, expected)
@httpretty.activate
def test_get_html_question(self):
"""Test if HTML Questions call works."""
body = read_file('data/askbot/askbot_question.html')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2481_URL,
body=body, status=200)
client = AskbotClient(ASKBOT_URL)
result = client.get_html_question(2481)
self.assertEqual(result, body)
req = httpretty.last_request()
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, '/question/2481')
@httpretty.activate
def test_get_html_question_multipage(self):
"""Test if HTML Questions multipage call works."""
body = read_file('data/askbot/askbot_question_multipage_2.html')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=body, status=200)
client = AskbotClient(ASKBOT_URL)
result = client.get_html_question(2488, 2)
self.assertEqual(result, body)
expected = {
'page': ['2'],
'sort': ['votes']
}
req = httpretty.last_request()
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, '/question/2488')
self.assertDictEqual(req.querystring, expected)
@httpretty.activate
def test_get_html_question_empty(self):
"""Test if HTML Questions call (non-existing question) works."""
body = read_file('data/askbot/askbot_question_empty.html')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_EMPTY_URL,
body=body, status=404)
client = AskbotClient(ASKBOT_URL)
self.assertRaises(requests.exceptions.HTTPError, client.get_html_question, 0)
req = httpretty.last_request()
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, '/question/0')
@httpretty.activate
def test_403_status_get_comments(self):
"""Test whether, when fetching comments, an exception is thrown if HTTPError is not 403"""
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body="", status=403)
client = AskbotClient(ASKBOT_URL)
with self.assertRaises(requests.exceptions.HTTPError):
_ = client.get_comments(5)
@httpretty.activate
def test_500_status_get_comments(self):
"""Test whether, when fetching comments, an exception is thrown if HTTPError is not 503"""
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body="", status=500)
client = AskbotClient(ASKBOT_URL)
self.assertEqual(client.get_comments(5), '[]')
@httpretty.activate
def test_get_comments(self):
"""Test if comments call works"""
body = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body=body, status=200)
client = AskbotClient(ASKBOT_URL)
result = client.get_comments(17)
self.assertEqual(result, body)
expected = {
'post_id': ['17'],
'post_type': ['answer'],
'avatar_size': ['0']
}
req = httpretty.last_request()
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, 's/post_comments')
self.assertDictEqual(req.querystring, expected)
@httpretty.activate
def test_get_comments_old_url(self):
"""Test if commits call works with the old URL schema"""
body = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body='', status=404)
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL_OLD,
body=body, status=200)
client = AskbotClient(ASKBOT_URL)
result = client.get_comments(17)
self.assertEqual(result, body)
expected = {
'post_id': ['17'],
'post_type': ['answer'],
'avatar_size': ['0']
}
reqs = httpretty.httpretty.latest_requests
self.assertEqual(len(reqs), 2)
self.assertEqual(reqs[0].method, 'GET')
self.assertRegex(reqs[0].path, '/s/post_comments')
self.assertDictEqual(reqs[0].querystring, expected)
self.assertEqual(reqs[1].method, 'GET')
self.assertRegex(reqs[1].path, '/post_comments')
self.assertDictEqual(reqs[1].querystring, expected)
class TestAskbotBackend(unittest.TestCase):
"""Askbot backend tests."""
def test_initialization(self):
"""Test whether attributes are initializated."""
ab = Askbot(ASKBOT_URL, tag='test')
self.assertEqual(ab.url, ASKBOT_URL)
self.assertEqual(ab.tag, 'test')
self.assertIsNone(ab.client, None)
self.assertTrue(ab.ssl_verify)
# When tag is empty or None it will be set to
# the value in url
ab = Askbot(ASKBOT_URL)
self.assertEqual(ab.url, ASKBOT_URL)
self.assertEqual(ab.tag, ASKBOT_URL)
ab = Askbot(ASKBOT_URL, tag='', ssl_verify=False)
self.assertEqual(ab.url, ASKBOT_URL)
self.assertEqual(ab.tag, ASKBOT_URL)
self.assertFalse(ab.ssl_verify)
@httpretty.activate
def test_too_many_redirects(self):
"""Test whether a too many redirects error is properly handled"""
question_api_1 = read_file('data/askbot/askbot_api_questions.json')
question_api_2 = read_file('data/askbot/askbot_api_questions_2.json')
question_html_2 = read_file('data/askbot/askbot_question_multipage_1.html')
question_html_2_2 = read_file('data/askbot/askbot_question_multipage_2.html')
comments = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2481_URL,
location=ASKBOT_QUESTION_2481_URL, status=301)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body=comments, status=200)
backend = Askbot(ASKBOT_URL)
questions = [question for question in backend.fetch()]
self.assertEqual(len(questions), 1)
@httpretty.activate
def test_fetch(self):
"""Test whether a list of questions is returned"""
question_api_1 = read_file('data/askbot/askbot_api_questions.json')
question_api_2 = read_file('data/askbot/askbot_api_questions_2.json')
question_html_1 = read_file('data/askbot/askbot_question.html')
question_html_2 = read_file('data/askbot/askbot_question_multipage_1.html')
question_html_2_2 = read_file('data/askbot/askbot_question_multipage_2.html')
comments = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2481_URL,
body=question_html_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body=comments, status=200)
backend = Askbot(ASKBOT_URL)
questions = [question for question in backend.fetch()]
json_comments = json.loads(comments)
self.assertEqual(len(questions[0]['data']['answers']), len(questions[0]['data']['answer_ids']))
self.assertTrue(questions[0]['data']['answers'][0]['accepted'])
self.assertEqual(questions[0]['tag'], 'http://example.com')
self.assertEqual(questions[0]['uuid'], '3fb5f945a0dd223c60218a98ad35bad6043f9f5f')
self.assertEqual(questions[0]['updated_on'], 1408116902.0)
self.assertEqual(questions[0]['data']['id'], 2488)
self.assertEqual(questions[0]['category'], backend.metadata_category(questions[0]))
self.assertEqual(questions[0]['data']['comments'][0], json_comments[0])
self.assertEqual(len(questions[0]['data']['comments']), len(json_comments))
self.assertEqual(len(questions[1]['data']['answers']), len(questions[1]['data']['answer_ids']))
self.assertFalse(questions[1]['data']['answers'][0]['accepted'])
self.assertEqual(questions[1]['tag'], 'http://example.com')
self.assertEqual(questions[1]['uuid'], 'ecc1320265e400edb28700cc3d02efc6d76410be')
self.assertEqual(questions[1]['updated_on'], 1349928216.0)
self.assertEqual(questions[1]['data']['id'], 2481)
self.assertEqual(questions[1]['category'], backend.metadata_category(questions[1]))
@httpretty.activate
def test_search_fields(self):
"""Test whether the search_fields is properly set"""
question_api_1 = read_file('data/askbot/askbot_api_questions.json')
question_api_2 = read_file('data/askbot/askbot_api_questions_2.json')
question_html_1 = read_file('data/askbot/askbot_question.html')
question_html_2 = read_file('data/askbot/askbot_question_multipage_1.html')
question_html_2_2 = read_file('data/askbot/askbot_question_multipage_2.html')
comments = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2481_URL,
body=question_html_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body=comments, status=200)
backend = Askbot(ASKBOT_URL)
questions = [question for question in backend.fetch()]
question = questions[0]
self.assertEqual(backend.metadata_id(question['data']), question['search_fields']['item_id'])
self.assertListEqual(question['data']['tags'], ['askbot-sites'])
self.assertEqual(question['data']['tags'], question['search_fields']['tags'])
question = questions[1]
self.assertEqual(backend.metadata_id(question['data']), question['search_fields']['item_id'])
self.assertListEqual(question['data']['tags'], ['feature-request', 'messaging'])
self.assertEqual(question['data']['tags'], question['search_fields']['tags'])
@httpretty.activate
def test_fetch_from_date(self):
"""Test whether a list of questions is returned from a given date."""
question_api_1 = read_file('data/askbot/askbot_api_questions.json')
question_api_2 = read_file('data/askbot/askbot_api_questions_2.json')
question_html_1 = read_file('data/askbot/askbot_question.html')
question_html_2 = read_file('data/askbot/askbot_question_multipage_1.html')
question_html_2_2 = read_file('data/askbot/askbot_question_multipage_2.html')
comments = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2481_URL,
body=question_html_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body=comments, status=200)
backend = Askbot(ASKBOT_URL)
from_date = datetime.datetime(2013, 1, 1)
questions = [question for question in backend.fetch(from_date=from_date)]
self.assertEqual(questions[0]['tag'], 'http://example.com')
self.assertEqual(questions[0]['uuid'], '3fb5f945a0dd223c60218a98ad35bad6043f9f5f')
self.assertEqual(questions[0]['updated_on'], 1408116902.0)
self.assertEqual(questions[0]['data']['id'], 2488)
self.assertEqual(len(questions), 1)
def test_has_resuming(self):
"""Test if it returns True when has_resuming is called."""
self.assertEqual(Askbot.has_resuming(), True)
def test_has_archiving(self):
"""Test if it returns True when has_resuming is called"""
self.assertEqual(Askbot.has_archiving(), True)
class TestAskbotBackendArchive(TestCaseBackendArchive):
"""Askbot backend tests using an archive"""
def setUp(self):
super().setUp()
self.backend_write_archive = Askbot(ASKBOT_URL, archive=self.archive)
self.backend_read_archive = Askbot(ASKBOT_URL, archive=self.archive)
def tearDown(self):
shutil.rmtree(self.test_path)
@httpretty.activate
def test_fetch_from_archive(self):
"""Test whether a list of questions is returned from the archive"""
question_api_1 = read_file('data/askbot/askbot_api_questions.json')
question_api_2 = read_file('data/askbot/askbot_api_questions_2.json')
question_html_1 = read_file('data/askbot/askbot_question.html')
question_html_2 = read_file('data/askbot/askbot_question_multipage_1.html')
question_html_2_2 = read_file('data/askbot/askbot_question_multipage_2.html')
comments = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2481_URL,
body=question_html_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body=comments, status=200)
self._test_fetch_from_archive(from_date=None)
@httpretty.activate
def test_fetch_from_date_from_archive(self):
"""Test whether a list of questions is returned from a given date from the archive."""
question_api_1 = read_file('data/askbot/askbot_api_questions.json')
question_api_2 = read_file('data/askbot/askbot_api_questions_2.json')
question_html_1 = read_file('data/askbot/askbot_question.html')
question_html_2 = read_file('data/askbot/askbot_question_multipage_1.html')
question_html_2_2 = read_file('data/askbot/askbot_question_multipage_2.html')
comments = read_file('data/askbot/askbot_2481_multicomments.json')
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTIONS_API_URL,
body=question_api_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2481_URL,
body=question_html_1, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_QUESTION_2488_URL,
body=question_html_2_2, status=200)
httpretty.register_uri(httpretty.GET,
ASKBOT_COMMENTS_API_URL,
body=comments, status=200)
from_date = datetime.datetime(2013, 1, 1)
self._test_fetch_from_archive(from_date=from_date)
class TestAskbotCommand(unittest.TestCase):
"""Tests for AskbotCommand class."""
def test_backend_class(self):
"""Test if the backend class is Askbot"""
self.assertIs(AskbotCommand.BACKEND, Askbot)
def test_setup_cmd_parser(self):
"""Test if it parser object is correctly initialized"""
parser = AskbotCommand.setup_cmd_parser()
self.assertIsInstance(parser, BackendCommandArgumentParser)
self.assertEqual(parser._backend, Askbot)
args = ['--tag', 'test',
'--from-date', '1970-01-01',
'--no-archive',
ASKBOT_URL]
parsed_args = parser.parse(*args)
self.assertEqual(parsed_args.url, ASKBOT_URL)
self.assertEqual(parsed_args.tag, 'test')
self.assertEqual(parsed_args.from_date, DEFAULT_DATETIME)
self.assertTrue(parsed_args.no_archive)
self.assertTrue(parsed_args.ssl_verify)
args = ['--tag', 'test',
'--from-date', '1970-01-01',
'--no-ssl-verify',
ASKBOT_URL]
parsed_args = parser.parse(*args)
self.assertEqual(parsed_args.url, ASKBOT_URL)
self.assertEqual(parsed_args.tag, 'test')
self.assertEqual(parsed_args.from_date, DEFAULT_DATETIME)
self.assertFalse(parsed_args.ssl_verify)
if __name__ == "__main__":
unittest.main(warnings='ignore')