Seperate unit and integration tests

This commit is contained in:
Stephane Bruckert 2020-02-02 14:11:07 +00:00
parent 51637d7b2c
commit e8928a47a7
5 changed files with 66 additions and 188 deletions

View File

@ -1,33 +1,52 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import unittest """
import os These tests require user authentication - provide client credentials using the
import requests following environment variables
import spotipy ::
from spotipy import (
CLIENT_CREDS_ENV_VARS as CCEV,
prompt_for_user_token,
Spotify,
SpotifyException,
SpotifyClientCredentials
)
from pprint import pprint # noqa
class TestSpotipy(unittest.TestCase):
"""
These tests require user authentication - provide client credentials using
the following environment variables
::
'SPOTIPY_CLIENT_USERNAME' 'SPOTIPY_CLIENT_USERNAME'
'SPOTIPY_CLIENT_ID' 'SPOTIPY_CLIENT_ID'
'SPOTIPY_CLIENT_SECRET' 'SPOTIPY_CLIENT_SECRET'
'SPOTIPY_REDIRECT_URI' 'SPOTIPY_REDIRECT_URI'
"""
from spotipy import (
Spotify,
SpotifyClientCredentials,
SpotifyException
)
import spotipy
import unittest
import requests
class AuthTestSpotipy(unittest.TestCase):
""" """
These tests require client authentication - provide client credentials
using the following environment variables
::
'SPOTIPY_CLIENT_ID'
'SPOTIPY_CLIENT_SECRET'
"""
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
two_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB"]
other_tracks = ["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
bad_id = 'BAD_ID'
creep_urn = 'spotify:track:3HfB5hBU0dmBt8T0iCmH42' creep_urn = 'spotify:track:3HfB5hBU0dmBt8T0iCmH42'
creep_id = '3HfB5hBU0dmBt8T0iCmH42' creep_id = '3HfB5hBU0dmBt8T0iCmH42'
@ -40,21 +59,40 @@ class TestSpotipy(unittest.TestCase):
radiohead_urn = 'spotify:artist:4Z8W4fKeB5YxbusRsdQVPb' radiohead_urn = 'spotify:artist:4Z8W4fKeB5YxbusRsdQVPb'
angeles_haydn_urn = 'spotify:album:1vAbqAeuJVWNAe7UR00bdM' angeles_haydn_urn = 'spotify:album:1vAbqAeuJVWNAe7UR00bdM'
bad_id = 'BAD_ID'
@classmethod @classmethod
def setUpClass(self): 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 '
'the following environment variables: {}').format(
CCEV.values()))
self.username = os.getenv(CCEV['client_username'])
self.scope = 'user-library-read'
self.token = prompt_for_user_token(self.username, scope=self.scope)
self.spotify = Spotify( self.spotify = Spotify(
client_credentials_manager=SpotifyClientCredentials()) client_credentials_manager=SpotifyClientCredentials())
self.spotify.trace = False
def test_audio_analysis(self):
result = self.spotify.audio_analysis(self.four_tracks[0])
assert('beats' in result)
def test_audio_features(self):
results = self.spotify.audio_features(self.four_tracks)
self.assertTrue(len(results) == len(self.four_tracks))
for track in results:
assert('speechiness' in track)
def test_audio_features_with_bad_track(self):
bad_tracks = []
bad_tracks = ['spotify:track:bad']
input = self.four_tracks + bad_tracks
results = self.spotify.audio_features(input)
self.assertTrue(len(results) == len(input))
for track in results[:-1]:
if track is not None:
assert('speechiness' in track)
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)
self.assertTrue(len(results['tracks']) == 20)
def test_artist_urn(self): def test_artist_urn(self):
artist = self.spotify.artist(self.radiohead_urn) artist = self.spotify.artist(self.radiohead_urn)
@ -216,14 +254,3 @@ class TestSpotipy(unittest.TestCase):
self.assertFalse(isinstance(with_no_session._session, Session)) self.assertFalse(isinstance(with_no_session._session, Session))
self.assertTrue(with_no_session.user(user="akx") self.assertTrue(with_no_session.user(user="akx")
["uri"] == "spotify:user:akx") ["uri"] == "spotify:user:akx")
'''
Need tests for:
- next
- previous
'''
if __name__ == '__main__':
unittest.main()

