From 221fbe82683985d5b6a29550a5f572d4594f493f Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 20 Jan 2025 15:43:49 -0500 Subject: [PATCH] bring in changes from context-manager-mock branch Updated integration tests to mock `_get` and `_post` methods in the Spotify class --- tests/integration/user_endpoints/test.py | 4 +++ tests/unit/test_oauth.py | 46 ++++++++---------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/tests/integration/user_endpoints/test.py b/tests/integration/user_endpoints/test.py index cb9e8c92..41252bd5 100644 --- a/tests/integration/user_endpoints/test.py +++ b/tests/integration/user_endpoints/test.py @@ -1,5 +1,6 @@ import os import unittest +from unittest.mock import patch from spotipy import CLIENT_CREDS_ENV_VARS as CCEV from spotipy import (Spotify, SpotifyException, SpotifyImplicitGrant, @@ -530,6 +531,7 @@ class SpotifyQueueApiTests(unittest.TestCase): def setUp(self): self.spotify = Spotify(auth="test_token") + @patch('spotipy.client.Spotify._get') def test_get_queue(self, mock_get): # Mock the response from _get mock_get.return_value = {'songs': ['song1', 'song2']} @@ -543,6 +545,7 @@ def test_get_queue(self, mock_get): # Check if the response is as expected self.assertEqual(response, {'songs': ['song1', 'song2']}) + @patch('spotipy.client.Spotify._post') def test_add_to_queue(self, mock_post): test_uri = 'spotify:track:123' @@ -553,6 +556,7 @@ def test_add_to_queue(self, mock_post): endpoint = f"me/player/queue?uri={test_uri}" mock_post.assert_called_with(endpoint) + @patch('spotipy.client.Spotify._post') def test_add_to_queue_with_device_id(self, mock_post): test_uri = 'spotify:track:123' device_id = 'device123' diff --git a/tests/unit/test_oauth.py b/tests/unit/test_oauth.py index 6adeae41..84bb0b4f 100644 --- a/tests/unit/test_oauth.py +++ b/tests/unit/test_oauth.py @@ -3,7 +3,6 @@ import unittest import unittest.mock as mock import urllib.parse as urllibparse -from unittest.mock import mock_open from spotipy import SpotifyImplicitGrant, SpotifyOAuth, SpotifyPKCE from spotipy.cache_handler import MemoryCacheHandler @@ -50,8 +49,7 @@ class OAuthCacheTest(unittest.TestCase): @patch.multiple(SpotifyOAuth, is_token_expired=DEFAULT, refresh_access_token=DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_gets_from_cache_path(self, opener, is_token_expired, refresh_access_token): """Test that the token is retrieved from the cache path.""" @@ -75,8 +73,7 @@ def test_gets_from_cache_path(self, opener, @patch.multiple(SpotifyOAuth, is_token_expired=DEFAULT, refresh_access_token=DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_expired_token_refreshes(self, opener, is_token_expired, refresh_access_token): """Test that an expired token is refreshed.""" @@ -99,8 +96,7 @@ def test_expired_token_refreshes(self, opener, @patch.multiple(SpotifyOAuth, is_token_expired=DEFAULT, refresh_access_token=DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_badly_scoped_token_bails(self, opener, is_token_expired, refresh_access_token): token_scope = "playlist-modify-public" @@ -121,8 +117,7 @@ def test_badly_scoped_token_bails(self, opener, self.assertIsNone(cached_tok) self.assertEqual(refresh_access_token.call_count, 0) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_saves_to_cache_path(self, opener): """Test that the token is saved to the cache path.""" scope = "playlist-modify-private" @@ -140,8 +135,7 @@ def test_saves_to_cache_path(self, opener): opener.assert_called_with(path, 'w', encoding='utf-8') self.assertTrue(fi.write.called) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_saves_to_cache_path_legacy(self, opener): scope = "playlist-modify-private" path = ".cache-username" @@ -265,8 +259,7 @@ def test_spotify_client_credentials_get_access_token(self): class ImplicitGrantCacheTest(unittest.TestCase): @patch.object(SpotifyImplicitGrant, "is_token_expired", DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_gets_from_cache_path(self, opener, is_token_expired): scope = "playlist-modify-private" path = ".cache-username" @@ -287,8 +280,7 @@ def test_gets_from_cache_path(self, opener, is_token_expired): self.assertIsNotNone(cached_tok_legacy) @patch.object(SpotifyImplicitGrant, "is_token_expired", DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_expired_token_returns_none(self, opener, is_token_expired): """Test that an expired token returns None.""" scope = "playlist-modify-private" @@ -308,8 +300,7 @@ def test_expired_token_returns_none(self, opener, is_token_expired): self.assertIsNone(cached_tok) @patch.object(SpotifyImplicitGrant, "is_token_expired", DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_badly_scoped_token_bails(self, opener, is_token_expired): token_scope = "playlist-modify-public" requested_scope = "playlist-modify-private" @@ -328,8 +319,7 @@ def test_badly_scoped_token_bails(self, opener, is_token_expired): opener.assert_called_with(path, encoding='utf-8') self.assertIsNone(cached_tok) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_saves_to_cache_path(self, opener): scope = "playlist-modify-private" path = ".cache-username" @@ -346,8 +336,7 @@ def test_saves_to_cache_path(self, opener): opener.assert_called_with(path, 'w', encoding='utf-8') self.assertTrue(fi.write.called) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_saves_to_cache_path_legacy(self, opener): scope = "playlist-modify-private" path = ".cache-username" @@ -419,8 +408,7 @@ class SpotifyPKCECacheTest(unittest.TestCase): @patch.multiple(SpotifyPKCE, is_token_expired=DEFAULT, refresh_access_token=DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_gets_from_cache_path(self, opener, is_token_expired, refresh_access_token): scope = "playlist-modify-private" @@ -444,8 +432,7 @@ def test_gets_from_cache_path(self, opener, @patch.multiple(SpotifyPKCE, is_token_expired=DEFAULT, refresh_access_token=DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_expired_token_refreshes(self, opener, is_token_expired, refresh_access_token): scope = "playlist-modify-private" @@ -467,8 +454,7 @@ def test_expired_token_refreshes(self, opener, @patch.multiple(SpotifyPKCE, is_token_expired=DEFAULT, refresh_access_token=DEFAULT) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_badly_scoped_token_bails(self, opener, is_token_expired, refresh_access_token): token_scope = "playlist-modify-public" @@ -489,8 +475,7 @@ def test_badly_scoped_token_bails(self, opener, self.assertIsNone(cached_tok) self.assertEqual(refresh_access_token.call_count, 0) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_saves_to_cache_path(self, opener): scope = "playlist-modify-private" path = ".cache-username" @@ -506,8 +491,7 @@ def test_saves_to_cache_path(self, opener): opener.assert_called_with(path, 'w', encoding='utf-8') self.assertTrue(fi.write.called) - @patch('spotipy.cache_handler.open', side_effect=lambda *args, ** - kwargs: mock_open(read_data='mock data')(), create=True) + @patch('spotipy.cache_handler.open', create=True) def test_saves_to_cache_path_legacy(self, opener): scope = "playlist-modify-private" path = ".cache-username"