test_howdoi.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. """Tests for Howdoi."""
  3. import os
  4. import unittest
  5. import re
  6. import sys
  7. from howdoi import howdoi
  8. class HowdoiTestCase(unittest.TestCase):
  9. def call_howdoi(self, query):
  10. parser = howdoi.get_parser()
  11. args = vars(parser.parse_args(query.split(' ')))
  12. return howdoi.howdoi(args)
  13. def setUp(self):
  14. self.queries = ['format date bash',
  15. 'print stack trace python',
  16. 'convert mp4 to animated gif',
  17. 'create tar archive',
  18. 'cat']
  19. self.pt_queries = ['abrir arquivo em python',
  20. 'enviar email em django',
  21. 'hello world em c']
  22. self.bad_queries = ['moe',
  23. 'mel']
  24. def tearDown(self):
  25. pass
  26. def test_get_link_at_pos(self):
  27. self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 1),
  28. '/questions/42/')
  29. self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 2),
  30. '/questions/42/')
  31. self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 1),
  32. '/howdoi')
  33. self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 2),
  34. '/questions/42/')
  35. self.assertEqual(howdoi.get_link_at_pos(['/questions/42/', '/questions/142/'], 1),
  36. '/questions/42/')
  37. def test_answers(self):
  38. for query in self.queries:
  39. self.assertTrue(self.call_howdoi(query))
  40. for query in self.bad_queries:
  41. self.assertTrue(self.call_howdoi(query))
  42. os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
  43. for query in self.pt_queries:
  44. self.assertTrue(self.call_howdoi(query))
  45. def test_answers_bing(self):
  46. os.environ['HOWDOI_SEARCH_ENGINE'] = 'bing'
  47. for query in self.queries:
  48. self.assertTrue(self.call_howdoi(query))
  49. for query in self.bad_queries:
  50. self.assertTrue(self.call_howdoi(query))
  51. os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
  52. for query in self.pt_queries:
  53. self.assertTrue(self.call_howdoi(query))
  54. os.environ['HOWDOI_SEARCH_ENGINE'] = ''
  55. def test_answer_links_using_l_option(self):
  56. for query in self.queries:
  57. response = self.call_howdoi(query + ' -l')
  58. self.assertNotEqual(re.match('http.?://.*questions/\d.*', response, re.DOTALL), None)
  59. def test_answer_links_using_all_option(self):
  60. for query in self.queries:
  61. response = self.call_howdoi(query + ' -a')
  62. self.assertNotEqual(re.match('.*http.?://.*questions/\d.*', response, re.DOTALL), None)
  63. def test_position(self):
  64. query = self.queries[0]
  65. first_answer = self.call_howdoi(query)
  66. second_answer = self.call_howdoi(query + ' -p2')
  67. self.assertNotEqual(first_answer, second_answer)
  68. def test_all_text(self):
  69. query = self.queries[0]
  70. first_answer = self.call_howdoi(query)
  71. second_answer = self.call_howdoi(query + ' -a')
  72. self.assertNotEqual(first_answer, second_answer)
  73. self.assertNotEqual(re.match('.*Answer from http.?://.*', second_answer, re.DOTALL), None)
  74. def test_multiple_answers(self):
  75. query = self.queries[0]
  76. first_answer = self.call_howdoi(query)
  77. second_answer = self.call_howdoi(query + ' -n3')
  78. self.assertNotEqual(first_answer, second_answer)
  79. def test_unicode_answer(self):
  80. assert self.call_howdoi('make a log scale d3')
  81. assert self.call_howdoi('python unittest -n3')
  82. assert self.call_howdoi('parse html regex -a')
  83. assert self.call_howdoi('delete remote git branch -a')
  84. def test_colorize(self):
  85. query = self.queries[0]
  86. normal = self.call_howdoi(query)
  87. colorized = self.call_howdoi('-c ' + query)
  88. self.assertTrue(normal.find('[39;') is -1)
  89. self.assertTrue(colorized.find('[39;') is not -1)
  90. def test_get_questions(self):
  91. links = ['https://stackoverflow.com/questions/tagged/cat', 'http://rads.stackoverflow.com/amzn/click/B007KAZ166', 'https://stackoverflow.com/questions/40108569/how-to-get-the-last-line-of-a-file-using-cat-command']
  92. expected_output = ['https://stackoverflow.com/questions/40108569/how-to-get-the-last-line-of-a-file-using-cat-command']
  93. actual_output = howdoi._get_questions(links)
  94. if sys.version < '2.7':
  95. self.assertEqual(len(actual_output), len(expected_output))
  96. self.assertEqual(sorted(actual_output), sorted(expected_output))
  97. else:
  98. self.assertSequenceEqual(actual_output, expected_output)
  99. class HowdoiTestCaseEnvProxies(unittest.TestCase):
  100. def setUp(self):
  101. self.temp_get_proxies = howdoi.getproxies
  102. def tearDown(self):
  103. howdoi.getproxies = self.temp_get_proxies
  104. def test_get_proxies1(self):
  105. def getproxies1():
  106. proxies = {'http': 'wwwproxy.company.com',
  107. 'https': 'wwwproxy.company.com',
  108. 'ftp': 'ftpproxy.company.com'}
  109. return proxies
  110. howdoi.getproxies = getproxies1
  111. filtered_proxies = howdoi.get_proxies()
  112. self.assertTrue('http://' in filtered_proxies['http'])
  113. self.assertTrue('http://' in filtered_proxies['https'])
  114. self.assertTrue('ftp' not in filtered_proxies.keys())
  115. if __name__ == '__main__':
  116. unittest.main()