test_soundcloud.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import soundcloud
  4. from searx.testing import SearxTestCase
  5. from urllib import quote_plus
  6. class TestSoundcloudEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = soundcloud.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('soundcloud.com', params['url'])
  15. def test_response(self):
  16. self.assertRaises(AttributeError, soundcloud.response, None)
  17. self.assertRaises(AttributeError, soundcloud.response, [])
  18. self.assertRaises(AttributeError, soundcloud.response, '')
  19. self.assertRaises(AttributeError, soundcloud.response, '[]')
  20. response = mock.Mock(text='{}')
  21. self.assertEqual(soundcloud.response(response), [])
  22. response = mock.Mock(text='{"data": []}')
  23. self.assertEqual(soundcloud.response(response), [])
  24. json = """
  25. {
  26. "collection": [
  27. {
  28. "kind": "track",
  29. "id": 159723640,
  30. "created_at": "2014/07/22 00:51:21 +0000",
  31. "user_id": 2976616,
  32. "duration": 303780,
  33. "commentable": true,
  34. "state": "finished",
  35. "original_content_size": 13236349,
  36. "last_modified": "2015/01/31 15:14:50 +0000",
  37. "sharing": "public",
  38. "tag_list": "seekae flume",
  39. "permalink": "seekae-test-recognise-flume-re-work",
  40. "streamable": true,
  41. "embeddable_by": "all",
  42. "downloadable": true,
  43. "purchase_url": "http://www.facebook.com/seekaemusic",
  44. "label_id": null,
  45. "purchase_title": "Seekae",
  46. "genre": "freedownload",
  47. "title": "This is the title",
  48. "description": "This is the content",
  49. "label_name": "Future Classic",
  50. "release": "",
  51. "track_type": "remix",
  52. "key_signature": "",
  53. "isrc": "",
  54. "video_url": null,
  55. "bpm": null,
  56. "release_year": 2014,
  57. "release_month": 7,
  58. "release_day": 22,
  59. "original_format": "mp3",
  60. "license": "all-rights-reserved",
  61. "uri": "https://api.soundcloud.com/tracks/159723640",
  62. "user": {
  63. "id": 2976616,
  64. "kind": "user",
  65. "permalink": "flume",
  66. "username": "Flume",
  67. "last_modified": "2014/11/24 19:21:29 +0000",
  68. "uri": "https://api.soundcloud.com/users/2976616",
  69. "permalink_url": "http://soundcloud.com/flume",
  70. "avatar_url": "https://i1.sndcdn.com/avatars-000044475439-4zi7ii-large.jpg"
  71. },
  72. "permalink_url": "http://soundcloud.com/this.is.the.url",
  73. "artwork_url": "https://i1.sndcdn.com/artworks-000085857162-xdxy5c-large.jpg",
  74. "waveform_url": "https://w1.sndcdn.com/DWrL1lAN8BkP_m.png",
  75. "stream_url": "https://api.soundcloud.com/tracks/159723640/stream",
  76. "download_url": "https://api.soundcloud.com/tracks/159723640/download",
  77. "playback_count": 2190687,
  78. "download_count": 54856,
  79. "favoritings_count": 49061,
  80. "comment_count": 826,
  81. "likes_count": 49061,
  82. "reposts_count": 15910,
  83. "attachments_uri": "https://api.soundcloud.com/tracks/159723640/attachments",
  84. "policy": "ALLOW"
  85. }
  86. ],
  87. "total_results": 375750,
  88. "next_href": "https://api.soundcloud.com/search?&q=test",
  89. "tx_id": ""
  90. }
  91. """
  92. response = mock.Mock(text=json)
  93. results = soundcloud.response(response)
  94. self.assertEqual(type(results), list)
  95. self.assertEqual(len(results), 1)
  96. self.assertEqual(results[0]['title'], 'This is the title')
  97. self.assertEqual(results[0]['url'], 'http://soundcloud.com/this.is.the.url')
  98. self.assertEqual(results[0]['content'], 'This is the content')
  99. self.assertIn(quote_plus('https://api.soundcloud.com/tracks/159723640'), results[0]['embedded'])
  100. json = """
  101. {
  102. "collection": [
  103. {
  104. "kind": "user",
  105. "id": 159723640,
  106. "created_at": "2014/07/22 00:51:21 +0000",
  107. "user_id": 2976616,
  108. "duration": 303780,
  109. "commentable": true,
  110. "state": "finished",
  111. "original_content_size": 13236349,
  112. "last_modified": "2015/01/31 15:14:50 +0000",
  113. "sharing": "public",
  114. "tag_list": "seekae flume",
  115. "permalink": "seekae-test-recognise-flume-re-work",
  116. "streamable": true,
  117. "embeddable_by": "all",
  118. "downloadable": true,
  119. "purchase_url": "http://www.facebook.com/seekaemusic",
  120. "label_id": null,
  121. "purchase_title": "Seekae",
  122. "genre": "freedownload",
  123. "title": "This is the title",
  124. "description": "This is the content",
  125. "label_name": "Future Classic",
  126. "release": "",
  127. "track_type": "remix",
  128. "key_signature": "",
  129. "isrc": "",
  130. "video_url": null,
  131. "bpm": null,
  132. "release_year": 2014,
  133. "release_month": 7,
  134. "release_day": 22,
  135. "original_format": "mp3",
  136. "license": "all-rights-reserved",
  137. "uri": "https://api.soundcloud.com/tracks/159723640",
  138. "user": {
  139. "id": 2976616,
  140. "kind": "user",
  141. "permalink": "flume",
  142. "username": "Flume",
  143. "last_modified": "2014/11/24 19:21:29 +0000",
  144. "uri": "https://api.soundcloud.com/users/2976616",
  145. "permalink_url": "http://soundcloud.com/flume",
  146. "avatar_url": "https://i1.sndcdn.com/avatars-000044475439-4zi7ii-large.jpg"
  147. },
  148. "permalink_url": "http://soundcloud.com/this.is.the.url",
  149. "artwork_url": "https://i1.sndcdn.com/artworks-000085857162-xdxy5c-large.jpg",
  150. "waveform_url": "https://w1.sndcdn.com/DWrL1lAN8BkP_m.png",
  151. "stream_url": "https://api.soundcloud.com/tracks/159723640/stream",
  152. "download_url": "https://api.soundcloud.com/tracks/159723640/download",
  153. "playback_count": 2190687,
  154. "download_count": 54856,
  155. "favoritings_count": 49061,
  156. "comment_count": 826,
  157. "likes_count": 49061,
  158. "reposts_count": 15910,
  159. "attachments_uri": "https://api.soundcloud.com/tracks/159723640/attachments",
  160. "policy": "ALLOW"
  161. }
  162. ],
  163. "total_results": 375750,
  164. "next_href": "https://api.soundcloud.com/search?&q=test",
  165. "tx_id": ""
  166. }
  167. """
  168. response = mock.Mock(text=json)
  169. results = soundcloud.response(response)
  170. self.assertEqual(type(results), list)
  171. self.assertEqual(len(results), 0)
  172. json = """
  173. {
  174. "collection": [],
  175. "total_results": 375750,
  176. "next_href": "https://api.soundcloud.com/search?&q=test",
  177. "tx_id": ""
  178. }
  179. """
  180. response = mock.Mock(text=json)
  181. results = soundcloud.response(response)
  182. self.assertEqual(type(results), list)
  183. self.assertEqual(len(results), 0)