test_flickr_noapi.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import flickr_noapi
  4. from searx.testing import SearxTestCase
  5. class TestFlickrNoapiEngine(SearxTestCase):
  6. def test_build_flickr_url(self):
  7. url = flickr_noapi.build_flickr_url("uid", "pid")
  8. self.assertIn("uid", url)
  9. self.assertIn("pid", url)
  10. def test_request(self):
  11. query = 'test_query'
  12. dicto = defaultdict(dict)
  13. dicto['pageno'] = 1
  14. params = flickr_noapi.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn(query, params['url'])
  17. self.assertIn('flickr.com', params['url'])
  18. def test_response(self):
  19. self.assertRaises(AttributeError, flickr_noapi.response, None)
  20. self.assertRaises(AttributeError, flickr_noapi.response, [])
  21. self.assertRaises(AttributeError, flickr_noapi.response, '')
  22. self.assertRaises(AttributeError, flickr_noapi.response, '[]')
  23. response = mock.Mock(text='"search-photos-models","photos":{},"totalItems":')
  24. self.assertEqual(flickr_noapi.response(response), [])
  25. response = mock.Mock(text='search-photos-models","photos":{"data": []},"totalItems":')
  26. self.assertEqual(flickr_noapi.response(response), [])
  27. json = """
  28. "search-photos-models","photos":
  29. {
  30. "_data": [
  31. {
  32. "_flickrModelRegistry": "photo-models",
  33. "title": "This is the title",
  34. "sizes": {
  35. "c": {
  36. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_c.jpg",
  37. "width": 541,
  38. "height": 800,
  39. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_c.jpg",
  40. "key": "c"
  41. },
  42. "h": {
  43. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_761d32237a_h.jpg",
  44. "width": 1081,
  45. "height": 1600,
  46. "url": "//c4.staticflickr.com/8/7246/14001294434_761d32237a_h.jpg",
  47. "key": "h"
  48. },
  49. "k": {
  50. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_f145a2c11a_k.jpg",
  51. "width": 1383,
  52. "height": 2048,
  53. "url": "//c4.staticflickr.com/8/7246/14001294434_f145a2c11a_k.jpg",
  54. "key": "k"
  55. },
  56. "l": {
  57. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_b.jpg",
  58. "width": 692,
  59. "height": 1024,
  60. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_b.jpg",
  61. "key": "l"
  62. },
  63. "m": {
  64. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777.jpg",
  65. "width": 338,
  66. "height": 500,
  67. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777.jpg",
  68. "key": "m"
  69. },
  70. "n": {
  71. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_n.jpg",
  72. "width": 216,
  73. "height": 320,
  74. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_n.jpg",
  75. "key": "n"
  76. },
  77. "q": {
  78. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_q.jpg",
  79. "width": 150,
  80. "height": 150,
  81. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_q.jpg",
  82. "key": "q"
  83. },
  84. "s": {
  85. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_m.jpg",
  86. "width": 162,
  87. "height": 240,
  88. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_m.jpg",
  89. "key": "s"
  90. },
  91. "sq": {
  92. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_s.jpg",
  93. "width": 75,
  94. "height": 75,
  95. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_s.jpg",
  96. "key": "sq"
  97. },
  98. "t": {
  99. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_t.jpg",
  100. "width": 68,
  101. "height": 100,
  102. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_t.jpg",
  103. "key": "t"
  104. },
  105. "z": {
  106. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
  107. "width": 433,
  108. "height": 640,
  109. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
  110. "key": "z"
  111. }
  112. },
  113. "canComment": false,
  114. "rotation": 0,
  115. "owner": {
  116. "_flickrModelRegistry": "person-models",
  117. "pathAlias": "klink692",
  118. "username": "Owner",
  119. "buddyicon": {
  120. "retina": null,
  121. "large": null,
  122. "medium": null,
  123. "small": null,
  124. "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
  125. },
  126. "isPro": true,
  127. "id": "59729010@N00"
  128. },
  129. "engagement": {
  130. "_flickrModelRegistry": "photo-engagement-models",
  131. "ownerNsid": "59729010@N00",
  132. "faveCount": 21,
  133. "commentCount": 14,
  134. "viewCount": 10160,
  135. "id": "14001294434"
  136. },
  137. "description": "Description",
  138. "isHD": false,
  139. "secret": "410f653777",
  140. "canAddMeta": false,
  141. "license": 0,
  142. "oWidth": 1803,
  143. "oHeight": 2669,
  144. "safetyLevel": 0,
  145. "id": "14001294434"
  146. }
  147. ],
  148. "fetchedStart": true,
  149. "fetchedEnd": false,
  150. "totalItems": "4386039"
  151. },"totalItems":
  152. """
  153. json = json.replace('\r\n', '').replace('\n', '').replace('\r', '')
  154. response = mock.Mock(text=json)
  155. results = flickr_noapi.response(response)
  156. self.assertEqual(type(results), list)
  157. self.assertEqual(len(results), 1)
  158. self.assertEqual(results[0]['title'], 'This is the title')
  159. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  160. self.assertIn('k.jpg', results[0]['img_src'])
  161. self.assertIn('n.jpg', results[0]['thumbnail_src'])
  162. self.assertIn('Owner', results[0]['content'])
  163. self.assertIn('Description', results[0]['content'])
  164. json = """
  165. "search-photos-models","photos":
  166. {
  167. "_data": [
  168. {
  169. "_flickrModelRegistry": "photo-models",
  170. "title": "This is the title",
  171. "sizes": {
  172. "z": {
  173. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
  174. "width": 433,
  175. "height": 640,
  176. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
  177. "key": "z"
  178. }
  179. },
  180. "canComment": false,
  181. "rotation": 0,
  182. "owner": {
  183. "_flickrModelRegistry": "person-models",
  184. "pathAlias": "klink692",
  185. "username": "Owner",
  186. "buddyicon": {
  187. "retina": null,
  188. "large": null,
  189. "medium": null,
  190. "small": null,
  191. "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
  192. },
  193. "isPro": true,
  194. "id": "59729010@N00"
  195. },
  196. "engagement": {
  197. "_flickrModelRegistry": "photo-engagement-models",
  198. "ownerNsid": "59729010@N00",
  199. "faveCount": 21,
  200. "commentCount": 14,
  201. "viewCount": 10160,
  202. "id": "14001294434"
  203. },
  204. "description": "Description",
  205. "isHD": false,
  206. "secret": "410f653777",
  207. "canAddMeta": false,
  208. "license": 0,
  209. "oWidth": 1803,
  210. "oHeight": 2669,
  211. "safetyLevel": 0,
  212. "id": "14001294434"
  213. }
  214. ],
  215. "fetchedStart": true,
  216. "fetchedEnd": false,
  217. "totalItems": "4386039"
  218. },"totalItems":
  219. """
  220. response = mock.Mock(text=json)
  221. results = flickr_noapi.response(response)
  222. self.assertEqual(type(results), list)
  223. self.assertEqual(len(results), 1)
  224. self.assertEqual(results[0]['title'], 'This is the title')
  225. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  226. self.assertIn('z.jpg', results[0]['img_src'])
  227. self.assertIn('z.jpg', results[0]['thumbnail_src'])
  228. self.assertIn('Owner', results[0]['content'])
  229. self.assertIn('Description', results[0]['content'])
  230. json = """
  231. "search-photos-models","photos":
  232. {
  233. "_data": [
  234. {
  235. "_flickrModelRegistry": "photo-models",
  236. "title": "This is the title",
  237. "sizes": {
  238. "o": {
  239. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
  240. "width": 433,
  241. "height": 640,
  242. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
  243. "key": "o"
  244. }
  245. },
  246. "canComment": false,
  247. "rotation": 0,
  248. "owner": {
  249. "_flickrModelRegistry": "person-models",
  250. "pathAlias": "klink692",
  251. "username": "Owner",
  252. "buddyicon": {
  253. "retina": null,
  254. "large": null,
  255. "medium": null,
  256. "small": null,
  257. "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
  258. },
  259. "isPro": true,
  260. "id": "59729010@N00"
  261. },
  262. "engagement": {
  263. "_flickrModelRegistry": "photo-engagement-models",
  264. "ownerNsid": "59729010@N00",
  265. "faveCount": 21,
  266. "commentCount": 14,
  267. "viewCount": 10160,
  268. "id": "14001294434"
  269. },
  270. "isHD": false,
  271. "secret": "410f653777",
  272. "canAddMeta": false,
  273. "license": 0,
  274. "oWidth": 1803,
  275. "oHeight": 2669,
  276. "safetyLevel": 0,
  277. "id": "14001294434"
  278. }
  279. ],
  280. "fetchedStart": true,
  281. "fetchedEnd": false,
  282. "totalItems": "4386039"
  283. },"totalItems":
  284. """
  285. response = mock.Mock(text=json)
  286. results = flickr_noapi.response(response)
  287. self.assertEqual(type(results), list)
  288. self.assertEqual(len(results), 1)
  289. self.assertEqual(results[0]['title'], 'This is the title')
  290. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  291. self.assertIn('o.jpg', results[0]['img_src'])
  292. self.assertIn('o.jpg', results[0]['thumbnail_src'])
  293. self.assertIn('Owner', results[0]['content'])
  294. json = """
  295. "search-photos-models","photos":
  296. {
  297. "_data": [
  298. {
  299. "_flickrModelRegistry": "photo-models",
  300. "title": "This is the title",
  301. "sizes": {
  302. },
  303. "canComment": false,
  304. "rotation": 0,
  305. "owner": {
  306. "_flickrModelRegistry": "person-models",
  307. "pathAlias": "klink692",
  308. "username": "Owner",
  309. "buddyicon": {
  310. "retina": null,
  311. "large": null,
  312. "medium": null,
  313. "small": null,
  314. "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
  315. },
  316. "isPro": true,
  317. "id": "59729010@N00"
  318. },
  319. "engagement": {
  320. "_flickrModelRegistry": "photo-engagement-models",
  321. "ownerNsid": "59729010@N00",
  322. "faveCount": 21,
  323. "commentCount": 14,
  324. "viewCount": 10160,
  325. "id": "14001294434"
  326. },
  327. "description": "Description",
  328. "isHD": false,
  329. "secret": "410f653777",
  330. "canAddMeta": false,
  331. "license": 0,
  332. "oWidth": 1803,
  333. "oHeight": 2669,
  334. "safetyLevel": 0,
  335. "id": "14001294434"
  336. }
  337. ],
  338. "fetchedStart": true,
  339. "fetchedEnd": false,
  340. "totalItems": "4386039"
  341. },"totalItems":
  342. """
  343. response = mock.Mock(text=json)
  344. results = flickr_noapi.response(response)
  345. self.assertEqual(type(results), list)
  346. self.assertEqual(len(results), 0)
  347. json = """
  348. "search-photos-models","photos":
  349. {
  350. "_data": [null],
  351. "fetchedStart": true,
  352. "fetchedEnd": false,
  353. "totalItems": "4386039"
  354. },"totalItems":
  355. """
  356. response = mock.Mock(text=json)
  357. results = flickr_noapi.response(response)
  358. self.assertEqual(type(results), list)
  359. self.assertEqual(len(results), 0)
  360. json = """
  361. "search-photos-models","photos":
  362. {
  363. "_data": [
  364. {
  365. "_flickrModelRegistry": "photo-models",
  366. "title": "This is the title",
  367. "sizes": {
  368. "o": {
  369. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
  370. "width": 433,
  371. "height": 640,
  372. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
  373. "key": "o"
  374. }
  375. },
  376. "canComment": false,
  377. "rotation": 0,
  378. "owner": {
  379. "_flickrModelRegistry": "person-models",
  380. "pathAlias": "klink692",
  381. "username": "Owner",
  382. "buddyicon": {
  383. "retina": null,
  384. "large": null,
  385. "medium": null,
  386. "small": null,
  387. "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
  388. },
  389. "isPro": true
  390. },
  391. "engagement": {
  392. "_flickrModelRegistry": "photo-engagement-models",
  393. "ownerNsid": "59729010@N00",
  394. "faveCount": 21,
  395. "commentCount": 14,
  396. "viewCount": 10160,
  397. "id": "14001294434"
  398. },
  399. "description": "Description",
  400. "isHD": false,
  401. "secret": "410f653777",
  402. "canAddMeta": false,
  403. "license": 0,
  404. "oWidth": 1803,
  405. "oHeight": 2669,
  406. "safetyLevel": 0,
  407. "id": "14001294434"
  408. }
  409. ],
  410. "fetchedStart": true,
  411. "fetchedEnd": false,
  412. "totalItems": "4386039"
  413. },"totalItems":
  414. """
  415. response = mock.Mock(text=json)
  416. results = flickr_noapi.response(response)
  417. self.assertEqual(type(results), list)
  418. self.assertEqual(len(results), 0)
  419. json = """
  420. {"toto":[
  421. {"id":200,"name":"Artist Name",
  422. "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
  423. ]}
  424. """
  425. response = mock.Mock(text=json)
  426. results = flickr_noapi.response(response)
  427. self.assertEqual(type(results), list)
  428. self.assertEqual(len(results), 0)