diff --git a/youtube/parsers/channel.py b/youtube/parsers/channel.py index a7c5906..ba13ee2 100644 --- a/youtube/parsers/channel.py +++ b/youtube/parsers/channel.py @@ -14,27 +14,27 @@ class ChannelListResponse(ResponseParser): in the response. """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['kind'] @property - @default_on_error(KeyError, None) + @default_on_error((KeyError, TypeError), None) def next_page_token(self): return self.result['nextPageToken'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def total_results(self): return self.result['pageInfo']['totalResults'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def results_per_page(self): return self.result['pageInfo']['resultsPerPage'] @@ -49,41 +49,41 @@ class Channel(ResponseParser): more info about videos api https://developers.google.com/youtube/v3/docs/playlists/list """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['kind'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def id(self): return self.result['id'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def title(self): return self.result['snippet']['title'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def description(self): return self.result['snippet']['description'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def published_at(self): return self.result['snippet']['publishedAt'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def custom_url(self): return self.result['snippet']['customUrl'] @property - @default_on_error(KeyError, ThumbnailsParser(result={})) + @default_on_error((KeyError, TypeError), ThumbnailsParser(result={})) def thumbnails(self): return ThumbnailsParser(self.result['snippet']['thumbnails']) diff --git a/youtube/parsers/parser.py b/youtube/parsers/parser.py index f128553..959dd3e 100644 --- a/youtube/parsers/parser.py +++ b/youtube/parsers/parser.py @@ -29,27 +29,27 @@ class ThumbnailsParser(ResponseParser): Youtube API parser for thumbnails. """ @property - @default_on_error(KeyError, {}) + @default_on_error((KeyError, TypeError), {}) def default(self): return self.result['default'] @property - @default_on_error(KeyError, {}) + @default_on_error((KeyError, TypeError), {}) def medium(self): return self.result['medium'] @property - @default_on_error(KeyError, {}) + @default_on_error((KeyError, TypeError), {}) def high(self): return self.result['high'] @property - @default_on_error(KeyError, {}) + @default_on_error((KeyError, TypeError), {}) def maxres(self): return self.result['maxres'] @property - @default_on_error(KeyError, {}) + @default_on_error((KeyError, TypeError), {}) def standard(self): return self.result['standard'] diff --git a/youtube/parsers/playlist.py b/youtube/parsers/playlist.py index 488912d..2022046 100644 --- a/youtube/parsers/playlist.py +++ b/youtube/parsers/playlist.py @@ -14,32 +14,32 @@ class PlaylistListResponse(ResponseParser): in the response. """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['kind'] @property - @default_on_error(KeyError, None) + @default_on_error((KeyError, TypeError), None) def next_page_token(self): return self.result['nextPageToken'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def region_code(self): return self.result['regionCode'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def total_results(self): return self.result['pageInfo']['totalResults'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def results_per_page(self): return self.result['pageInfo']['resultsPerPage'] @@ -54,46 +54,46 @@ class Playlist(ResponseParser): more info about videos api https://developers.google.com/youtube/v3/docs/playlists/list """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['kind'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def id(self): return self.result['id'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def channel_id(self): return self.result['snippet']['channelId'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def title(self): return self.result['snippet']['title'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def description(self): return self.result['snippet']['description'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def channel_title(self): return self.result['snippet']['channelTitle'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def published_at(self): return self.result['snippet']['publishedAt'] @property - @default_on_error(KeyError, ThumbnailsParser({})) + @default_on_error((KeyError, TypeError), ThumbnailsParser({})) def thumbnails(self): return ThumbnailsParser(self.result['snippet']['thumbnails']) diff --git a/youtube/parsers/search.py b/youtube/parsers/search.py index 45976cb..01c4a14 100644 --- a/youtube/parsers/search.py +++ b/youtube/parsers/search.py @@ -14,32 +14,32 @@ class SearchResponse(ResponseParser): in the response. """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['kind'] @property - @default_on_error(KeyError, None) + @default_on_error((KeyError, TypeError), None) def next_page_token(self): return self.result['nextPageToken'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def region_code(self): return self.result['regionCode'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def total_results(self): return self.result['pageInfo']['totalResults'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def results_per_page(self): return self.result['pageInfo']['resultsPerPage'] @@ -54,54 +54,54 @@ class SearchResult(ResponseParser): more info about search api https://developers.google.com/youtube/v3/docs/search/list """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['id']['kind'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def video_id(self): return self.result['id']['videoId'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def channel_id(self): try: return self.result['id']['channelId'] - except KeyError: + except (KeyError, TypeError): return self.result['snippet']['channelId'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def playlist_id(self): return self.result['id']['playlistId'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def title(self): return self.result['snippet']['title'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def description(self): return self.result['snippet']['description'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def channel_title(self): return self.result['snippet']['channelTitle'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def published_at(self): return self.result['snippet']['publishedAt'] @property - @default_on_error(KeyError, ThumbnailsParser({})) + @default_on_error((KeyError, TypeError), ThumbnailsParser({})) def thumbnails(self): return ThumbnailsParser(self.result['snippet']['thumbnails']) diff --git a/youtube/parsers/videos.py b/youtube/parsers/videos.py index 5389298..93a0d0f 100644 --- a/youtube/parsers/videos.py +++ b/youtube/parsers/videos.py @@ -14,32 +14,32 @@ class VideoListResponse(ResponseParser): in the response. """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['kind'] @property - @default_on_error(KeyError, None) + @default_on_error((KeyError, TypeError), None) def next_page_token(self): return self.result['nextPageToken'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def region_code(self): return self.result['regionCode'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def total_results(self): return self.result['pageInfo']['totalResults'] @property - @default_on_error(KeyError, 1) + @default_on_error((KeyError, TypeError), 1) def results_per_page(self): return self.result['pageInfo']['resultsPerPage'] @@ -54,51 +54,51 @@ class VideoListItemParser(ResponseParser): more info about videos api https://developers.google.com/youtube/v3/docs/videos/list """ @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def etag(self): return self.result['etag'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def kind(self): return self.result['kind'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def id(self): return self.result['id'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def channel_id(self): return self.result['snippet']['channelId'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def category_id(self): return self.result['snippet']['categoryId'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def title(self): return self.result['snippet']['title'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def description(self): return self.result['snippet']['description'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def channel_title(self): return self.result['snippet']['channelTitle'] @property - @default_on_error(KeyError, '') + @default_on_error((KeyError, TypeError), '') def published_at(self): return self.result['snippet']['publishedAt'] @property - @default_on_error(KeyError, ThumbnailsParser({})) + @default_on_error((KeyError, TypeError), ThumbnailsParser({})) def thumbnails(self): return ThumbnailsParser(self.result['snippet']['thumbnails']) diff --git a/youtube/tests/test_decorators.py b/youtube/tests/test_decorators.py new file mode 100644 index 0000000..c713107 --- /dev/null +++ b/youtube/tests/test_decorators.py @@ -0,0 +1,37 @@ +""" +Test for decorators. +""" +from unittest import TestCase +from youtube.decorators import default_on_error + + +class DecoratorTest(TestCase): + """ + Tests for decorators + """ + def test_default_on_error(self): + test_dict = { + "key1": { + "key1.1": "value" + }, + "key2": [], + } + + # Make sure key error is raised if key does not exists + with self.assertRaises(KeyError): + value = test_dict["key1"]["dummy"] + + @default_on_error(KeyError, 'default') + def value(): + return test_dict["key1"]["dummy"] + + # Make sure default is returned if key does not exist + self.assertEqual(value(), "default") + + # Make sure multiple exceptions can be caught + @default_on_error((KeyError, TypeError), 'default') + def value(): + return test_dict["key2"]["dummy"] + + # Make sure default is returned if key does not exist + self.assertEqual(value(), "default")