test_flickr_noapi.py 12 KB

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