123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import spotify
  4. from searx.testing import SearxTestCase
  5. class TestSpotifyEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = spotify.request(query, dicto)
  11. self.assertIn('url', params)
  12. self.assertIn(query, params['url'])
  13. self.assertIn('spotify.com', params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, spotify.response, None)
  16. self.assertRaises(AttributeError, spotify.response, [])
  17. self.assertRaises(AttributeError, spotify.response, '')
  18. self.assertRaises(AttributeError, spotify.response, '[]')
  19. response = mock.Mock(text='{}')
  20. self.assertEqual(spotify.response(response), [])
  21. response = mock.Mock(text='{"data": []}')
  22. self.assertEqual(spotify.response(response), [])
  23. json = """
  24. {
  25. "tracks": {
  26. "href": "https://api.spotify.com/v1/search?query=nosfell&offset=0&limit=20&type=track",
  27. "items": [
  28. {
  29. "album": {
  30. "album_type": "album",
  31. "external_urls": {
  32. "spotify": "https://open.spotify.com/album/5c9ap1PBkSGLxT3J73toxA"
  33. },
  34. "href": "https://api.spotify.com/v1/albums/5c9ap1PBkSGLxT3J73toxA",
  35. "id": "5c9ap1PBkSGLxT3J73toxA",
  36. "name": "Album Title",
  37. "type": "album",
  38. "uri": "spotify:album:5c9ap1PBkSGLxT3J73toxA"
  39. },
  40. "artists": [
  41. {
  42. "external_urls": {
  43. "spotify": "https://open.spotify.com/artist/0bMc6b75FfZEpQHG1jifKu"
  44. },
  45. "href": "https://api.spotify.com/v1/artists/0bMc6b75FfZEpQHG1jifKu",
  46. "id": "0bMc6b75FfZEpQHG1jifKu",
  47. "name": "Artist Name",
  48. "type": "artist",
  49. "uri": "spotify:artist:0bMc6b75FfZEpQHG1jifKu"
  50. }
  51. ],
  52. "disc_number": 1,
  53. "duration_ms": 202386,
  54. "explicit": false,
  55. "external_ids": {
  56. "isrc": "FRV640600067"
  57. },
  58. "external_urls": {
  59. "spotify": "https://open.spotify.com/track/2GzvFiedqW8hgqUpWcASZa"
  60. },
  61. "href": "https://api.spotify.com/v1/tracks/2GzvFiedqW8hgqUpWcASZa",
  62. "id": "1000",
  63. "is_playable": true,
  64. "name": "Title of track",
  65. "popularity": 6,
  66. "preview_url": "https://p.scdn.co/mp3-preview/7b8ecda580965a066b768c2647f877e43f7b1a0a",
  67. "track_number": 3,
  68. "type": "track",
  69. "uri": "spotify:track:2GzvFiedqW8hgqUpWcASZa"
  70. }
  71. ],
  72. "limit": 20,
  73. "next": "https://api.spotify.com/v1/search?query=nosfell&offset=20&limit=20&type=track",
  74. "offset": 0,
  75. "previous": null,
  76. "total": 107
  77. }
  78. }
  79. """
  80. response = mock.Mock(text=json)
  81. results = spotify.response(response)
  82. self.assertEqual(type(results), list)
  83. self.assertEqual(len(results), 1)
  84. self.assertEqual(results[0]['title'], 'Title of track')
  85. self.assertEqual(results[0]['url'], 'https://open.spotify.com/track/2GzvFiedqW8hgqUpWcASZa')
  86. self.assertEqual(results[0]['content'], 'Artist Name - Album Title - Title of track')
  87. self.assertIn('1000', results[0]['embedded'])
  88. json = """
  89. {
  90. "tracks": {
  91. "href": "https://api.spotify.com/v1/search?query=nosfell&offset=0&limit=20&type=track",
  92. "items": [
  93. {
  94. "href": "https://api.spotify.com/v1/tracks/2GzvFiedqW8hgqUpWcASZa",
  95. "id": "1000",
  96. "is_playable": true,
  97. "name": "Title of track",
  98. "popularity": 6,
  99. "preview_url": "https://p.scdn.co/mp3-preview/7b8ecda580965a066b768c2647f877e43f7b1a0a",
  100. "track_number": 3,
  101. "type": "album",
  102. "uri": "spotify:track:2GzvFiedqW8hgqUpWcASZa"
  103. }
  104. ],
  105. "limit": 20,
  106. "next": "https://api.spotify.com/v1/search?query=nosfell&offset=20&limit=20&type=track",
  107. "offset": 0,
  108. "previous": null,
  109. "total": 107
  110. }
  111. }
  112. """
  113. response = mock.Mock(text=json)
  114. results = spotify.response(response)
  115. self.assertEqual(type(results), list)
  116. self.assertEqual(len(results), 0)