From 5d1c97824c31493935a6c10eb2fadf83ff2d3b66 Mon Sep 17 00:00:00 2001 From: Reg Date: Sun, 23 Dec 2018 17:48:44 +0100 Subject: [PATCH] Unit tests for fetchart plugin iTunes source --- test/test_art.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/test/test_art.py b/test/test_art.py index a95cd4c95..7848ae80f 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -274,6 +274,88 @@ class AAOTest(UseThePlugin): next(self.source.get(album, self.settings, [])) +class ITunesStoreTest(UseThePlugin): + def setUp(self): + super(ITunesStoreTest, self).setUp() + self.source = fetchart.ITunesStore(logger, self.plugin.config) + self.settings = Settings() + self.album = _common.Bag(albumartist="some artist", album="some album") + + @responses.activate + def run(self, *args, **kwargs): + super(ITunesStoreTest, self).run(*args, **kwargs) + + def mock_response(self, url, json): + responses.add(responses.GET, url, body=json, + content_type='application/json') + + def test_itunesstore_finds_image(self): + json = """{ + "results": + [ + { + "artistName": "some artist", + "collectionName": "some album", + "artworkUrl100": "url_to_the_image" + } + ] + }""" + self.mock_response(fetchart.ITunesStore.API_URL, json) + candidate = next(self.source.get(self.album, self.settings, [])) + self.assertEqual(candidate.url, 'url_to_the_image') + + def test_itunesstore_no_result(self): + json = '{"results": []}' + self.mock_response(fetchart.ITunesStore.API_URL, json) + with self.assertRaises(StopIteration): + next(self.source.get(self.album, self.settings, [])) + + def test_itunesstore_bad_url(self): + self.mock_response(u'https://www.apple.com', "") + with self.assertRaises(StopIteration): + next(self.source.get(self.album, self.settings, [])) + + def test_itunesstore_returns_result_without_artist(self): + json = """{ + "results": + [ + { + "collectionName": "some album", + "artworkUrl100": "url_to_the_image" + } + ] + }""" + self.mock_response(fetchart.ITunesStore.API_URL, json) + candidate = next(self.source.get(self.album, self.settings, [])) + self.assertEqual(candidate.url, 'url_to_the_image') + + def test_itunesstore_returns_result_without_artwork(self): + json = """{ + "results": + [ + { + "artistName": "some artist", + "collectionName": "some album" + } + ] + }""" + self.mock_response(fetchart.ITunesStore.API_URL, json) + with self.assertRaises(StopIteration): + next(self.source.get(self.album, self.settings, [])) + + def test_itunesstore_returns_no_result_when_error_received(self): + json = '{"error": {"errors": [{"reason": "some reason"}]}}' + self.mock_response(fetchart.ITunesStore.API_URL, json) + with self.assertRaises(StopIteration): + next(self.source.get(self.album, self.settings, [])) + + def test_itunesstore_returns_no_result_with_malformed_response(self): + json = """bla blup""" + self.mock_response(fetchart.ITunesStore.API_URL, json) + with self.assertRaises(StopIteration): + next(self.source.get(self.album, self.settings, [])) + + class GoogleImageTest(UseThePlugin): def setUp(self): super(GoogleImageTest, self).setUp()