View File

@ -27,8 +27,6 @@ import warnings
import requests import requests
from pprint import pprint # noqa from pprint import pprint # noqa
sys.path.insert(0, os.path.abspath(os.pardir))
class AuthTestSpotipy(unittest.TestCase): class AuthTestSpotipy(unittest.TestCase):
""" """
@ -389,7 +387,3 @@ class AuthTestSpotipy(unittest.TestCase):
pl = self.spotify.user_playlist_tracks(None, self.playlist, limit=2) pl = self.spotify.user_playlist_tracks(None, self.playlist, limit=2)
self.assertTrue(len(pl["items"]) == 2) self.assertTrue(len(pl["items"]) == 2)
self.assertTrue(pl["total"] > 0) self.assertTrue(pl["total"] > 0)
if __name__ == '__main__':
unittest.main()

View File

@ -1,91 +0,0 @@
# -*- coding: utf-8 -*-
"""
These tests require user authentication - provide client credentials using the
following environment variables
::
'SPOTIPY_CLIENT_USERNAME'
'SPOTIPY_CLIENT_ID'
'SPOTIPY_CLIENT_SECRET'
'SPOTIPY_REDIRECT_URI'
"""
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
::
'SPOTIPY_CLIENT_USERNAME'
'SPOTIPY_CLIENT_ID'
'SPOTIPY_CLIENT_SECRET'
'SPOTIPY_REDIRECT_URI'
"""
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
two_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB"]
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.trace = False
def test_audio_analysis(self):
result = self.spotify.audio_analysis(self.four_tracks[0])
assert('beats' in result)
def test_audio_features(self):
results = self.spotify.audio_features(self.four_tracks)
self.assertTrue(len(results) == len(self.four_tracks))
for track in results:
assert('speechiness' in track)
def test_audio_features_with_bad_track(self):
bad_tracks = []
bad_tracks = ['spotify:track:bad']
input = self.four_tracks + bad_tracks
results = self.spotify.audio_features(input)
self.assertTrue(len(results) == len(input))
for track in results[:-1]:
if track is not None:
assert('speechiness' in track)
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)
self.assertTrue(len(results['tracks']) == 20)
if __name__ == '__main__':
unittest.main()

View File

@ -1,43 +0,0 @@
# -*- coding: utf-8 -*-
""" Client Credentials Requests Tests """
from spotipy import (
Spotify,
SpotifyClientCredentials,
)
import os
import sys
import unittest
sys.path.insert(0, os.path.abspath(os.pardir))
class ClientCredentialsTestSpotipy(unittest.TestCase):
"""
These tests require user authentication - provide client credentials using
the following environment variables
::
'SPOTIPY_CLIENT_USERNAME'
'SPOTIPY_CLIENT_ID'
'SPOTIPY_CLIENT_SECRET'
'SPOTIPY_REDIRECT_URI'
"""
@classmethod
def setUpClass(self):
self.spotify = Spotify(
client_credentials_manager=SpotifyClientCredentials())
self.spotify.trace = False
muse_urn = 'spotify:artist:12Chz98pHFMPJEknJQMWvI'
def test_request_with_token(self):
artist = self.spotify.artist(self.muse_urn)
self.assertTrue(artist['name'] == 'Muse')
if __name__ == '__main__':
unittest.main()

View File

@ -4,12 +4,8 @@ import six.moves.urllib.parse as urllibparse
from spotipy import SpotifyOAuth from spotipy import SpotifyOAuth
import io import io
import json import json
import os
import sys
import unittest import unittest
sys.path.insert(0, os.path.abspath(os.pardir))
try: try:
import unittest.mock as mock import unittest.mock as mock
@ -170,8 +166,3 @@ class TestSpotifyOAuth(unittest.TestCase):
parsed_url = urllibparse.urlparse(url) parsed_url = urllibparse.urlparse(url)
parsed_qs = urllibparse.parse_qs(parsed_url.query) parsed_qs = urllibparse.parse_qs(parsed_url.query)
self.assertTrue(parsed_qs['show_dialog']) self.assertTrue(parsed_qs['show_dialog'])
if __name__ == '__main__':
unittest.main()