[PR #257] [CLOSED] Cleanup imports, file structures and tests #834

Closed
opened 2026-02-28 00:02:00 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/spotipy-dev/spotipy/pull/257
Author: @sr-murthy
Created: 2/17/2018
Status: Closed

Base: masterHead: cleanup-imports-and-file-structures-and-tests


📝 Commits (7)

📊 Changes

14 files changed (+304 additions, -156 deletions)

View changed files

📝 .gitignore (+1 -0)
📝 requirements.txt (+2 -0)
📝 setup.py (+1 -0)
📝 spotipy/__init__.py (+4 -1)
📝 spotipy/client.py (+11 -6)
📝 spotipy/oauth2.py (+13 -3)
📝 spotipy/util.py (+17 -1)
tests/client_credentials_tests.py (+0 -27)
📝 tests/test_auth.py (+106 -76)
📝 tests/test_auth2.py (+42 -21)
tests/test_client_credentials.py (+44 -0)
📝 tests/test_oauth.py (+10 -2)
📝 tests/tests.py (+51 -18)
📝 tox.ini (+2 -1)

📄 Description

Changes in this PR involve

  1. adding the UTF-8 source code encoding declaration to all Python modules (https://www.python.org/dev/peps/pep-0008/#source-file-encoding)

  2. re-ordering imports so that they conform to the PEP8 recommendations (https://www.python.org/dev/peps/pep-0008/#imports)

  3. adding the absolute path of the spotify package to sys.path in all test modules

  4. removing all test data initialisation in a test module to a class method (setUpClass) in the relevant test class

  5. modifying client authentication code in tests/tests.py and tests/authtests.py to expect and use environment variables for client credentials - SPOTIPY_CLIENT_USERNAME, SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI

  6. cleaning up invalid variable references in test modules, e.g. replacing module-level variable references to Spotify client objects by test class instance variables, i.e replace spotify by self.spotify where self is the relevant test class instance

  7. fixing user-related test case errors in tests/authtests.py - Spotify lowercases user names so that the user name comparison in the test cases test_basic_user_profile, test_current_user, test_me would always fail for mixed case or upper-cased test user names

  8. fixing a logic error in test case tests/authtests.AuthTestSpotify.test_user_playlist_follow - the playlist follower checking and assertion should be of the currently authenticated user not plamere, who is the test playlist owner

  9. adding test dependencies mock==2.0.0 and simplejson==3.13.2 to requirements.txt and setup.py, and updating tox.ini to add the simplejson dependency

  10. renaming some of the test modules to make them discoverable using unittest discovery - authtests.py -> test_auth.py, authtests2.py -> test_auth2.py, client_credentials_tests.py -> test_client_credentials.py and

  11. updating .gitignore to ignore Shell scripts

The status of the tests is that all test suites pass 100% except for tests/authtests.py - failures/errors in tests/authtests.py are related to the fact that the test application user, for whom credentials were created, does not appear to be the same as the currently authenticated user. This affects the test cases test_user_playlist_ops, test_current_user, test_me:

$ python -m unittest -v authtests
test_basic_user_profile (authtests.AuthTestSpotipy) ... ok
test_categories (authtests.AuthTestSpotipy) ... ok
test_category_playlists (authtests.AuthTestSpotipy) ... ok
test_current_user (authtests.AuthTestSpotipy) ... FAIL
test_current_user_follows (authtests.AuthTestSpotipy) ... ok
test_current_user_playlists (authtests.AuthTestSpotipy) ... ok
test_current_user_save_and_unsave_tracks (authtests.AuthTestSpotipy) ... ok
test_current_user_saved_albums (authtests.AuthTestSpotipy) ... ok
test_current_user_saved_tracks (authtests.AuthTestSpotipy) ... ok
test_current_user_top_artists (authtests.AuthTestSpotipy) ... ok
test_current_user_top_tracks (authtests.AuthTestSpotipy) ... ok
test_featured_releases (authtests.AuthTestSpotipy) ... ok
test_me (authtests.AuthTestSpotipy) ... FAIL
test_new_releases (authtests.AuthTestSpotipy) ... ok
test_track_bad_id (authtests.AuthTestSpotipy) ... ok
test_user_playlist_follow (authtests.AuthTestSpotipy) ... ok
test_user_playlist_ops (authtests.AuthTestSpotipy) ... ERROR
test_user_playlist_tracks (authtests.AuthTestSpotipy) ... ok
test_user_playlists (authtests.AuthTestSpotipy) ... ok

======================================================================
ERROR: test_user_playlist_ops (authtests.AuthTestSpotipy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "authtests.py", line 215, in test_user_playlist_ops
    playlist_id = self.get_or_create_spotify_playlist('spotipy-testing-playlist-1')
  File "authtests.py", line 209, in get_or_create_spotify_playlist
    playlist = self.spotify.user_playlist_create(self.username, playlist_name)
  File "/Users/srm/Documents/sandeep/cst/dev/spotipy/spotipy/client.py", line 420, in user_playlist_create
    return self._post("users/%s/playlists" % (user,), payload=data)
  File "/Users/srm/Documents/sandeep/cst/dev/spotipy/spotipy/client.py", line 185, in _post
    return self._internal_call('POST', url, payload, kwargs)
  File "/Users/srm/Documents/sandeep/cst/dev/spotipy/spotipy/client.py", line 131, in _internal_call
    headers=r.headers)
SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/users/SpotifyTest/playlists:
 You cannot create a playlist for another user.

======================================================================
FAIL: test_current_user (authtests.AuthTestSpotipy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "authtests.py", line 97, in test_current_user
    self.assertTrue(user['id'] == self.username.lower())
AssertionError: False is not true

======================================================================
FAIL: test_me (authtests.AuthTestSpotipy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "authtests.py", line 101, in test_me
    self.assertTrue(user['id'] == self.username.lower())
AssertionError: False is not true

----------------------------------------------------------------------
Ran 19 tests in 10.441s

FAILED (failures=2, errors=1)

$ python -m unittest -v authtests2
test_audio_analysis (authtests2.AuthTestSpotipy) ... ok
test_audio_features (authtests2.AuthTestSpotipy) ... ok
test_audio_features_with_bad_track (authtests2.AuthTestSpotipy) ... ok
test_recommendations (authtests2.AuthTestSpotipy) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.838s

OK

$ python -m unittest -v test_oauth
test_badly_scoped_token_bails (test_oauth.OAuthCacheTest) ... ok
test_expired_token_refreshes (test_oauth.OAuthCacheTest) ... ok
test_gets_from_cache_path (test_oauth.OAuthCacheTest) ... ok
test_saves_to_cache_path (test_oauth.OAuthCacheTest) ... ok
test_get_authorize_url_does_not_show_dialog_by_default (test_oauth.TestSpotifyOAuth) ... ok
test_get_authorize_url_doesnt_pass_state_by_default (test_oauth.TestSpotifyOAuth) ... ok
test_get_authorize_url_passes_state_from_constructor (test_oauth.TestSpotifyOAuth) ... ok
test_get_authorize_url_passes_state_from_func_call (test_oauth.TestSpotifyOAuth) ... ok
test_get_authorize_url_shows_dialog_when_requested (test_oauth.TestSpotifyOAuth) ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.006s

OK

$ python -m unittest -v client_credentials_tests
test_request_with_token (client_credentials_tests.ClientCredentialsTestSpotipy) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.208s

OK

$ python -m unittest -v tests
test_album_search (tests.TestSpotipy) ... ok
test_album_tracks (tests.TestSpotipy) ... ok
test_album_tracks_many (tests.TestSpotipy) ... ok
test_album_urn (tests.TestSpotipy) ... ok
test_albums (tests.TestSpotipy) ... ok
test_artist_albums (tests.TestSpotipy) ... ok
test_artist_related_artists (tests.TestSpotipy) ... ok
test_artist_search (tests.TestSpotipy) ... ok
test_artist_search_with_market (tests.TestSpotipy) ... ok
test_artist_top_tracks (tests.TestSpotipy) ... ok
test_artist_urn (tests.TestSpotipy) ... ok
test_artists (tests.TestSpotipy) ... ok
test_custom_requests_session (tests.TestSpotipy) ... ok
test_force_no_requests_session (tests.TestSpotipy) ... ok
test_search_timeout (tests.TestSpotipy) ... ok
test_track_bad_id (tests.TestSpotipy) ... ok
test_track_bad_urn (tests.TestSpotipy) ... ok
test_track_id (tests.TestSpotipy) ... ok
test_track_search (tests.TestSpotipy) ... ok
test_track_url (tests.TestSpotipy) ... ok
test_track_urn (tests.TestSpotipy) ... ok
test_tracks (tests.TestSpotipy) ... ok
test_unauthenticated_post_fails (tests.TestSpotipy) ... ok
test_user (tests.TestSpotipy) ... ok

----------------------------------------------------------------------
Ran 24 tests in 3.420s

OK

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/spotipy-dev/spotipy/pull/257 **Author:** [@sr-murthy](https://github.com/sr-murthy) **Created:** 2/17/2018 **Status:** ❌ Closed **Base:** `master` ← **Head:** `cleanup-imports-and-file-structures-and-tests` --- ### 📝 Commits (7) - [`90d6622`](https://github.com/spotipy-dev/spotipy/commit/90d66222fd6e1af28a1fe80eb3a21ccf6064e96a) Re-order imports and specify UTF-8 encoding header in all base modules and package initialiser (ref. https://www.python.org/dev/peps/pep-0008/#imports, https://www.python.org/dev/peps/pep-0008/#source-file-encoding) + add `__all__` attribute - [`5f47ae2`](https://github.com/spotipy-dev/spotipy/commit/5f47ae29bd94a70adc9fb046ee8727c1dfb1441f) Update spotify package initialiser - [`8cc601f`](https://github.com/spotipy-dev/spotipy/commit/8cc601fe53adb8138accd61b3f1acdcb06488e72) Remove relative imports in `spotify` package initialiser - [`816dac8`](https://github.com/spotipy-dev/spotipy/commit/816dac861f812ae079a3cbae1b89433c7d82774e) Miscellaneous fixes - clean up tests - [`613d99a`](https://github.com/spotipy-dev/spotipy/commit/613d99a031fc7956673faf1cf3b999e1b3d03c0f) Further clean up of tests - [`578b46e`](https://github.com/spotipy-dev/spotipy/commit/578b46e8cbead0db3b5465880d926ebb7b09ca0c) Renaming some test modules: `authtests.py` -> `test_auth.py`, `authtests2.py` -> `test_auth2.py`, `client_credential_tests.py` -> `test_client_credentials.py` + update `tox.ini` - [`2c5d55d`](https://github.com/spotipy-dev/spotipy/commit/2c5d55d2e0d82f76e33a533c7d0e2c302f07e0af) Update tests - make client credentials env. vars list a commonly accessible dictionary in `spotipy/util.py` ### 📊 Changes **14 files changed** (+304 additions, -156 deletions) <details> <summary>View changed files</summary> 📝 `.gitignore` (+1 -0) 📝 `requirements.txt` (+2 -0) 📝 `setup.py` (+1 -0) 📝 `spotipy/__init__.py` (+4 -1) 📝 `spotipy/client.py` (+11 -6) 📝 `spotipy/oauth2.py` (+13 -3) 📝 `spotipy/util.py` (+17 -1) ➖ `tests/client_credentials_tests.py` (+0 -27) 📝 `tests/test_auth.py` (+106 -76) 📝 `tests/test_auth2.py` (+42 -21) ➕ `tests/test_client_credentials.py` (+44 -0) 📝 `tests/test_oauth.py` (+10 -2) 📝 `tests/tests.py` (+51 -18) 📝 `tox.ini` (+2 -1) </details> ### 📄 Description Changes in this PR involve 1. adding the UTF-8 source code encoding declaration to all Python modules (https://www.python.org/dev/peps/pep-0008/#source-file-encoding) 2. re-ordering imports so that they conform to the PEP8 recommendations (https://www.python.org/dev/peps/pep-0008/#imports) 3. adding the absolute path of the `spotify` package to `sys.path` in all test modules 4. removing all test data initialisation in a test module to a class method (`setUpClass`) in the relevant test class 5. modifying client authentication code in `tests/tests.py` and `tests/authtests.py` to expect and use environment variables for client credentials - `SPOTIPY_CLIENT_USERNAME`, `SPOTIPY_CLIENT_ID`, `SPOTIPY_CLIENT_SECRET`, `SPOTIPY_REDIRECT_URI` 6. cleaning up invalid variable references in test modules, e.g. replacing module-level variable references to Spotify client objects by test class instance variables, i.e replace `spotify` by `self.spotify` where `self` is the relevant test class instance 7. fixing user-related test case errors in `tests/authtests.py` - Spotify lowercases user names so that the user name comparison in the test cases `test_basic_user_profile`, `test_current_user`, `test_me` would always fail for mixed case or upper-cased test user names 8. fixing a logic error in test case `tests/authtests.AuthTestSpotify.test_user_playlist_follow` - the playlist follower checking and assertion should be of the currently authenticated user not `plamere`, who is the test playlist owner 9. adding test dependencies `mock==2.0.0` and `simplejson==3.13.2` to `requirements.txt` and `setup.py`, and updating `tox.ini` to add the `simplejson` dependency 10. renaming some of the test modules to make them discoverable using `unittest` discovery - `authtests.py` -> `test_auth.py`, `authtests2.py` -> `test_auth2.py`, `client_credentials_tests.py` -> `test_client_credentials.py` and 10. updating `.gitignore` to ignore Shell scripts The status of the tests is that all test suites pass 100% except for `tests/authtests.py` - failures/errors in `tests/authtests.py` are related to the fact that the test application user, for whom credentials were created, does not appear to be the same as the currently authenticated user. This affects the test cases `test_user_playlist_ops`, `test_current_user`, `test_me`: $ python -m unittest -v authtests test_basic_user_profile (authtests.AuthTestSpotipy) ... ok test_categories (authtests.AuthTestSpotipy) ... ok test_category_playlists (authtests.AuthTestSpotipy) ... ok test_current_user (authtests.AuthTestSpotipy) ... FAIL test_current_user_follows (authtests.AuthTestSpotipy) ... ok test_current_user_playlists (authtests.AuthTestSpotipy) ... ok test_current_user_save_and_unsave_tracks (authtests.AuthTestSpotipy) ... ok test_current_user_saved_albums (authtests.AuthTestSpotipy) ... ok test_current_user_saved_tracks (authtests.AuthTestSpotipy) ... ok test_current_user_top_artists (authtests.AuthTestSpotipy) ... ok test_current_user_top_tracks (authtests.AuthTestSpotipy) ... ok test_featured_releases (authtests.AuthTestSpotipy) ... ok test_me (authtests.AuthTestSpotipy) ... FAIL test_new_releases (authtests.AuthTestSpotipy) ... ok test_track_bad_id (authtests.AuthTestSpotipy) ... ok test_user_playlist_follow (authtests.AuthTestSpotipy) ... ok test_user_playlist_ops (authtests.AuthTestSpotipy) ... ERROR test_user_playlist_tracks (authtests.AuthTestSpotipy) ... ok test_user_playlists (authtests.AuthTestSpotipy) ... ok ====================================================================== ERROR: test_user_playlist_ops (authtests.AuthTestSpotipy) ---------------------------------------------------------------------- Traceback (most recent call last): File "authtests.py", line 215, in test_user_playlist_ops playlist_id = self.get_or_create_spotify_playlist('spotipy-testing-playlist-1') File "authtests.py", line 209, in get_or_create_spotify_playlist playlist = self.spotify.user_playlist_create(self.username, playlist_name) File "/Users/srm/Documents/sandeep/cst/dev/spotipy/spotipy/client.py", line 420, in user_playlist_create return self._post("users/%s/playlists" % (user,), payload=data) File "/Users/srm/Documents/sandeep/cst/dev/spotipy/spotipy/client.py", line 185, in _post return self._internal_call('POST', url, payload, kwargs) File "/Users/srm/Documents/sandeep/cst/dev/spotipy/spotipy/client.py", line 131, in _internal_call headers=r.headers) SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/users/SpotifyTest/playlists: You cannot create a playlist for another user. ====================================================================== FAIL: test_current_user (authtests.AuthTestSpotipy) ---------------------------------------------------------------------- Traceback (most recent call last): File "authtests.py", line 97, in test_current_user self.assertTrue(user['id'] == self.username.lower()) AssertionError: False is not true ====================================================================== FAIL: test_me (authtests.AuthTestSpotipy) ---------------------------------------------------------------------- Traceback (most recent call last): File "authtests.py", line 101, in test_me self.assertTrue(user['id'] == self.username.lower()) AssertionError: False is not true ---------------------------------------------------------------------- Ran 19 tests in 10.441s FAILED (failures=2, errors=1) $ python -m unittest -v authtests2 test_audio_analysis (authtests2.AuthTestSpotipy) ... ok test_audio_features (authtests2.AuthTestSpotipy) ... ok test_audio_features_with_bad_track (authtests2.AuthTestSpotipy) ... ok test_recommendations (authtests2.AuthTestSpotipy) ... ok ---------------------------------------------------------------------- Ran 4 tests in 0.838s OK $ python -m unittest -v test_oauth test_badly_scoped_token_bails (test_oauth.OAuthCacheTest) ... ok test_expired_token_refreshes (test_oauth.OAuthCacheTest) ... ok test_gets_from_cache_path (test_oauth.OAuthCacheTest) ... ok test_saves_to_cache_path (test_oauth.OAuthCacheTest) ... ok test_get_authorize_url_does_not_show_dialog_by_default (test_oauth.TestSpotifyOAuth) ... ok test_get_authorize_url_doesnt_pass_state_by_default (test_oauth.TestSpotifyOAuth) ... ok test_get_authorize_url_passes_state_from_constructor (test_oauth.TestSpotifyOAuth) ... ok test_get_authorize_url_passes_state_from_func_call (test_oauth.TestSpotifyOAuth) ... ok test_get_authorize_url_shows_dialog_when_requested (test_oauth.TestSpotifyOAuth) ... ok ---------------------------------------------------------------------- Ran 9 tests in 0.006s OK $ python -m unittest -v client_credentials_tests test_request_with_token (client_credentials_tests.ClientCredentialsTestSpotipy) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.208s OK $ python -m unittest -v tests test_album_search (tests.TestSpotipy) ... ok test_album_tracks (tests.TestSpotipy) ... ok test_album_tracks_many (tests.TestSpotipy) ... ok test_album_urn (tests.TestSpotipy) ... ok test_albums (tests.TestSpotipy) ... ok test_artist_albums (tests.TestSpotipy) ... ok test_artist_related_artists (tests.TestSpotipy) ... ok test_artist_search (tests.TestSpotipy) ... ok test_artist_search_with_market (tests.TestSpotipy) ... ok test_artist_top_tracks (tests.TestSpotipy) ... ok test_artist_urn (tests.TestSpotipy) ... ok test_artists (tests.TestSpotipy) ... ok test_custom_requests_session (tests.TestSpotipy) ... ok test_force_no_requests_session (tests.TestSpotipy) ... ok test_search_timeout (tests.TestSpotipy) ... ok test_track_bad_id (tests.TestSpotipy) ... ok test_track_bad_urn (tests.TestSpotipy) ... ok test_track_id (tests.TestSpotipy) ... ok test_track_search (tests.TestSpotipy) ... ok test_track_url (tests.TestSpotipy) ... ok test_track_urn (tests.TestSpotipy) ... ok test_tracks (tests.TestSpotipy) ... ok test_unauthenticated_post_fails (tests.TestSpotipy) ... ok test_user (tests.TestSpotipy) ... ok ---------------------------------------------------------------------- Ran 24 tests in 3.420s OK --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-28 00:02:00 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#834
No description provided.