From 91f2b2d5a2d053aa3c0f0691418d2949b1aba983 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Sat, 7 Oct 2017 23:26:51 +0000 Subject: Run flake8 on tests --- tests/__init__.py | 1 + tests/test_pandora/test_client.py | 34 ++++----- tests/test_pandora/test_clientbuilder.py | 14 ++-- tests/test_pandora/test_models.py | 126 +++++++++++++++++++------------ tests/test_pandora/test_transport.py | 12 +-- tests/test_pandora/test_util.py | 22 ++++++ tests/test_pandora/test_utils.py | 22 ------ tests/test_pydora/test_utils.py | 28 ++++--- 8 files changed, 150 insertions(+), 109 deletions(-) create mode 100644 tests/test_pandora/test_util.py delete mode 100644 tests/test_pandora/test_utils.py diff --git a/tests/__init__.py b/tests/__init__.py index e080ee5..80793cf 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,5 +1,6 @@ import os from unittest import TestLoader + def discover_suite(): return TestLoader().discover(os.path.dirname(__file__)) diff --git a/tests/test_pandora/test_client.py b/tests/test_pandora/test_client.py index 8d93e4c..f88b755 100644 --- a/tests/test_pandora/test_client.py +++ b/tests/test_pandora/test_client.py @@ -65,13 +65,12 @@ class TestCallingAPIClient(TestCase): transport.assert_has_calls([call("method"), call("method")]) def test_playlist_fetches_ads(self): - fake_playlist = { "items": [ - { "songName": "test" }, - { "adToken": "foo" }, - { "songName": "test" }, + fake_playlist = {"items": [ + {"songName": "test"}, + {"adToken": "foo"}, + {"songName": "test"}, ]} - with patch.object( - APIClient, '__call__', return_value=fake_playlist) as mock: + with patch.object(APIClient, '__call__', return_value=fake_playlist): client = APIClient(Mock(), None, None, None, None) client._authenticate = Mock() @@ -88,10 +87,10 @@ class TestCallingAPIClient(TestCase): client.get_playlist('token_mock') playlist_mock.assert_has_calls([call("station.getPlaylist", - audioAdPodCapable=True, - includeTrackLength=True, - stationToken='token_mock', - xplatformAdCapable=True)]) + audioAdPodCapable=True, + includeTrackLength=True, + stationToken='token_mock', + xplatformAdCapable=True)]) class TestGettingQualities(TestCase): @@ -118,8 +117,10 @@ class TestGettingQualities(TestCase): class TestGettingAds(TestCase): def test_get_ad_item_(self): - with patch.object(APIClient, '__call__', - return_value=TestAdItem.JSON_DATA) as ad_metadata_mock: + metamock = patch.object( + APIClient, '__call__', return_value=TestAdItem.JSON_DATA) + + with metamock as ad_metadata_mock: transport = Mock(side_effect=[errors.InvalidAuthToken(), None]) client = APIClient(transport, None, None, None, None) @@ -129,10 +130,9 @@ class TestGettingAds(TestCase): assert ad_item.station_id == 'id_mock' assert ad_item.ad_token == 'token_mock' - ad_metadata_mock.assert_has_calls([call("ad.getAdMetadata", - adToken='token_mock', - returnAdTrackingTokens=True, - supportAudioAds=True)]) + ad_metadata_mock.assert_has_calls([ + call("ad.getAdMetadata", adToken='token_mock', + returnAdTrackingTokens=True, supportAudioAds=True)]) def test_get_ad_item_with_no_station_id_specified_raises_exception(self): transport = Mock(side_effect=[errors.InvalidAuthToken(), None]) @@ -175,7 +175,7 @@ class TestCreatingGenreStation(TestCase): def test_has_initial_checksum(self): fake_data = { "categories": [ - { "categoryName": "foo", "stations": [] }, + {"categoryName": "foo", "stations": []}, ], # Not actually part of the genre station response but is needed to diff --git a/tests/test_pandora/test_clientbuilder.py b/tests/test_pandora/test_clientbuilder.py index b02a4d1..0105c09 100644 --- a/tests/test_pandora/test_clientbuilder.py +++ b/tests/test_pandora/test_clientbuilder.py @@ -11,8 +11,8 @@ class TestTranslatingDict(TestCase): class TestDict(cb.TranslatingDict): - KEY_TRANSLATIONS = { "FOO": "BAR" } - VALUE_TRANSLATIONS = { "BAZ": lambda v: v + 1 } + KEY_TRANSLATIONS = {"FOO": "BAR"} + VALUE_TRANSLATIONS = {"BAZ": lambda v: v + 1} callback_value = None def was_translated(self, from_key, to_key): @@ -22,7 +22,7 @@ class TestTranslatingDict(TestCase): self.dct = self.TestDict() def test_construction_with_dict(self): - dct = self.TestDict({ "BIZ": 1, "BUZ": 2 }) + dct = self.TestDict({"BIZ": 1, "BUZ": 2}) self.assertEqual(1, dct["BIZ"]) self.assertEqual(2, dct["BUZ"]) @@ -42,7 +42,7 @@ class TestTranslatingDict(TestCase): self.assertEqual(True, self.dct["BAR"]) def test_value_translation(self): - dct = self.TestDict({ " Baz": 41 }) + dct = self.TestDict({" Baz": 41}) self.assertEqual(42, dct["BAZ"]) @@ -96,8 +96,8 @@ class TestSettingsDictBuilder(TestCase): self.assertEqual({}, client.transport._http.proxies) self.assertEqual(DEFAULT_API_HOST, client.transport.api_host) - self.assertEqual(APIClient.MED_AUDIO_QUALITY, - client.default_audio_quality) + self.assertEqual( + APIClient.MED_AUDIO_QUALITY, client.default_audio_quality) def test_validate_client(self): client = TestSettingsDictBuilder._build_maximal() @@ -125,7 +125,7 @@ class TestFileBasedBuilder(TestCase): DEFAULT_CONFIG_FILE = "foo" def parse_config(self): - return { "USER": { "USERNAME": "U", "PASSWORD": "P" }} + return {"USER": {"USERNAME": "U", "PASSWORD": "P"}} def build_from_settings_dict(self, config): mock = Mock() diff --git a/tests/test_pandora/test_models.py b/tests/test_pandora/test_models.py index 42f1fbd..914f09e 100644 --- a/tests/test_pandora/test_models.py +++ b/tests/test_pandora/test_models.py @@ -37,7 +37,7 @@ class TestModelMetaClass(TestCase): class TestPandoraModel(TestCase): - JSON_DATA = { "field2": ["test2"], "field3": 41 } + JSON_DATA = {"field2": ["test2"], "field3": 41} class TestModel(m.PandoraModel): @@ -57,7 +57,7 @@ class TestPandoraModel(TestCase): def test_json_to_date(self): expected = datetime(2015, 7, 18, 3, 8, 17) - result = m.PandoraModel.json_to_date(None, { "time": 1437188897616 }) + result = m.PandoraModel.json_to_date(None, {"time": 1437188897616}) # Python2.7 doesn't restore microseconds and we don't care about # it anyhow so just remove it for this test self.assertEqual(expected, result.replace(microsecond=0)) @@ -113,8 +113,8 @@ class TestPandoraListModel(TestCase): JSON_DATA = { "field1": 42, "field2": [ - { "idx": "foo", "fieldS1": "Foo" }, - { "idx": "bar", "fieldS1": "Bar" }, + {"idx": "foo", "fieldS1": "Foo"}, + {"idx": "bar", "fieldS1": "Bar"}, ] } @@ -166,11 +166,12 @@ class TestPandoraDictListModel(TestCase): JSON_DATA = { "field1": 42, "fieldD1": [ - { "dictKey": "Foobear", - "listKey": [ - { "idx": "foo", "fieldS1": "Foo" }, - { "idx": "bar", "fieldS1": "Bar" }, - ] + { + "dictKey": "Foobear", + "listKey": [ + {"idx": "foo", "fieldS1": "Foo"}, + {"idx": "bar", "fieldS1": "Bar"}, + ] } ] } @@ -205,11 +206,8 @@ class TestPandoraDictListModel(TestCase): class TestPlaylistItemModel(TestCase): - AUDIO_URL_NO_MAP = { "audioUrl": "foo" } - WEIRD_FORMAT = { "audioUrlMap": { - "highQuality": { - } - }} + AUDIO_URL_NO_MAP = {"audioUrl": "foo"} + WEIRD_FORMAT = {"audioUrlMap": {"highQuality": {}}} def test_audio_url_without_map(self): item = pm.PlaylistItem.from_json(Mock(), self.AUDIO_URL_NO_MAP) @@ -248,19 +246,30 @@ class TestAdItem(TestCase): JSON_DATA = { 'audioUrlMap': { 'mediumQuality': { - 'audioUrl': 'med_url_mock', 'bitrate': '64', 'protocol': 'http', 'encoding': 'aacplus' + 'audioUrl': 'med_url_mock', + 'bitrate': '64', + 'protocol': 'http', + 'encoding': 'aacplus' }, 'highQuality': { - 'audioUrl': 'high_url_mock', 'bitrate': '64', 'protocol': 'http', 'encoding': 'aacplus' + 'audioUrl': 'high_url_mock', + 'bitrate': '64', + 'protocol': 'http', + 'encoding': 'aacplus' }, 'lowQuality': { - 'audioUrl': 'low_url_mock', 'bitrate': '32', 'protocol': 'http', 'encoding': 'aacplus'}}, - 'clickThroughUrl': 'click_url_mock', - 'imageUrl': 'img_url_mock', - 'companyName': '', - 'title': '', - 'trackGain': '0.0', - 'adTrackingTokens': ['token_1_mock', 'token_2_mock'] + 'audioUrl': 'low_url_mock', + 'bitrate': '32', + 'protocol': 'http', + 'encoding': 'aacplus' + } + }, + 'clickThroughUrl': 'click_url_mock', + 'imageUrl': 'img_url_mock', + 'companyName': '', + 'title': '', + 'trackGain': '0.0', + 'adTrackingTokens': ['token_1_mock', 'token_2_mock'] } def setUp(self): @@ -279,7 +288,7 @@ class TestAdItem(TestCase): assert self.result._api_client.register_ad.called - def test_register_ad_raises_exception_if_no_tracking_tokens_available(self): + def test_register_ad_raises_if_no_tracking_tokens_available(self): with self.assertRaises(ParameterMissing): self.result.tracking_tokens = [] self.result._api_client.register_ad = Mock(spec=pm.AdItem) @@ -299,8 +308,8 @@ class TestAdItem(TestCase): def test_prepare_playback_raises_paramater_missing(self): with patch.object(pm.PlaylistModel, 'prepare_playback') as super_mock: - self.result.register_ad = Mock(side_effect=ParameterMissing('No ad tracking tokens provided for ' - 'registration.') + self.result.register_ad = Mock(side_effect=ParameterMissing( + 'No ad tracking tokens provided for registration.') ) self.assertRaises(ParameterMissing, self.result.prepare_playback) assert self.result.register_ad.called @@ -310,8 +319,8 @@ class TestAdItem(TestCase): with patch.object(pm.PlaylistModel, 'prepare_playback') as super_mock: self.result.tracking_tokens = [] - self.result.register_ad = Mock(side_effect=ParameterMissing('No ad tracking tokens provided for ' - 'registration.')) + self.result.register_ad = Mock(side_effect=ParameterMissing( + 'No ad tracking tokens provided for registration.')) self.result.prepare_playback() assert self.result.register_ad.called assert super_mock.called @@ -354,7 +363,8 @@ class TestSearchResultItem(TestCase): def setUp(self): self.api_client_mock = Mock(spec=APIClient) - self.api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY + self.api_client_mock.default_audio_quality = \ + APIClient.HIGH_AUDIO_QUALITY def test_is_song(self): result = pm.SearchResultItem.from_json( @@ -412,17 +422,22 @@ class TestArtistSearchResultItem(TestCase): def setUp(self): self.api_client_mock = Mock(spec=APIClient) - self.api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY + self.api_client_mock.default_audio_quality = \ + APIClient.HIGH_AUDIO_QUALITY def test_repr(self): result = pm.SearchResultItem.from_json( self.api_client_mock, self.ARTIST_JSON_DATA) - expected = ("ArtistSearchResultItem(artist='artist_name_mock', likely_match=False, score=100, token='R0000000')") + expected = ( + "ArtistSearchResultItem(artist='artist_name_mock', " + "likely_match=False, score=100, token='R0000000')") self.assertEqual(expected, repr(result)) result = pm.SearchResultItem.from_json( self.api_client_mock, self.COMPOSER_JSON_DATA) - expected = ("ArtistSearchResultItem(artist='composer_name_mock', likely_match=False, score=100, token='C0000000')") + expected = ( + "ArtistSearchResultItem(artist='composer_name_mock', " + "likely_match=False, score=100, token='C0000000')") self.assertEqual(expected, repr(result)) def test_create_station(self): @@ -431,7 +446,8 @@ class TestArtistSearchResultItem(TestCase): result._api_client.create_station = Mock() result.create_station() - result._api_client.create_station.assert_called_with(artist_token=result.token) + result._api_client.create_station.assert_called_with( + artist_token=result.token) class TestSongSearchResultItem(TestCase): @@ -445,12 +461,15 @@ class TestSongSearchResultItem(TestCase): def setUp(self): self.api_client_mock = Mock(spec=APIClient) - self.api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY + self.api_client_mock.default_audio_quality = \ + APIClient.HIGH_AUDIO_QUALITY def test_repr(self): result = pm.SearchResultItem.from_json( self.api_client_mock, self.SONG_JSON_DATA) - expected = ("SongSearchResultItem(artist='artist_name_mock', score=100, song_name='song_name_mock', token='S0000000')") + expected = ( + "SongSearchResultItem(artist='artist_name_mock', score=100, " + "song_name='song_name_mock', token='S0000000')") self.assertEqual(expected, repr(result)) def test_create_station(self): @@ -459,7 +478,8 @@ class TestSongSearchResultItem(TestCase): result._api_client.create_station = Mock() result.create_station() - result._api_client.create_station.assert_called_with(track_token=result.token) + result._api_client.create_station.assert_called_with( + track_token=result.token) class TestGenreStationSearchResultItem(TestCase): @@ -472,12 +492,15 @@ class TestGenreStationSearchResultItem(TestCase): def setUp(self): self.api_client_mock = Mock(spec=APIClient) - self.api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY + self.api_client_mock.default_audio_quality = \ + APIClient.HIGH_AUDIO_QUALITY def test_repr(self): result = pm.SearchResultItem.from_json( self.api_client_mock, self.GENRE_JSON_DATA) - expected = ("GenreStationSearchResultItem(score=100, station_name='station_name_mock', token='G0000000')") + expected = ( + "GenreStationSearchResultItem(score=100, " + "station_name='station_name_mock', token='G0000000')") self.assertEqual(expected, repr(result)) def test_create_station(self): @@ -486,7 +509,8 @@ class TestGenreStationSearchResultItem(TestCase): result._api_client.create_station = Mock() result.create_station() - result._api_client.create_station.assert_called_with(search_token=result.token) + result._api_client.create_station.assert_called_with( + search_token=result.token) class TestSearchResult(TestCase): @@ -516,14 +540,20 @@ class TestSearchResult(TestCase): def setUp(self): api_client_mock = Mock(spec=APIClient) api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY - self.result = pm.SearchResult.from_json(api_client_mock, self.JSON_DATA) + self.result = pm.SearchResult.from_json( + api_client_mock, self.JSON_DATA) def test_repr(self): - expected = ("SearchResult(artists=[ArtistSearchResultItem(artist='artist_mock', likely_match=False, score=80, " - "token='R000000')], explanation='', genre_stations=[GenreStationSearchResultItem(score=50, " - "station_name='station_mock', token='G0000')], nearest_matches_available=True, " - "songs=[SongSearchResultItem(artist='song_artist_mock', score=100, song_name='song_name_mock', " - "token='S0000000')])") + expected = ( + "SearchResult(artists=[ArtistSearchResultItem(" + "artist='artist_mock', likely_match=False, score=80, " + "token='R000000')], explanation='', genre_stations=[" + "GenreStationSearchResultItem(score=50, " + "station_name='station_mock', token='G0000')], " + "nearest_matches_available=True, " + "songs=[SongSearchResultItem(artist='song_artist_mock', " + "score=100, song_name='song_name_mock', " + "token='S0000000')])") self.assertEqual(expected, repr(self.result)) @@ -532,7 +562,7 @@ class TestGenreStationList(TestCase): TEST_DATA = { "checksum": "bar", "categories": [ - { "categoryName": "foo", "stations": [] }, + {"categoryName": "foo", "stations": []}, ] } @@ -561,8 +591,8 @@ class TestStationList(TestCase): class TestBookmark(TestCase): - SONG_BOOKMARK = { "songName": "foo", "bookmarkToken": "token" } - ARTIST_BOOKMARK = { "artistName": "foo", "bookmarkToken": "token" } + SONG_BOOKMARK = {"songName": "foo", "bookmarkToken": "token"} + ARTIST_BOOKMARK = {"artistName": "foo", "bookmarkToken": "token"} def setUp(self): self.client = Mock() diff --git a/tests/test_pandora/test_transport.py b/tests/test_pandora/test_transport.py index 08e3292..ff9c572 100644 --- a/tests/test_pandora/test_transport.py +++ b/tests/test_pandora/test_transport.py @@ -164,7 +164,7 @@ class TestParseResponse(TestCase): def test_with_valid_response(self): res = self.transport._parse_response(self.VALID_MSG_JSON) - self.assertEqual({ "foo": "bar" }, res) + self.assertEqual({"foo": "bar"}, res) def test_with_valid_response_no_body(self): res = self.transport._parse_response(self.VALID_MSG_NO_BODY_JSON) @@ -208,9 +208,9 @@ class TestTransportRequestPrep(TestCase): self.transport._http = http res = self.transport._make_http_request( - "/url", b"data", { "a":None, "b":"c" }) + "/url", b"data", {"a": None, "b": "c"}) - http.post.assert_called_with("/url", data=b"data", params={"b":"c"}) + http.post.assert_called_with("/url", data=b"data", params={"b": "c"}) retval.raise_for_status.assert_called_with() self.assertEqual("foo", res) @@ -223,7 +223,7 @@ class TestTransportRequestPrep(TestCase): self.transport.start_time = 23 with patch.object(time, "time", return_value=20): - val = self.transport._build_data("foo", {"a":"b", "c":None}) + val = self.transport._build_data("foo", {"a": "b", "c": None}) val = json.loads(val) self.assertEqual("b", val["a"]) @@ -238,7 +238,7 @@ class TestTransportRequestPrep(TestCase): with patch.object(time, "time", return_value=20): val = self.transport._build_data( - t.APITransport.NO_ENCRYPT[0], {"a":"b", "c":None}) + t.APITransport.NO_ENCRYPT[0], {"a": "b", "c": None}) val = json.loads(val) self.assertEqual("b", val["a"]) @@ -324,7 +324,7 @@ class TestEncryptor(TestCase): def test_decrypt(self): self.assertEqual( - { "foo": "bar" }, self.cryptor.decrypt(self.ENCODED_JSON)) + {"foo": "bar"}, self.cryptor.decrypt(self.ENCODED_JSON)) def test_encrypt(self): self.assertEqual( diff --git a/tests/test_pandora/test_util.py b/tests/test_pandora/test_util.py new file mode 100644 index 0000000..2b4ca83 --- /dev/null +++ b/tests/test_pandora/test_util.py @@ -0,0 +1,22 @@ +import warnings +from unittest import TestCase +from pandora.py2compat import patch + +from pandora import util + + +class TestDeprecatedWarning(TestCase): + + def test_warning(self): + class Bar(object): + + @util.deprecated("1.0", "2.0", "Don't use this") + def foo(self): + pass + + with patch.object(warnings, "warn") as wmod: + Bar().foo() + + wmod.assert_called_with( + ("foo is deprecated as of version 1.0 and will be removed in " + "version 2.0. Don't use this"), DeprecationWarning) diff --git a/tests/test_pandora/test_utils.py b/tests/test_pandora/test_utils.py deleted file mode 100644 index 2b4ca83..0000000 --- a/tests/test_pandora/test_utils.py +++ /dev/null @@ -1,22 +0,0 @@ -import warnings -from unittest import TestCase -from pandora.py2compat import patch - -from pandora import util - - -class TestDeprecatedWarning(TestCase): - - def test_warning(self): - class Bar(object): - - @util.deprecated("1.0", "2.0", "Don't use this") - def foo(self): - pass - - with patch.object(warnings, "warn") as wmod: - Bar().foo() - - wmod.assert_called_with( - ("foo is deprecated as of version 1.0 and will be removed in " - "version 2.0. Don't use this"), DeprecationWarning) diff --git a/tests/test_pydora/test_utils.py b/tests/test_pydora/test_utils.py index 5334ff6..9900913 100644 --- a/tests/test_pydora/test_utils.py +++ b/tests/test_pydora/test_utils.py @@ -16,11 +16,16 @@ class TestIterateForever(TestCase): def test_handle_missing_params_exception_due_to_missing_ad_tokens(self): with patch.object(APIClient, 'get_playlist') as get_playlist_mock: - with patch.object(APIClient, 'register_ad', side_effect=ParameterMissing("ParameterMissing")): + admock = patch.object( + APIClient, 'register_ad', + side_effect=ParameterMissing("ParameterMissing")) - station = Station.from_json(self.client, {'stationToken': 'token_mock'}) - ad_mock = AdItem.from_json(self.client, {'station_id': 'id_mock'}) - get_playlist_mock.return_value=iter([ad_mock]) + with admock: + station = Station.from_json( + self.client, {'stationToken': 'token_mock'}) + ad_mock = AdItem.from_json( + self.client, {'station_id': 'id_mock'}) + get_playlist_mock.return_value = iter([ad_mock]) station_iter = iterate_forever(station.get_playlist) @@ -28,12 +33,17 @@ class TestIterateForever(TestCase): self.assertEqual(ad_mock, next_track) def test_reraise_missing_params_exception(self): - with patch.object(APIClient, 'get_playlist', side_effect=ParameterMissing("ParameterMissing")) as get_playlist_mock: - with self.assertRaises(ParameterMissing): + plmock = patch.object( + APIClient, 'get_playlist', + side_effect=ParameterMissing("ParameterMissing")) - station = Station.from_json(self.client, {'stationToken': 'token_mock'}) - track_mock = PlaylistItem.from_json(self.client, {'token': 'token_mock'}) - get_playlist_mock.return_value=iter([track_mock]) + with plmock as get_playlist_mock: + with self.assertRaises(ParameterMissing): + station = Station.from_json( + self.client, {'stationToken': 'token_mock'}) + track_mock = PlaylistItem.from_json( + self.client, {'token': 'token_mock'}) + get_playlist_mock.return_value = iter([track_mock]) station_iter = iterate_forever(station.get_playlist) next(station_iter) -- cgit v1.2.3