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