123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import flickr
  4. from searx.testing import SearxTestCase
  5. class TestFlickrEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = flickr.request(query, dicto)
  11. self.assertTrue('url' in params)
  12. self.assertTrue(query in params['url'])
  13. self.assertTrue('flickr.com' in params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, flickr.response, None)
  16. self.assertRaises(AttributeError, flickr.response, [])
  17. self.assertRaises(AttributeError, flickr.response, '')
  18. self.assertRaises(AttributeError, flickr.response, '[]')
  19. response = mock.Mock(text='{}')
  20. self.assertEqual(flickr.response(response), [])
  21. response = mock.Mock(text='{"data": []}')
  22. self.assertEqual(flickr.response(response), [])
  23. json = r"""
  24. { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
  25. "photo": [
  26. { "id": "15751017054", "owner": "66847915@N08",
  27. "secret": "69c22afc40", "server": "7285", "farm": 8,
  28. "title": "Photo title", "ispublic": 1,
  29. "isfriend": 0, "isfamily": 0,
  30. "description": { "_content": "Description" },
  31. "ownername": "Owner",
  32. "url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
  33. "height_o": "2100", "width_o": "2653",
  34. "url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
  35. "height_n": "253", "width_n": "320",
  36. "url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
  37. "height_z": "507", "width_z": "640" }
  38. ] }, "stat": "ok" }
  39. """
  40. response = mock.Mock(text=json)
  41. results = flickr.response(response)
  42. self.assertEqual(type(results), list)
  43. self.assertEqual(len(results), 1)
  44. self.assertEqual(results[0]['title'], 'Photo title')
  45. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
  46. self.assertTrue('o.jpg' in results[0]['img_src'])
  47. self.assertTrue('n.jpg' in results[0]['thumbnail_src'])
  48. self.assertTrue('Owner' in results[0]['content'])
  49. self.assertTrue('Description' in results[0]['content'])
  50. json = r"""
  51. { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
  52. "photo": [
  53. { "id": "15751017054", "owner": "66847915@N08",
  54. "secret": "69c22afc40", "server": "7285", "farm": 8,
  55. "title": "Photo title", "ispublic": 1,
  56. "isfriend": 0, "isfamily": 0,
  57. "description": { "_content": "Description" },
  58. "ownername": "Owner",
  59. "url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
  60. "height_z": "507", "width_z": "640" }
  61. ] }, "stat": "ok" }
  62. """
  63. response = mock.Mock(text=json)
  64. results = flickr.response(response)
  65. self.assertEqual(type(results), list)
  66. self.assertEqual(len(results), 1)
  67. self.assertEqual(results[0]['title'], 'Photo title')
  68. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
  69. self.assertTrue('z.jpg' in results[0]['img_src'])
  70. self.assertTrue('z.jpg' in results[0]['thumbnail_src'])
  71. self.assertTrue('Owner' in results[0]['content'])
  72. self.assertTrue('Description' in results[0]['content'])
  73. json = r"""
  74. { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
  75. "photo": [
  76. { "id": "15751017054", "owner": "66847915@N08",
  77. "secret": "69c22afc40", "server": "7285", "farm": 8,
  78. "title": "Photo title", "ispublic": 1,
  79. "isfriend": 0, "isfamily": 0,
  80. "description": { "_content": "Description" },
  81. "ownername": "Owner",
  82. "url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
  83. "height_o": "2100", "width_o": "2653" }
  84. ] }, "stat": "ok" }
  85. """
  86. response = mock.Mock(text=json)
  87. results = flickr.response(response)
  88. self.assertEqual(type(results), list)
  89. self.assertEqual(len(results), 1)
  90. self.assertEqual(results[0]['title'], 'Photo title')
  91. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
  92. self.assertTrue('o.jpg' in results[0]['img_src'])
  93. self.assertTrue('o.jpg' in results[0]['thumbnail_src'])
  94. self.assertTrue('Owner' in results[0]['content'])
  95. self.assertTrue('Description' in results[0]['content'])
  96. json = r"""
  97. { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
  98. "photo": [
  99. { "id": "15751017054", "owner": "66847915@N08",
  100. "secret": "69c22afc40", "server": "7285", "farm": 8,
  101. "title": "Photo title", "ispublic": 1,
  102. "isfriend": 0, "isfamily": 0,
  103. "description": { "_content": "Description" },
  104. "ownername": "Owner",
  105. "url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
  106. "height_n": "253", "width_n": "320" }
  107. ] }, "stat": "ok" }
  108. """
  109. response = mock.Mock(text=json)
  110. results = flickr.response(response)
  111. self.assertEqual(type(results), list)
  112. self.assertEqual(len(results), 0)
  113. json = """
  114. { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
  115. "toto": [] }, "stat": "ok" }
  116. """
  117. response = mock.Mock(text=json)
  118. results = flickr.response(response)
  119. self.assertEqual(type(results), list)
  120. self.assertEqual(len(results), 0)
  121. json = r"""
  122. {"toto":[
  123. {"id":200,"name":"Artist Name",
  124. "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
  125. ]}
  126. """
  127. response = mock.Mock(text=json)
  128. results = flickr.response(response)
  129. self.assertEqual(type(results), list)
  130. self.assertEqual(len(results), 0)