test_swisscows.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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(content='<html></html>')
  30. self.assertEqual(swisscows.response(response), [])
  31. response = mock.Mock(content='<html></html>')
  32. self.assertEqual(swisscows.response(response), [])
  33. html = u"""
  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(content=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')