test_swisscows.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import swisscows
  4. from searx.testing import SearxTestCase
  5. class TestSwisscowsEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 1
  10. dicto['language'] = 'de-DE'
  11. params = swisscows.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('swisscows.ch' in params['url'])
  15. self.assertTrue('uiLanguage=de' in params['url'])
  16. self.assertTrue('region=de-DE' in params['url'])
  17. dicto['language'] = 'all'
  18. params = swisscows.request(query, dicto)
  19. self.assertTrue('uiLanguage=browser' in params['url'])
  20. self.assertTrue('region=browser' in params['url'])
  21. dicto['category'] = 'images'
  22. params = swisscows.request(query, dicto)
  23. self.assertIn('image', params['url'])
  24. def test_response(self):
  25. self.assertRaises(AttributeError, swisscows.response, None)
  26. self.assertRaises(AttributeError, swisscows.response, [])
  27. self.assertRaises(AttributeError, swisscows.response, '')
  28. self.assertRaises(AttributeError, swisscows.response, '[]')
  29. response = mock.Mock(text=b'<html></html>')
  30. self.assertEqual(swisscows.response(response), [])
  31. response = mock.Mock(text=b'<html></html>')
  32. self.assertEqual(swisscows.response(response), [])
  33. html = b"""
  34. <script>
  35. App.Dispatcher.dispatch("initialize", {
  36. html5history: true,
  37. initialData: {"Request":
  38. {"Page":1,
  39. "ItemsCount":1,
  40. "Query":"This should ",
  41. "NormalizedQuery":"This should ",
  42. "Region":"de-AT",
  43. "UILanguage":"de"},
  44. "Results":{"items":[
  45. {"Title":"\uE000This should\uE001 be the title",
  46. "Description":"\uE000This should\uE001 be the content.",
  47. "Url":"http://this.should.be.the.link/",
  48. "DisplayUrl":"www.\uE000this.should.be.the\uE001.link",
  49. "Id":"782ef287-e439-451c-b380-6ebc14ba033d"},
  50. {"Title":"Datei:This should1.svg",
  51. "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should1.png",
  52. "SourceUrl":"http://de.wikipedia.org/wiki/Datei:This should1.svg",
  53. "DisplayUrl":"de.wikipedia.org/wiki/Datei:This should1.svg",
  54. "Width":950,
  55. "Height":534,
  56. "FileSize":92100,
  57. "ContentType":"image/jpeg",
  58. "Thumbnail":{
  59. "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should1.png",
  60. "ContentType":"image/jpeg",
  61. "Width":300,
  62. "Height":168,
  63. "FileSize":9134},
  64. "Id":"6a97a542-8f65-425f-b7f6-1178c3aba7be"
  65. }
  66. ],"TotalCount":55300,
  67. "Query":"This should "
  68. },
  69. "Images":[{"Title":"Datei:This should.svg",
  70. "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
  71. "SourceUrl":"http://de.wikipedia.org/wiki/Datei:This should.svg",
  72. "DisplayUrl":"de.wikipedia.org/wiki/Datei:This should.svg",
  73. "Width":1280,
  74. "Height":677,
  75. "FileSize":50053,
  76. "ContentType":"image/png",
  77. "Thumbnail":{"Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
  78. "ContentType":"image/png",
  79. "Width":300,
  80. "Height":158,
  81. "FileSize":8023},
  82. "Id":"ae230fd8-a06a-47d6-99d5-e74766d8143a"}]},
  83. environment: "production"
  84. }).then(function (options) {
  85. $('#Search_Form').on('submit', function (e) {
  86. if (!Modernizr.history) return;
  87. e.preventDefault();
  88. var $form = $(this),
  89. $query = $('#Query'),
  90. query = $.trim($query.val()),
  91. path = App.Router.makePath($form.attr('action'), null, $form.serializeObject())
  92. if (query.length) {
  93. options.html5history ?
  94. ReactRouter.HistoryLocation.push(path) :
  95. ReactRouter.RefreshLocation.push(path);
  96. }
  97. else $('#Query').trigger('blur');
  98. });
  99. });
  100. </script>
  101. """
  102. response = mock.Mock(text=html)
  103. results = swisscows.response(response)
  104. self.assertEqual(type(results), list)
  105. self.assertEqual(len(results), 3)
  106. self.assertEqual(results[0]['title'], 'This should be the title')
  107. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  108. self.assertEqual(results[0]['content'], 'This should be the content.')
  109. self.assertEqual(results[1]['title'], 'Datei:This should1.svg')
  110. self.assertEqual(results[1]['url'], 'http://de.wikipedia.org/wiki/Datei:This should1.svg')
  111. self.assertEqual(results[1]['img_src'], 'http://ts2.mm.This/should1.png')
  112. self.assertEqual(results[1]['template'], 'images.html')
  113. self.assertEqual(results[2]['title'], 'Datei:This should.svg')
  114. self.assertEqual(results[2]['url'], 'http://de.wikipedia.org/wiki/Datei:This should.svg')
  115. self.assertEqual(results[2]['img_src'], 'http://ts2.mm.This/should.png')
  116. self.assertEqual(results[2]['template'], 'images.html')
  117. def test_fetch_supported_languages(self):
  118. html = """<html></html>"""
  119. response = mock.Mock(text=html)
  120. languages = swisscows._fetch_supported_languages(response)
  121. self.assertEqual(type(languages), list)
  122. self.assertEqual(len(languages), 0)
  123. html = """
  124. <html>
  125. <div id="regions-popup">
  126. <div>
  127. <ul>
  128. <li><a data-val="browser"></a></li>
  129. <li><a data-val="de-CH"></a></li>
  130. <li><a data-val="fr-CH"></a></li>
  131. </ul>
  132. </div>
  133. </div>
  134. </html>
  135. """
  136. response = mock.Mock(text=html)
  137. languages = swisscows._fetch_supported_languages(response)
  138. self.assertEqual(type(languages), list)
  139. self.assertEqual(len(languages), 3)
  140. self.assertIn('de-CH', languages)
  141. self.assertIn('fr-CH', languages)