123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import qwant
  4. from searx.testing import SearxTestCase
  5. class TestQwantEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'fr-FR'
  11. qwant.categories = ['']
  12. params = qwant.request(query, dicto)
  13. self.assertIn('url', params)
  14. self.assertIn(query, params['url'])
  15. self.assertIn('web', params['url'])
  16. self.assertIn('qwant.com', params['url'])
  17. self.assertIn('fr_fr', params['url'])
  18. dicto['language'] = 'all'
  19. qwant.categories = ['news']
  20. params = qwant.request(query, dicto)
  21. self.assertFalse('fr' in params['url'])
  22. self.assertIn('news', params['url'])
  23. qwant.supported_languages = ['en', 'fr-FR', 'fr-CA']
  24. dicto['language'] = 'fr'
  25. params = qwant.request(query, dicto)
  26. self.assertIn('fr_fr', params['url'])
  27. def test_response(self):
  28. self.assertRaises(AttributeError, qwant.response, None)
  29. self.assertRaises(AttributeError, qwant.response, [])
  30. self.assertRaises(AttributeError, qwant.response, '')
  31. self.assertRaises(AttributeError, qwant.response, '[]')
  32. response = mock.Mock(text='{}')
  33. self.assertEqual(qwant.response(response), [])
  34. response = mock.Mock(text='{"data": {}}')
  35. self.assertEqual(qwant.response(response), [])
  36. json = """
  37. {
  38. "status": "success",
  39. "data": {
  40. "query": {
  41. "locale": "en_us",
  42. "query": "Test",
  43. "offset": 10
  44. },
  45. "result": {
  46. "items": [
  47. {
  48. "title": "Title",
  49. "score": 9999,
  50. "url": "http://www.url.xyz",
  51. "source": "...",
  52. "desc": "Description",
  53. "date": "",
  54. "_id": "db0aadd62c2a8565567ffc382f5c61fa",
  55. "favicon": "https://s.qwant.com/fav.ico"
  56. }
  57. ],
  58. "filters": []
  59. },
  60. "cache": {
  61. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  62. "created": 1433092754,
  63. "expiration": 259200,
  64. "status": "miss",
  65. "age": 0
  66. }
  67. }
  68. }
  69. """
  70. response = mock.Mock(text=json)
  71. qwant.categories = ['general']
  72. results = qwant.response(response)
  73. self.assertEqual(type(results), list)
  74. self.assertEqual(len(results), 1)
  75. self.assertEqual(results[0]['title'], 'Title')
  76. self.assertEqual(results[0]['url'], 'http://www.url.xyz')
  77. self.assertEqual(results[0]['content'], 'Description')
  78. json = """
  79. {
  80. "status": "success",
  81. "data": {
  82. "query": {
  83. "locale": "en_us",
  84. "query": "Test",
  85. "offset": 10
  86. },
  87. "result": {
  88. "items": [
  89. {
  90. "title": "Title",
  91. "score": 9999,
  92. "url": "http://www.url.xyz",
  93. "source": "...",
  94. "media": "http://image.jpg",
  95. "desc": "",
  96. "thumbnail": "http://thumbnail.jpg",
  97. "date": "",
  98. "_id": "db0aadd62c2a8565567ffc382f5c61fa",
  99. "favicon": "https://s.qwant.com/fav.ico"
  100. }
  101. ],
  102. "filters": []
  103. },
  104. "cache": {
  105. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  106. "created": 1433092754,
  107. "expiration": 259200,
  108. "status": "miss",
  109. "age": 0
  110. }
  111. }
  112. }
  113. """
  114. response = mock.Mock(text=json)
  115. qwant.categories = ['images']
  116. results = qwant.response(response)
  117. self.assertEqual(type(results), list)
  118. self.assertEqual(len(results), 1)
  119. self.assertEqual(results[0]['title'], 'Title')
  120. self.assertEqual(results[0]['url'], 'http://www.url.xyz')
  121. self.assertEqual(results[0]['content'], '')
  122. self.assertEqual(results[0]['thumbnail_src'], 'http://thumbnail.jpg')
  123. self.assertEqual(results[0]['img_src'], 'http://image.jpg')
  124. json = """
  125. {
  126. "status": "success",
  127. "data": {
  128. "query": {
  129. "locale": "en_us",
  130. "query": "Test",
  131. "offset": 10
  132. },
  133. "result": {
  134. "items": [
  135. {
  136. "title": "Title",
  137. "score": 9999,
  138. "url": "http://www.url.xyz",
  139. "source": "...",
  140. "desc": "Description",
  141. "date": 1433260920,
  142. "_id": "db0aadd62c2a8565567ffc382f5c61fa",
  143. "favicon": "https://s.qwant.com/fav.ico"
  144. }
  145. ],
  146. "filters": []
  147. },
  148. "cache": {
  149. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  150. "created": 1433092754,
  151. "expiration": 259200,
  152. "status": "miss",
  153. "age": 0
  154. }
  155. }
  156. }
  157. """
  158. response = mock.Mock(text=json)
  159. qwant.categories = ['news']
  160. results = qwant.response(response)
  161. self.assertEqual(type(results), list)
  162. self.assertEqual(len(results), 1)
  163. self.assertEqual(results[0]['title'], 'Title')
  164. self.assertEqual(results[0]['url'], 'http://www.url.xyz')
  165. self.assertEqual(results[0]['content'], 'Description')
  166. self.assertIn('publishedDate', results[0])
  167. json = """
  168. {
  169. "status": "success",
  170. "data": {
  171. "query": {
  172. "locale": "en_us",
  173. "query": "Test",
  174. "offset": 10
  175. },
  176. "result": {
  177. "items": [
  178. {
  179. "title": "Title",
  180. "score": 9999,
  181. "url": "http://www.url.xyz",
  182. "source": "...",
  183. "desc": "Description",
  184. "date": 1433260920,
  185. "_id": "db0aadd62c2a8565567ffc382f5c61fa",
  186. "favicon": "https://s.qwant.com/fav.ico"
  187. }
  188. ],
  189. "filters": []
  190. },
  191. "cache": {
  192. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  193. "created": 1433092754,
  194. "expiration": 259200,
  195. "status": "miss",
  196. "age": 0
  197. }
  198. }
  199. }
  200. """
  201. response = mock.Mock(text=json)
  202. qwant.categories = ['social media']
  203. results = qwant.response(response)
  204. self.assertEqual(type(results), list)
  205. self.assertEqual(len(results), 1)
  206. self.assertEqual(results[0]['title'], 'Title')
  207. self.assertEqual(results[0]['url'], 'http://www.url.xyz')
  208. self.assertEqual(results[0]['content'], 'Description')
  209. self.assertIn('publishedDate', results[0])
  210. json = """
  211. {
  212. "status": "success",
  213. "data": {
  214. "query": {
  215. "locale": "en_us",
  216. "query": "Test",
  217. "offset": 10
  218. },
  219. "result": {
  220. "items": [
  221. {
  222. "title": "Title",
  223. "score": 9999,
  224. "url": "http://www.url.xyz",
  225. "source": "...",
  226. "desc": "Description",
  227. "date": 1433260920,
  228. "_id": "db0aadd62c2a8565567ffc382f5c61fa",
  229. "favicon": "https://s.qwant.com/fav.ico"
  230. }
  231. ],
  232. "filters": []
  233. },
  234. "cache": {
  235. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  236. "created": 1433092754,
  237. "expiration": 259200,
  238. "status": "miss",
  239. "age": 0
  240. }
  241. }
  242. }
  243. """
  244. response = mock.Mock(text=json)
  245. qwant.categories = ['']
  246. results = qwant.response(response)
  247. self.assertEqual(type(results), list)
  248. self.assertEqual(len(results), 0)
  249. json = """
  250. {
  251. "status": "success",
  252. "data": {
  253. "query": {
  254. "locale": "en_us",
  255. "query": "Test",
  256. "offset": 10
  257. },
  258. "result": {
  259. "filters": []
  260. },
  261. "cache": {
  262. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  263. "created": 1433092754,
  264. "expiration": 259200,
  265. "status": "miss",
  266. "age": 0
  267. }
  268. }
  269. }
  270. """
  271. response = mock.Mock(text=json)
  272. results = qwant.response(response)
  273. self.assertEqual(type(results), list)
  274. self.assertEqual(len(results), 0)
  275. json = """
  276. {
  277. "status": "success",
  278. "data": {
  279. "query": {
  280. "locale": "en_us",
  281. "query": "Test",
  282. "offset": 10
  283. },
  284. "cache": {
  285. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  286. "created": 1433092754,
  287. "expiration": 259200,
  288. "status": "miss",
  289. "age": 0
  290. }
  291. }
  292. }
  293. """
  294. response = mock.Mock(text=json)
  295. results = qwant.response(response)
  296. self.assertEqual(type(results), list)
  297. self.assertEqual(len(results), 0)
  298. json = """
  299. {
  300. "status": "success"
  301. }
  302. """
  303. response = mock.Mock(text=json)
  304. results = qwant.response(response)
  305. self.assertEqual(type(results), list)
  306. self.assertEqual(len(results), 0)
  307. def test_fetch_supported_languages(self):
  308. page = """some code...
  309. config_set('project.regionalisation', {"continents":{},"languages":
  310. {"de":{"code":"de","name":"Deutsch","countries":["DE","CH","AT"]},
  311. "it":{"code":"it","name":"Italiano","countries":["IT","CH"]}}});
  312. some more code..."""
  313. response = mock.Mock(text=page)
  314. languages = qwant._fetch_supported_languages(response)
  315. self.assertEqual(type(languages), list)
  316. self.assertEqual(len(languages), 5)
  317. self.assertIn('de-DE', languages)
  318. self.assertIn('de-CH', languages)
  319. self.assertIn('de-AT', languages)
  320. self.assertIn('it-IT', languages)
  321. self.assertIn('it-CH', languages)