Add linter, solves #348 (#415)

This commit is contained in:
Stéphane Bruckert authored and GitHub committed 2020-01-12 13:19:40 +00:00
1 parent 5928981e3b
commit f54830e272
35 files changed
+324 -248

No files matched your search

+61 -46
View File
@@ -14,27 +14,23 @@ following environment variables
from __future__ import print_function
import os
from pprint import pprint
import sys
import unittest
import simplejson as json
sys.path.insert(0, os.path.abspath(os.pardir))
from spotipy import (
CLIENT_CREDS_ENV_VARS as CCEV,
prompt_for_user_token,
Spotify,
SpotifyException,
)
import os
import sys
import unittest
sys.path.insert(0, os.path.abspath(os.pardir))
class AuthTestSpotipy(unittest.TestCase):
"""
These tests require user authentication - provide client credentials using the
following environment variables
These tests require user authentication - provide client credentials using
the following environment variables
::
@@ -47,30 +43,32 @@ class AuthTestSpotipy(unittest.TestCase):
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
playlist_new_id = "spotify:playlist:7GlxpQjjxRjmbb3RP2rDqI"
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
two_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB"]
"spotify:track:7IHOIqZUUInxjVkko181PB"]
other_tracks=["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
other_tracks = ["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
album_ids = ["spotify:album:6kL09DaURb7rAoqqaA51KU",
"spotify:album:6RTzC0rDbvagTSJLlY7AKl"]
bad_id = 'BAD_ID'
@classmethod
def setUpClass(self):
missing = list(filter(lambda var: not os.getenv(CCEV[var]), CCEV))
if missing:
raise Exception('Please set the client credentials for the test application using the following environment variables: {}'.format(CCEV.values()))
raise Exception(
('Please set the client credentials for the test application'
' using the following environment variables: {}').format(
CCEV.values()))
self.username = os.getenv(CCEV['client_username'])
@@ -121,14 +119,12 @@ class AuthTestSpotipy(unittest.TestCase):
results = self.spotify.user_playlist_tracks(user, pid)
self.assertTrue(len(results['items']) >= 0)
def user_playlist_tracks(self, user, playlist_id = None, fields=None,
limit=100, offset=0):
# known API issue currently causes this test to fail
# the issue is that the API doesn't currently respect the
# limit parameter
self.assertTrue(len(playlists['items']) == 5)
# known API issue currently causes this test to fail
# the issue is that the API doesn't currently respect the
# limit parameter
# def user_playlist_tracks(self, user, playlist_id=None, fields=None,
# limit=100, offset=0):
# self.assertTrue(len(playlists['items']) == 5)
def test_current_user_saved_albums(self):
# List
@@ -139,7 +135,10 @@ class AuthTestSpotipy(unittest.TestCase):
self.spotify.current_user_saved_albums_add(self.album_ids)
# Contains
self.assertTrue(self.spotify.current_user_saved_albums_contains(self.album_ids) == [True, True])
self.assertTrue(
self.spotify.current_user_saved_albums_contains(
self.album_ids) == [
True, True])
# Remove
self.spotify.current_user_saved_albums_delete(self.album_ids)
@@ -152,14 +151,20 @@ class AuthTestSpotipy(unittest.TestCase):
self.assertTrue(len(playlists['items']) == 10)
def test_user_playlist_follow(self):
self.spotify.user_playlist_follow_playlist('plamere', '4erXB04MxwRAVqcUEpu30O')
follows = self.spotify.user_playlist_is_following('plamere', '4erXB04MxwRAVqcUEpu30O', [self.spotify.current_user()['id']])
self.spotify.user_playlist_follow_playlist(
'plamere', '4erXB04MxwRAVqcUEpu30O')
follows = self.spotify.user_playlist_is_following(
'plamere', '4erXB04MxwRAVqcUEpu30O', [
self.spotify.current_user()['id']])
self.assertTrue(len(follows) == 1, 'proper follows length')
self.assertTrue(follows[0], 'is following')
self.spotify.user_playlist_unfollow('plamere', '4erXB04MxwRAVqcUEpu30O')
self.spotify.user_playlist_unfollow(
'plamere', '4erXB04MxwRAVqcUEpu30O')
follows = self.spotify.user_playlist_is_following('plamere', '4erXB04MxwRAVqcUEpu30O', [self.spotify.current_user()['id']])
follows = self.spotify.user_playlist_is_following(
'plamere', '4erXB04MxwRAVqcUEpu30O', [
self.spotify.current_user()['id']])
self.assertTrue(len(follows) == 1, 'proper follows length')
self.assertFalse(follows[0], 'is no longer following')
@@ -176,7 +181,8 @@ class AuthTestSpotipy(unittest.TestCase):
new_total = tracks['total']
self.assertTrue(new_total - total == len(self.four_tracks))
tracks = self.spotify.current_user_saved_tracks_delete(self.four_tracks)
tracks = self.spotify.current_user_saved_tracks_delete(
self.four_tracks)
tracks = self.spotify.current_user_saved_tracks()
new_total = tracks['total']
self.assertTrue(new_total == total)
@@ -224,37 +230,45 @@ class AuthTestSpotipy(unittest.TestCase):
if item['name'] == playlist_name:
return item['id']
playlists = self.spotify.next(playlists)
playlist = self.spotify.user_playlist_create(self.username, playlist_name)
playlist = self.spotify.user_playlist_create(
self.username, playlist_name)
playlist_id = playlist['uri']
return playlist_id
def test_user_playlist_ops(self):
sp = self.spotify
# create empty playlist
playlist_id = self.get_or_create_spotify_playlist('spotipy-testing-playlist-1')
playlist_id = self.get_or_create_spotify_playlist(
'spotipy-testing-playlist-1')
# remove all tracks from it
self.spotify.user_playlist_replace_tracks(self.username, playlist_id,[])
playlist = self.spotify.user_playlist(self.username, playlist_id)
sp.user_playlist_replace_tracks(
self.username, playlist_id, [])
playlist = sp.user_playlist(self.username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 0)
self.assertTrue(len(playlist['tracks']['items']) == 0)
# add tracks to it
self.spotify.user_playlist_add_tracks(self.username, playlist_id, self.four_tracks)
playlist = self.spotify.user_playlist(self.username, playlist_id)
sp.user_playlist_add_tracks(
self.username, playlist_id, self.four_tracks)
playlist = sp.user_playlist(self.username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 4)
self.assertTrue(len(playlist['tracks']['items']) == 4)
# remove two tracks from it
self.spotify.user_playlist_remove_all_occurrences_of_tracks (self.username,
playlist_id, self.two_tracks)
playlist = self.spotify.user_playlist(self.username, playlist_id)
sp.user_playlist_remove_all_occurrences_of_tracks(self.username,
playlist_id,
self.two_tracks)
playlist = sp.user_playlist(self.username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 2)
self.assertTrue(len(playlist['tracks']['items']) == 2)
# replace with 3 other tracks
self.spotify.user_playlist_replace_tracks(self.username,
playlist_id, self.other_tracks)
playlist = self.spotify.user_playlist(self.username, playlist_id)
sp.user_playlist_replace_tracks(self.username,
playlist_id,
self.other_tracks)
playlist = sp.user_playlist(self.username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 3)
self.assertTrue(len(playlist['tracks']['items']) == 3)
@@ -293,5 +307,6 @@ class AuthTestSpotipy(unittest.TestCase):
# Unfollow these 2 users
self.spotify.user_unfollow_users(users)
if __name__ == '__main__':
unittest.main()
+23 -23
View File
@@ -12,25 +12,21 @@ following environment variables
'SPOTIPY_REDIRECT_URI'
"""
import os
import pprint
import sys
import unittest
import simplejson as json
sys.path.insert(0, os.path.abspath(os.pardir))
from spotipy import (
Spotify,
SpotifyClientCredentials,
)
import os
import sys
import unittest
sys.path.insert(0, os.path.abspath(os.pardir))
class AuthTestSpotipy(unittest.TestCase):
"""
These tests require user authentication - provide client credentials using the
following environment variables
These tests require user authentication - provide client credentials using
the following environment variables
::
@@ -42,22 +38,23 @@ class AuthTestSpotipy(unittest.TestCase):
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
two_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB"]
"spotify:track:7IHOIqZUUInxjVkko181PB"]
other_tracks=["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
other_tracks = ["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
bad_id = 'BAD_ID'
@classmethod
def setUpClass(self):
self.spotify = Spotify(client_credentials_manager=SpotifyClientCredentials())
self.spotify = Spotify(
client_credentials_manager=SpotifyClientCredentials())
self.spotify.trace = False
def test_audio_analysis(self):
@@ -77,15 +74,18 @@ class AuthTestSpotipy(unittest.TestCase):
results = self.spotify.audio_features(input)
self.assertTrue(len(results) == len(input))
for track in results[:-1]:
if track != None:
if track is not None:
assert('speechiness' in track)
self.assertTrue(results[-1] == None)
self.assertTrue(results[-1] is None)
def test_recommendations(self):
results = self.spotify.recommendations(seed_tracks=self.four_tracks, min_danceability=0, max_loudness=0, target_popularity=50)
results = self.spotify.recommendations(
seed_tracks=self.four_tracks,
min_danceability=0,
max_loudness=0,
target_popularity=50)
self.assertTrue(len(results['tracks']) == 20)
if __name__ == '__main__':
unittest.main()
+8 -9
View File
@@ -2,22 +2,21 @@
""" Client Credentials Requests Tests """
from spotipy import (
Spotify,
SpotifyClientCredentials,
)
import os
import sys
import unittest
sys.path.insert(0, os.path.abspath(os.pardir))
from spotipy import (
Spotify,
SpotifyClientCredentials,
)
class ClientCredentialsTestSpotipy(unittest.TestCase):
"""
These tests require user authentication - provide client credentials using the
following environment variables
These tests require user authentication - provide client credentials using
the following environment variables
::
@@ -29,7 +28,8 @@ class ClientCredentialsTestSpotipy(unittest.TestCase):
@classmethod
def setUpClass(self):
self.spotify = Spotify(client_credentials_manager=SpotifyClientCredentials())
self.spotify = Spotify(
client_credentials_manager=SpotifyClientCredentials())
self.spotify.trace = False
muse_urn = 'spotify:artist:12Chz98pHFMPJEknJQMWvI'
@@ -40,5 +40,4 @@ class ClientCredentialsTestSpotipy(unittest.TestCase):
if __name__ == '__main__':
unittest.main()
+2 -2
View File
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import six.moves.urllib.parse as urllibparse
from spotipy import SpotifyOAuth
import io
import json
import os
@@ -8,13 +10,11 @@ import unittest
sys.path.insert(0, os.path.abspath(os.pardir))
from spotipy import SpotifyOAuth
try:
import unittest.mock as mock
except ImportError:
import mock
import six.moves.urllib.parse as urllibparse
patch = mock.patch
DEFAULT = mock.DEFAULT
+34 -33
View File
@@ -1,27 +1,25 @@
# -*- coding: utf-8 -*-
import os
import pprint
import sys
import unittest
import requests
sys.path.insert(0, os.path.abspath(os.pardir))
from spotipy import (
CLIENT_CREDS_ENV_VARS as CCEV,
prompt_for_user_token,
Spotify,
SpotifyException,
)
import os
import sys
import unittest
import requests
sys.path.insert(0, os.path.abspath(os.pardir))
class TestSpotipy(unittest.TestCase):
"""
These tests require user authentication - provide client credentials using the
following environment variables
These tests require user authentication - provide client credentials using
the following environment variables
::
@@ -42,7 +40,6 @@ class TestSpotipy(unittest.TestCase):
radiohead_urn = 'spotify:artist:4Z8W4fKeB5YxbusRsdQVPb'
angeles_haydn_urn = 'spotify:album:1vAbqAeuJVWNAe7UR00bdM'
bad_id = 'BAD_ID'
@classmethod
@@ -50,7 +47,10 @@ class TestSpotipy(unittest.TestCase):
missing = list(filter(lambda var: not os.getenv(CCEV[var]), CCEV))
if missing:
raise Exception('Please set the client credentials for the test application using the following environment variables: {}'.format(CCEV.values()))
raise Exception(
('Please set the client credentials for the test '
'the following environment variables: {}').format(
CCEV.values()))
self.username = os.getenv(CCEV['client_username'])
@@ -82,14 +82,16 @@ class TestSpotipy(unittest.TestCase):
tracks = results['items']
total, received = results['total'], len(tracks)
while received < total:
results = self.spotify.album_tracks(self.angeles_haydn_urn, offset=received)
results = self.spotify.album_tracks(
self.angeles_haydn_urn, offset=received)
tracks.extend(results['items'])
received = len(tracks)
self.assertEqual(received, total)
def test_albums(self):
results = self.spotify.albums([self.pinkerton_urn, self.pablo_honey_urn])
results = self.spotify.albums(
[self.pinkerton_urn, self.pablo_honey_urn])
self.assertTrue('albums' in results)
self.assertTrue(len(results['albums']) == 2)
@@ -107,7 +109,7 @@ class TestSpotipy(unittest.TestCase):
def test_track_bad_urn(self):
try:
track = self.spotify.track(self.el_scorcho_bad_urn)
self.spotify.track(self.el_scorcho_bad_urn)
self.assertTrue(False)
except SpotifyException:
self.assertTrue(True)
@@ -158,17 +160,17 @@ class TestSpotipy(unittest.TestCase):
def test_search_timeout(self):
sp = Spotify(auth=self.token, requests_timeout=.01)
try:
results = sp.search(q='my*', type='track')
sp.search(q='my*', type='track')
self.assertTrue(False, 'unexpected search timeout')
except requests.Timeout:
self.assertTrue(True, 'expected search timeout')
def test_album_search(self):
results = self.spotify.search(q='weezer pinkerton', type='album')
self.assertTrue('albums' in results)
self.assertTrue(len(results['albums']['items']) > 0)
self.assertTrue(results['albums']['items'][0]['name'].find('Pinkerton') >= 0)
self.assertTrue(results['albums']['items'][0]
['name'].find('Pinkerton') >= 0)
def test_track_search(self):
results = self.spotify.search(q='el scorcho weezer', type='track')
@@ -182,34 +184,34 @@ class TestSpotipy(unittest.TestCase):
def test_track_bad_id(self):
try:
track = self.spotify.track(self.bad_id)
self.assertTrue(False)
except SpotifyException:
self.assertTrue(True)
def test_track_bad_id(self):
try:
track = self.spotify.track(self.bad_id)
self.spotify.track(self.bad_id)
self.assertTrue(False)
except SpotifyException:
self.assertTrue(True)
def test_unauthenticated_post_fails(self):
with self.assertRaises(SpotifyException) as cm:
self.spotify.user_playlist_create("spotify", "Best hits of the 90s")
self.spotify.user_playlist_create(
"spotify", "Best hits of the 90s")
self.assertTrue(cm.exception.http_status == 401 or
cm.exception.http_status == 403)
cm.exception.http_status == 403)
def test_custom_requests_session(self):
sess = requests.Session()
sess.headers["user-agent"] = "spotipy-test"
with_custom_session = Spotify(auth=self.token, requests_session=sess)
self.assertTrue(with_custom_session.user(user="akx")["uri"] == "spotify:user:akx")
self.assertTrue(
with_custom_session.user(
user="akx")["uri"] == "spotify:user:akx")
def test_force_no_requests_session(self):
with_no_session = Spotify(auth=self.token, requests_session=False)
self.assertFalse(isinstance(with_no_session._session, requests.Session))
self.assertTrue(with_no_session.user(user="akx")["uri"] == "spotify:user:akx")
self.assertFalse(
isinstance(
with_no_session._session,
requests.Session))
self.assertTrue(with_no_session.user(user="akx")
["uri"] == "spotify:user:akx")
'''
@@ -220,5 +222,4 @@ class TestSpotipy(unittest.TestCase):
'''
if __name__ == '__main__':
unittest.main()