test_flickr_noapi.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. dicto['time_range'] = ''
  15. params = flickr_noapi.request(query, dicto)
  16. self.assertIn('url', params)
  17. self.assertIn(query, params['url'])
  18. self.assertIn('flickr.com', params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, flickr_noapi.response, None)
  21. self.assertRaises(AttributeError, flickr_noapi.response, [])
  22. self.assertRaises(AttributeError, flickr_noapi.response, '')
  23. self.assertRaises(AttributeError, flickr_noapi.response, '[]')
  24. response = mock.Mock(text='"search-photos-lite-models","photos":{},"totalItems":')
  25. self.assertEqual(flickr_noapi.response(response), [])
  26. response = mock.Mock(text='search-photos-lite-models","photos":{"data": []},"totalItems":')
  27. self.assertEqual(flickr_noapi.response(response), [])
  28. # everthing is ok test
  29. json = """
  30. "search-photos-lite-models","photos":
  31. {
  32. "_data": [
  33. {
  34. "_flickrModelRegistry": "photo-lite-models",
  35. "title": "This is the title",
  36. "username": "Owner",
  37. "pathAlias": "klink692",
  38. "realname": "Owner",
  39. "license": 0,
  40. "ownerNsid": "59729010@N00",
  41. "canComment": false,
  42. "commentCount": 14,
  43. "faveCount": 21,
  44. "id": "14001294434",
  45. "sizes": {
  46. "c": {
  47. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_c.jpg",
  48. "width": 541,
  49. "height": 800,
  50. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_c.jpg",
  51. "key": "c"
  52. },
  53. "h": {
  54. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_761d32237a_h.jpg",
  55. "width": 1081,
  56. "height": 1600,
  57. "url": "//c4.staticflickr.com/8/7246/14001294434_761d32237a_h.jpg",
  58. "key": "h"
  59. },
  60. "k": {
  61. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_f145a2c11a_k.jpg",
  62. "width": 1383,
  63. "height": 2048,
  64. "url": "//c4.staticflickr.com/8/7246/14001294434_f145a2c11a_k.jpg",
  65. "key": "k"
  66. },
  67. "l": {
  68. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_b.jpg",
  69. "width": 692,
  70. "height": 1024,
  71. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_b.jpg",
  72. "key": "l"
  73. },
  74. "m": {
  75. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777.jpg",
  76. "width": 338,
  77. "height": 500,
  78. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777.jpg",
  79. "key": "m"
  80. },
  81. "n": {
  82. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_n.jpg",
  83. "width": 216,
  84. "height": 320,
  85. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_n.jpg",
  86. "key": "n"
  87. },
  88. "q": {
  89. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_q.jpg",
  90. "width": 150,
  91. "height": 150,
  92. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_q.jpg",
  93. "key": "q"
  94. },
  95. "s": {
  96. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_m.jpg",
  97. "width": 162,
  98. "height": 240,
  99. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_m.jpg",
  100. "key": "s"
  101. },
  102. "sq": {
  103. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_s.jpg",
  104. "width": 75,
  105. "height": 75,
  106. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_s.jpg",
  107. "key": "sq"
  108. },
  109. "t": {
  110. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_t.jpg",
  111. "width": 68,
  112. "height": 100,
  113. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_t.jpg",
  114. "key": "t"
  115. },
  116. "z": {
  117. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
  118. "width": 433,
  119. "height": 640,
  120. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
  121. "key": "z"
  122. }
  123. }
  124. }
  125. ],
  126. "fetchedStart": true,
  127. "fetchedEnd": false,
  128. "totalItems": "4386039"
  129. },"totalItems":
  130. """
  131. json = json.replace('\r\n', '').replace('\n', '').replace('\r', '')
  132. response = mock.Mock(text=json)
  133. results = flickr_noapi.response(response)
  134. self.assertEqual(type(results), list)
  135. self.assertEqual(len(results), 1)
  136. self.assertEqual(results[0]['title'], 'This is the title')
  137. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  138. self.assertIn('k.jpg', results[0]['img_src'])
  139. self.assertIn('n.jpg', results[0]['thumbnail_src'])
  140. self.assertIn('Owner', results[0]['author'])
  141. # no n size, only the z size
  142. json = """
  143. "search-photos-lite-models","photos":
  144. {
  145. "_data": [
  146. {
  147. "_flickrModelRegistry": "photo-lite-models",
  148. "title": "This is the title",
  149. "username": "Owner",
  150. "pathAlias": "klink692",
  151. "realname": "Owner",
  152. "license": 0,
  153. "ownerNsid": "59729010@N00",
  154. "canComment": false,
  155. "commentCount": 14,
  156. "faveCount": 21,
  157. "id": "14001294434",
  158. "sizes": {
  159. "z": {
  160. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
  161. "width": 433,
  162. "height": 640,
  163. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
  164. "key": "z"
  165. }
  166. }
  167. }
  168. ],
  169. "fetchedStart": true,
  170. "fetchedEnd": false,
  171. "totalItems": "4386039"
  172. },"totalItems":
  173. """
  174. response = mock.Mock(text=json)
  175. results = flickr_noapi.response(response)
  176. self.assertEqual(type(results), list)
  177. self.assertEqual(len(results), 1)
  178. self.assertEqual(results[0]['title'], 'This is the title')
  179. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  180. self.assertIn('z.jpg', results[0]['img_src'])
  181. self.assertIn('z.jpg', results[0]['thumbnail_src'])
  182. self.assertIn('Owner', results[0]['author'])
  183. # no z or n size
  184. json = """
  185. "search-photos-lite-models","photos":
  186. {
  187. "_data": [
  188. {
  189. "_flickrModelRegistry": "photo-lite-models",
  190. "title": "This is the title",
  191. "username": "Owner",
  192. "pathAlias": "klink692",
  193. "realname": "Owner",
  194. "license": 0,
  195. "ownerNsid": "59729010@N00",
  196. "canComment": false,
  197. "commentCount": 14,
  198. "faveCount": 21,
  199. "id": "14001294434",
  200. "sizes": {
  201. "o": {
  202. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
  203. "width": 433,
  204. "height": 640,
  205. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
  206. "key": "o"
  207. }
  208. }
  209. }
  210. ],
  211. "fetchedStart": true,
  212. "fetchedEnd": false,
  213. "totalItems": "4386039"
  214. },"totalItems":
  215. """
  216. response = mock.Mock(text=json)
  217. results = flickr_noapi.response(response)
  218. self.assertEqual(type(results), list)
  219. self.assertEqual(len(results), 1)
  220. self.assertEqual(results[0]['title'], 'This is the title')
  221. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  222. self.assertIn('o.jpg', results[0]['img_src'])
  223. self.assertIn('o.jpg', results[0]['thumbnail_src'])
  224. self.assertIn('Owner', results[0]['author'])
  225. # no image test
  226. json = """
  227. "search-photos-lite-models","photos":
  228. {
  229. "_data": [
  230. {
  231. "_flickrModelRegistry": "photo-lite-models",
  232. "title": "This is the title",
  233. "username": "Owner",
  234. "pathAlias": "klink692",
  235. "realname": "Owner",
  236. "license": 0,
  237. "ownerNsid": "59729010@N00",
  238. "canComment": false,
  239. "commentCount": 14,
  240. "faveCount": 21,
  241. "id": "14001294434",
  242. "sizes": {
  243. }
  244. }
  245. ],
  246. "fetchedStart": true,
  247. "fetchedEnd": false,
  248. "totalItems": "4386039"
  249. },"totalItems":
  250. """
  251. response = mock.Mock(text=json)
  252. results = flickr_noapi.response(response)
  253. self.assertEqual(type(results), list)
  254. self.assertEqual(len(results), 0)
  255. # null test
  256. json = """
  257. "search-photos-models","photos":
  258. {
  259. "_data": [null],
  260. "fetchedStart": true,
  261. "fetchedEnd": false,
  262. "totalItems": "4386039"
  263. },"totalItems":
  264. """
  265. response = mock.Mock(text=json)
  266. results = flickr_noapi.response(response)
  267. self.assertEqual(type(results), list)
  268. self.assertEqual(len(results), 0)
  269. # no ownerNsid test
  270. json = """
  271. "search-photos-lite-models","photos":
  272. {
  273. "_data": [
  274. {
  275. "_flickrModelRegistry": "photo-lite-models",
  276. "title": "This is the title",
  277. "username": "Owner",
  278. "pathAlias": "klink692",
  279. "realname": "Owner",
  280. "license": 0,
  281. "canComment": false,
  282. "commentCount": 14,
  283. "faveCount": 21,
  284. "id": "14001294434",
  285. "sizes": {
  286. "o": {
  287. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
  288. "width": 433,
  289. "height": 640,
  290. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
  291. "key": "o"
  292. }
  293. }
  294. }
  295. ],
  296. "fetchedStart": true,
  297. "fetchedEnd": false,
  298. "totalItems": "4386039"
  299. },"totalItems":
  300. """
  301. response = mock.Mock(text=json)
  302. results = flickr_noapi.response(response)
  303. self.assertEqual(type(results), list)
  304. self.assertEqual(len(results), 0)
  305. # garbage test
  306. json = r"""
  307. {"toto":[
  308. {"id":200,"name":"Artist Name",
  309. "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
  310. ]}
  311. """
  312. response = mock.Mock(text=json)
  313. results = flickr_noapi.response(response)
  314. self.assertEqual(type(results), list)
  315. self.assertEqual(len(results), 0)