File: test_spotify.py

package info (click to toggle)
beets 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 7,988 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (251 lines) | stat: -rw-r--r-- 7,994 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""Tests for the 'spotify' plugin"""

import os
from urllib.parse import parse_qs, urlparse

import responses

from beets.library import Item
from beets.test import _common
from beets.test.helper import PluginTestCase
from beetsplug import spotify


class ArgumentsMock:
    def __init__(self, mode, show_failures):
        self.mode = mode
        self.show_failures = show_failures
        self.verbose = 1


def _params(url):
    """Get the query parameters from a URL."""
    return parse_qs(urlparse(url).query)


class SpotifyPluginTest(PluginTestCase):
    plugin = "spotify"

    @responses.activate
    def setUp(self):
        responses.add(
            responses.POST,
            spotify.SpotifyPlugin.oauth_token_url,
            status=200,
            json={
                "access_token": "3XyiC3raJySbIAV5LVYj1DaWbcocNi3LAJTNXRnYY"
                "GVUl6mbbqXNhW3YcZnQgYXNWHFkVGSMlc0tMuvq8CF",
                "token_type": "Bearer",
                "expires_in": 3600,
                "scope": "",
            },
        )
        super().setUp()
        self.spotify = spotify.SpotifyPlugin()
        opts = ArgumentsMock("list", False)
        self.spotify._parse_opts(opts)

    def test_args(self):
        opts = ArgumentsMock("fail", True)
        assert not self.spotify._parse_opts(opts)
        opts = ArgumentsMock("list", False)
        assert self.spotify._parse_opts(opts)

    def test_empty_query(self):
        assert self.spotify._match_library_tracks(self.lib, "1=2") is None

    @responses.activate
    def test_missing_request(self):
        json_file = os.path.join(
            _common.RSRC, b"spotify", b"missing_request.json"
        )
        with open(json_file, "rb") as f:
            response_body = f.read()

        responses.add(
            responses.GET,
            spotify.SpotifyPlugin.search_url,
            body=response_body,
            status=200,
            content_type="application/json",
        )
        item = Item(
            mb_trackid="01234",
            album="lkajsdflakjsd",
            albumartist="ujydfsuihse",
            title="duifhjslkef",
            length=10,
        )
        item.add(self.lib)
        assert [] == self.spotify._match_library_tracks(self.lib, "")

        params = _params(responses.calls[0].request.url)
        query = params["q"][0]
        assert "duifhjslkef" in query
        assert "artist:'ujydfsuihse'" in query
        assert "album:'lkajsdflakjsd'" in query
        assert params["type"] == ["track"]

    @responses.activate
    def test_track_request(self):
        json_file = os.path.join(
            _common.RSRC, b"spotify", b"track_request.json"
        )
        with open(json_file, "rb") as f:
            response_body = f.read()

        responses.add(
            responses.GET,
            spotify.SpotifyPlugin.search_url,
            body=response_body,
            status=200,
            content_type="application/json",
        )
        item = Item(
            mb_trackid="01234",
            album="Despicable Me 2",
            albumartist="Pharrell Williams",
            title="Happy",
            length=10,
        )
        item.add(self.lib)
        results = self.spotify._match_library_tracks(self.lib, "Happy")
        assert 1 == len(results)
        assert "6NPVjNh8Jhru9xOmyQigds" == results[0]["id"]
        self.spotify._output_match_results(results)

        params = _params(responses.calls[0].request.url)
        query = params["q"][0]
        assert "Happy" in query
        assert "artist:'Pharrell Williams'" in query
        assert "album:'Despicable Me 2'" in query
        assert params["type"] == ["track"]

    @responses.activate
    def test_track_for_id(self):
        """Tests if plugin is able to fetch a track by its Spotify ID"""

        # Mock the Spotify 'Get Track' call
        json_file = os.path.join(_common.RSRC, b"spotify", b"track_info.json")
        with open(json_file, "rb") as f:
            response_body = f.read()

        responses.add(
            responses.GET,
            f"{spotify.SpotifyPlugin.track_url}6NPVjNh8Jhru9xOmyQigds",
            body=response_body,
            status=200,
            content_type="application/json",
        )

        # Mock the Spotify 'Get Album' call
        json_file = os.path.join(_common.RSRC, b"spotify", b"album_info.json")
        with open(json_file, "rb") as f:
            response_body = f.read()

        responses.add(
            responses.GET,
            f"{spotify.SpotifyPlugin.album_url}5l3zEmMrOhOzG8d8s83GOL",
            body=response_body,
            status=200,
            content_type="application/json",
        )

        # Mock the Spotify 'Search' call
        json_file = os.path.join(
            _common.RSRC, b"spotify", b"track_request.json"
        )
        with open(json_file, "rb") as f:
            response_body = f.read()

        responses.add(
            responses.GET,
            spotify.SpotifyPlugin.search_url,
            body=response_body,
            status=200,
            content_type="application/json",
        )

        track_info = self.spotify.track_for_id("6NPVjNh8Jhru9xOmyQigds")
        item = Item(
            mb_trackid=track_info.track_id,
            albumartist=track_info.artist,
            title=track_info.title,
            length=track_info.length,
        )
        item.add(self.lib)

        results = self.spotify._match_library_tracks(self.lib, "Happy")
        assert 1 == len(results)
        assert "6NPVjNh8Jhru9xOmyQigds" == results[0]["id"]

    @responses.activate
    def test_japanese_track(self):
        """Ensure non-ASCII characters remain unchanged in search queries"""

        # Path to the mock JSON file for the Japanese track
        json_file = os.path.join(
            _common.RSRC, b"spotify", b"japanese_track_request.json"
        )

        # Load the mock JSON response
        with open(json_file, "rb") as f:
            response_body = f.read()

        # Mock Spotify Search API response
        responses.add(
            responses.GET,
            spotify.SpotifyPlugin.search_url,
            body=response_body,
            status=200,
            content_type="application/json",
        )

        # Create a mock item with Japanese metadata
        item = Item(
            mb_trackid="56789",
            album="盗作",
            albumartist="ヨルシカ",
            title="思想犯",
            length=10,
        )
        item.add(self.lib)

        # Search without ascii encoding

        with self.configure_plugin(
            {
                "search_query_ascii": False,
            }
        ):
            assert self.spotify.config["search_query_ascii"].get() is False
            # Call the method to match library tracks
            results = self.spotify._match_library_tracks(self.lib, item.title)

            # Assertions to verify results
            assert results is not None
            assert 1 == len(results)
            assert results[0]["name"] == item.title
            assert results[0]["artists"][0]["name"] == item.albumartist
            assert results[0]["album"]["name"] == item.album

            # Verify search query parameters
            params = _params(responses.calls[0].request.url)
            query = params["q"][0]
            assert item.title in query
            assert f"artist:'{item.albumartist}'" in query
            assert f"album:'{item.album}'" in query
            assert not query.isascii()

        # Is not found in the library if ascii encoding is enabled
        with self.configure_plugin(
            {
                "search_query_ascii": True,
            }
        ):
            assert self.spotify.config["search_query_ascii"].get() is True
            results = self.spotify._match_library_tracks(self.lib, item.title)
            params = _params(responses.calls[1].request.url)
            query = params["q"][0]

            assert query.isascii()