test_swisscows.py 6.8KB

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