test_stackoverflow.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import stackoverflow
  4. from searx.testing import SearxTestCase
  5. class TestStackoverflowEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = stackoverflow.request(query, dicto)
  11. self.assertTrue('url' in params)
  12. self.assertTrue(query in params['url'])
  13. self.assertTrue('stackoverflow.com' in params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, stackoverflow.response, None)
  16. self.assertRaises(AttributeError, stackoverflow.response, [])
  17. self.assertRaises(AttributeError, stackoverflow.response, '')
  18. self.assertRaises(AttributeError, stackoverflow.response, '[]')
  19. response = mock.Mock(text='<html></html>')
  20. self.assertEqual(stackoverflow.response(response), [])
  21. html = """
  22. <div class="question-summary search-result" id="answer-id-1783426">
  23. <div class="statscontainer">
  24. <div class="statsarrow"></div>
  25. <div class="stats">
  26. <div class="vote">
  27. <div class="votes answered">
  28. <span class="vote-count-post "><strong>2583</strong></span>
  29. <div class="viewcount">votes</div>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <div class="summary">
  35. <div class="result-link">
  36. <span>
  37. <a href="/questions/this.is.the.url"
  38. data-searchsession="/questions"
  39. title="Checkout remote Git branch">
  40. This is the title
  41. </a>
  42. </span>
  43. </div>
  44. <div class="excerpt">
  45. This is the content
  46. </div>
  47. <div class="tags user-tags t-git t-git-checkout t-remote-branch">
  48. </div>
  49. <div class="started fr">
  50. answered <span title="2009-11-23 14:26:08Z" class="relativetime">nov 23 '09</span> by
  51. <a href="/users/214090/hallski">hallski</a>
  52. </div>
  53. </div>
  54. </div>
  55. """
  56. response = mock.Mock(text=html)
  57. results = stackoverflow.response(response)
  58. self.assertEqual(type(results), list)
  59. self.assertEqual(len(results), 1)
  60. self.assertEqual(results[0]['title'], 'This is the title')
  61. self.assertEqual(results[0]['url'], 'https://stackoverflow.com/questions/this.is.the.url')
  62. self.assertEqual(results[0]['content'], 'This is the content')
  63. html = """
  64. <div class="statscontainer">
  65. <div class="statsarrow"></div>
  66. <div class="stats">
  67. <div class="vote">
  68. <div class="votes answered">
  69. <span class="vote-count-post "><strong>2583</strong></span>
  70. <div class="viewcount">votes</div>
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. <div class="summary">
  76. <div class="result-link">
  77. <span>
  78. <a href="/questions/this.is.the.url"
  79. data-searchsession="/questions"
  80. title="Checkout remote Git branch">
  81. This is the title
  82. </a>
  83. </span>
  84. </div>
  85. <div class="excerpt">
  86. This is the content
  87. </div>
  88. <div class="tags user-tags t-git t-git-checkout t-remote-branch">
  89. </div>
  90. <div class="started fr">
  91. answered <span title="2009-11-23 14:26:08Z" class="relativetime">nov 23 '09</span> by
  92. <a href="/users/214090/hallski">hallski</a>
  93. </div>
  94. </div>
  95. """
  96. response = mock.Mock(text=html)
  97. results = stackoverflow.response(response)
  98. self.assertEqual(type(results), list)
  99. self.assertEqual(len(results), 0)