test_qwant.py 9.6KB

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