mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 01:03:53 +00:00
Miscellaneous fixes - clean up tests
This commit is contained in:
parent
502a56f987
commit
e9edf2a255
1
.gitignore
vendored
1
.gitignore
vendored
@ -55,3 +55,4 @@ docs/_build/
|
||||
|
||||
.*
|
||||
archive
|
||||
*.sh
|
||||
|
||||
@ -1,2 +1,3 @@
|
||||
mock==2.0.0
|
||||
requests==2.3.0
|
||||
six==1.10.0
|
||||
1
setup.py
1
setup.py
@ -9,6 +9,7 @@ setup(
|
||||
author_email="paul@echonest.com",
|
||||
url='http://spotipy.readthedocs.org/',
|
||||
install_requires=[
|
||||
'mock>=2.0.0',
|
||||
'requests>=2.3.0',
|
||||
'six>=1.10.0',
|
||||
],
|
||||
|
||||
@ -60,7 +60,7 @@ class Spotify(object):
|
||||
def __init__(self, auth=None, requests_session=True,
|
||||
client_credentials_manager=None, proxies=None, requests_timeout=None):
|
||||
"""
|
||||
Create a Spotify API object.
|
||||
Creates a Spotify API client.
|
||||
|
||||
:param auth: An authorization token (optional)
|
||||
:param requests_session:
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
from __future__ import print_function
|
||||
|
||||
__all__ = [
|
||||
'prompt_for_user_token9'
|
||||
'prompt_for_user_token'
|
||||
]
|
||||
|
||||
import os
|
||||
|
||||
@ -1,27 +1,36 @@
|
||||
# -*- coding: latin-1 -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import spotipy
|
||||
from spotipy.oauth2 import SpotifyClientCredentials
|
||||
""" Client Credentials Requests Tests """
|
||||
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
'''
|
||||
Client Credentials Requests Tests
|
||||
'''
|
||||
sys.path.insert(0, os.path.abspath(os.pardir))
|
||||
|
||||
from spotipy import (
|
||||
Spotify,
|
||||
SpotifyClientCredentials,
|
||||
)
|
||||
|
||||
|
||||
class ClientCredentialsTestSpotipy(unittest.TestCase):
|
||||
'''
|
||||
These tests require user authentication
|
||||
'''
|
||||
|
||||
@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 = spotify.artist(self.muse_urn)
|
||||
artist = self.spotify.artist(self.muse_urn)
|
||||
self.assertTrue(artist['name'] == 'Muse')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
spotify_cc = SpotifyClientCredentials()
|
||||
spotify = spotipy.Spotify(client_credentials_manager=spotify_cc)
|
||||
spotify.trace = False
|
||||
|
||||
unittest.main()
|
||||
|
||||
@ -1,8 +1,15 @@
|
||||
from spotipy.oauth2 import SpotifyOAuth
|
||||
import json
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.pardir))
|
||||
|
||||
from spotipy import SpotifyOAuth
|
||||
|
||||
try:
|
||||
import unittest.mock as mock
|
||||
except ImportError:
|
||||
@ -166,4 +173,5 @@ class TestSpotifyOAuth(unittest.TestCase):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
unittest.main()
|
||||
|
||||
@ -1,9 +1,19 @@
|
||||
# -*- coding: latin-1 -*-
|
||||
import spotipy
|
||||
import unittest
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import pprint
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
import requests
|
||||
from spotipy.client import SpotifyException
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.pardir))
|
||||
|
||||
from spotipy import (
|
||||
prompt_for_user_token,
|
||||
Spotify,
|
||||
SpotifyException,
|
||||
)
|
||||
|
||||
|
||||
class TestSpotipy(unittest.TestCase):
|
||||
@ -22,8 +32,14 @@ class TestSpotipy(unittest.TestCase):
|
||||
|
||||
bad_id = 'BAD_ID'
|
||||
|
||||
def setUp(self):
|
||||
self.spotify = spotipy.Spotify()
|
||||
@classmethod
|
||||
def setUpClass(self):
|
||||
self.token = os.getenv('SPOTIPY_CLIENT_TOKEN')
|
||||
if not self.token:
|
||||
raise Exception('Set your Spotify client app token via the environment variable `SPOTIPY_CLIENT_TOKEN`')
|
||||
|
||||
self.spotify = Spotify(auth=self.token)
|
||||
|
||||
|
||||
def test_artist_urn(self):
|
||||
artist = self.spotify.artist(self.radiohead_urn)
|
||||
@ -74,7 +90,7 @@ class TestSpotipy(unittest.TestCase):
|
||||
try:
|
||||
track = self.spotify.track(self.el_scorcho_bad_urn)
|
||||
self.assertTrue(False)
|
||||
except spotipy.SpotifyException:
|
||||
except SpotifyException:
|
||||
self.assertTrue(True)
|
||||
|
||||
def test_tracks(self):
|
||||
@ -121,11 +137,11 @@ class TestSpotipy(unittest.TestCase):
|
||||
self.assertTrue(found)
|
||||
|
||||
def test_search_timeout(self):
|
||||
sp = spotipy.Spotify(requests_timeout=.1)
|
||||
sp = Spotify(auth=self.token, requests_timeout=.1)
|
||||
try:
|
||||
results = sp.search(q='my*', type='track')
|
||||
self.assertTrue(False, 'unexpected search timeout')
|
||||
except requests.ReadTimeout:
|
||||
except requests.Timeout:
|
||||
self.assertTrue(True, 'expected search timeout')
|
||||
|
||||
|
||||
@ -149,14 +165,14 @@ class TestSpotipy(unittest.TestCase):
|
||||
try:
|
||||
track = self.spotify.track(self.bad_id)
|
||||
self.assertTrue(False)
|
||||
except spotipy.SpotifyException:
|
||||
except SpotifyException:
|
||||
self.assertTrue(True)
|
||||
|
||||
def test_track_bad_id(self):
|
||||
try:
|
||||
track = self.spotify.track(self.bad_id)
|
||||
self.assertTrue(False)
|
||||
except spotipy.SpotifyException:
|
||||
except SpotifyException:
|
||||
self.assertTrue(True)
|
||||
|
||||
def test_unauthenticated_post_fails(self):
|
||||
@ -166,20 +182,17 @@ class TestSpotipy(unittest.TestCase):
|
||||
cm.exception.http_status == 403)
|
||||
|
||||
def test_custom_requests_session(self):
|
||||
from requests import Session
|
||||
sess = Session()
|
||||
sess = requests.Session()
|
||||
sess.headers["user-agent"] = "spotipy-test"
|
||||
with_custom_session = spotipy.Spotify(requests_session=sess)
|
||||
with_custom_session = Spotify(auth=self.token, requests_session=sess)
|
||||
self.assertTrue(with_custom_session.user(user="akx")["uri"] == "spotify:user:akx")
|
||||
|
||||
def test_force_no_requests_session(self):
|
||||
from requests import Session
|
||||
with_no_session = spotipy.Spotify(requests_session=False)
|
||||
self.assertFalse(isinstance(with_no_session._session, Session))
|
||||
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")
|
||||
|
||||
|
||||
|
||||
'''
|
||||
Need tests for:
|
||||
|
||||
@ -188,4 +201,5 @@ class TestSpotipy(unittest.TestCase):
|
||||
'''
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
unittest.main()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user