Skip to content

Commit 8eb583e

Browse files
author
Nick Steel
committed
search: Set market parameter to user's country. Fixes mopidy#97
1 parent af6994d commit 8eb583e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

mopidy_spotify/search.py

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def search(config, session, web_client,
5656
result = web_client.get(_API_BASE_URI, params={
5757
'q': sp_query,
5858
'limit': search_count,
59+
'market': session.user_country,
5960
'type': ','.join(types)})
6061

6162
albums = [

tests/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def session_mock():
366366
sp_session_mock = mock.Mock(spec=spotify.Session)
367367
sp_session_mock.connection.state = spotify.ConnectionState.LOGGED_IN
368368
sp_session_mock.playlist_container = []
369+
sp_session_mock.user_country = 'GB'
369370
return sp_session_mock
370371

371372

tests/test_search.py

+21
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def test_search_returns_albums_and_artists_and_tracks(
7777
params={
7878
'q': '"ABBA"',
7979
'limit': 50,
80+
'market': 'GB',
8081
'type': 'album,artist,track'})
8182

8283
assert 'Searching Spotify for: "ABBA"' in caplog.text()
@@ -124,6 +125,7 @@ def test_sets_api_limit_to_album_count_when_max(
124125
params={
125126
'q': '"ABBA"',
126127
'limit': 6,
128+
'market': 'GB',
127129
'type': 'album,artist,track'})
128130

129131
assert len(result.albums) == 6
@@ -144,6 +146,7 @@ def test_sets_api_limit_to_artist_count_when_max(
144146
params={
145147
'q': '"ABBA"',
146148
'limit': 6,
149+
'market': 'GB',
147150
'type': 'album,artist,track'})
148151

149152
assert len(result.artists) == 6
@@ -164,6 +167,7 @@ def test_sets_api_limit_to_track_count_when_max(
164167
params={
165168
'q': '"ABBA"',
166169
'limit': 6,
170+
'market': 'GB',
167171
'type': 'album,artist,track'})
168172

169173
assert len(result.tracks) == 6
@@ -183,9 +187,26 @@ def test_sets_types_parameter(
183187
params={
184188
'q': '"ABBA"',
185189
'limit': 50,
190+
'market': 'GB',
186191
'type': 'album,artist'})
187192

188193

194+
def test_sets_market_parameter_from_user_country(
195+
web_client_mock, web_search_mock_large, provider, session_mock):
196+
session_mock.user_country = 'SE'
197+
web_client_mock.get.return_value = web_search_mock_large
198+
199+
provider.search({'any': ['ABBA']})
200+
201+
web_client_mock.get.assert_called_once_with(
202+
'https://api.spotify.com/v1/search',
203+
params={
204+
'q': '"ABBA"',
205+
'limit': 50,
206+
'market': 'SE',
207+
'type': 'album,artist,track'})
208+
209+
189210
def test_handles_empty_response(web_client_mock, provider):
190211
web_client_mock.get.return_value = {}
191212

0 commit comments

Comments
 (0)