Skip to content

Commit

Permalink
search: Set market parameter to user's country. Fixes #97
Browse files Browse the repository at this point in the history
  • Loading branch information
nsteel committed Feb 3, 2017
1 parent 5b39827 commit 84f63ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions mopidy_spotify/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def search(config, session, requests_session,
response = requests_session.get(_API_BASE_URI, params={
'q': sp_query,
'limit': search_count,
'market': session.user_country,
'type': ','.join(types)})
except requests.RequestException as exc:
logger.debug('Fetching %s failed: %s', uri, exc)
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def session_mock():
sp_session_mock = mock.Mock(spec=spotify.Session)
sp_session_mock.connection.state = spotify.ConnectionState.LOGGED_IN
sp_session_mock.playlist_container = []
sp_session_mock.user_country = 'GB'
return sp_session_mock


Expand Down
25 changes: 25 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test_search_returns_albums_and_artists_and_tracks(
assert (uri_parts == [
'https://api.spotify.com/v1/search',
'limit=50',
'market=GB',
'q=%22ABBA%22',
'type=album%2Cartist%2Ctrack'])

Expand Down Expand Up @@ -149,6 +150,7 @@ def test_sets_api_limit_to_album_count_when_max(
assert (uri_parts == [
'https://api.spotify.com/v1/search',
'limit=6',
'market=GB',
'q=%22ABBA%22',
'type=album%2Cartist%2Ctrack'])

Expand All @@ -174,6 +176,7 @@ def test_sets_api_limit_to_artist_count_when_max(
assert (uri_parts == [
'https://api.spotify.com/v1/search',
'limit=6',
'market=GB',
'q=%22ABBA%22',
'type=album%2Cartist%2Ctrack'])

Expand All @@ -199,6 +202,7 @@ def test_sets_api_limit_to_track_count_when_max(
assert (uri_parts == [
'https://api.spotify.com/v1/search',
'limit=6',
'market=GB',
'q=%22ABBA%22',
'type=album%2Cartist%2Ctrack'])

Expand All @@ -222,10 +226,31 @@ def test_sets_types_parameter(
assert (uri_parts == [
'https://api.spotify.com/v1/search',
'limit=50',
'market=GB',
'q=%22ABBA%22',
'type=album%2Cartist'])


@responses.activate
def test_sets_market_parameter(
web_search_mock_large, provider):
responses.add(
responses.GET, 'https://api.spotify.com/v1/search',
body=json.dumps(web_search_mock_large))

provider.search({'any': ['ABBA']})

assert len(responses.calls) == 1

uri_parts = sorted(re.split('[?&]', responses.calls[0].request.url))
assert (uri_parts == [
'https://api.spotify.com/v1/search',
'limit=50',
'market=GB',
'q=%22ABBA%22',
'type=album%2Cartist%2Ctrack'])


@responses.activate
def test_handles_empty_response(
web_search_mock_large, provider):
Expand Down

0 comments on commit 84f63ea

Please sign in to comment.