forked from gleitz/howdoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_howdoi.py
113 lines (89 loc) · 3.91 KB
/
test_howdoi.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
#!/usr/bin/env python
"""Tests for Howdoi."""
import os
import unittest
from howdoi import howdoi
class HowdoiTestCase(unittest.TestCase):
def call_howdoi(self, query):
parser = howdoi.get_parser()
args = vars(parser.parse_args(query.split(' ')))
return howdoi.howdoi(args)
def setUp(self):
self.queries = ['format date bash',
'print stack trace python',
'convert mp4 to animated gif',
'create tar archive']
self.pt_queries = ['abrir arquivo em python',
'enviar email em django',
'hello world em c']
self.bad_queries = ['moe',
'mel']
def tearDown(self):
pass
def test_get_link_at_pos(self):
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 1),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 2),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 1),
'/howdoi')
self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 2),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/', '/questions/142/'], 1),
'/questions/42/')
def test_answers(self):
for query in self.queries:
self.assertTrue(self.call_howdoi(query))
for query in self.bad_queries:
self.assertTrue(self.call_howdoi(query))
os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
for query in self.pt_queries:
self.assertTrue(self.call_howdoi(query))
def test_answer_links(self):
for query in self.queries:
self.assertTrue('http://' in self.call_howdoi(query + ' -l'))
def test_position(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -p2')
self.assertNotEqual(first_answer, second_answer)
def test_all_text(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -a')
self.assertNotEqual(first_answer, second_answer)
self.assertTrue("Answer from http://stackoverflow.com" in second_answer)
def test_multiple_answers(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -n3')
self.assertNotEqual(first_answer, second_answer)
def test_unicode_answer(self):
assert self.call_howdoi('make a log scale d3')
assert self.call_howdoi('python unittest -n3')
assert self.call_howdoi('parse html regex -a')
assert self.call_howdoi('delete remote git branch -a')
def test_colorize(self):
query = self.queries[0]
normal = self.call_howdoi(query)
colorized = self.call_howdoi('-c ' + query)
self.assertTrue(normal.find('[39;') is -1)
self.assertTrue(colorized.find('[39;') is not -1)
class HowdoiTestCaseEnvProxies(unittest.TestCase):
def setUp(self):
self.temp_get_proxies = howdoi.getproxies
def tearDown(self):
howdoi.getproxies = self.temp_get_proxies
def test_get_proxies1(self):
def getproxies1():
proxies = {'http': 'wwwproxy.company.com',
'https': 'wwwproxy.company.com',
'ftp': 'ftpproxy.company.com'}
return proxies
howdoi.getproxies = getproxies1
filtered_proxies = howdoi.get_proxies()
self.assertTrue('http://' in filtered_proxies['http'])
self.assertTrue('http://' in filtered_proxies['https'])
self.assertTrue('ftp' not in filtered_proxies.keys())
if __name__ == '__main__':
unittest.main()