Skip to content

Commit c789d31

Browse files
author
wordofglass
committed
fetchart: fix and add tests for the new behaviour
1 parent 34cdf0f commit c789d31

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

test/test_art.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,26 @@ def setUp(self):
4545
self.plugin = fetchart.FetchArtPlugin()
4646

4747

48-
class FetchImageTest(UseThePlugin):
49-
URL = 'http://example.com/test.jpg'
50-
48+
class FetchImageHelper(_common.TestCase):
49+
"""Helper mixin for mocking requests when fetching images
50+
with remote art sources.
51+
"""
5152
@responses.activate
5253
def run(self, *args, **kwargs):
53-
super(FetchImageTest, self).run(*args, **kwargs)
54+
super(FetchImageHelper, self).run(*args, **kwargs)
55+
56+
def mock_response(self, url, content_type='image/jpeg', file_type=None):
57+
IMAGEHEADER = {'image/jpeg': b'\x00' * 6 + b'JFIF',
58+
'image/png': b'\211PNG\r\n\032\n', }
59+
if file_type is None:
60+
file_type = content_type
61+
responses.add(responses.GET, url,
62+
content_type=content_type,
63+
body=IMAGEHEADER.get(file_type, b'\x00' * 32))
5464

55-
def mock_response(self, content_type):
56-
responses.add(responses.GET, self.URL,
57-
content_type=content_type)
65+
66+
class FetchImageTest(FetchImageHelper, UseThePlugin):
67+
URL = 'http://example.com/test.jpg'
5868

5969
def setUp(self):
6070
super(FetchImageTest, self).setUp()
@@ -64,17 +74,23 @@ def setUp(self):
6474
self.candidate = fetchart.Candidate(logger, url=self.URL)
6575

6676
def test_invalid_type_returns_none(self):
67-
self.mock_response('image/watercolour')
77+
self.mock_response(self.URL, 'image/watercolour')
6878
self.source.fetch_image(self.candidate, self.extra)
6979
self.assertEqual(self.candidate.path, None)
7080

7181
def test_jpeg_type_returns_path(self):
72-
self.mock_response('image/jpeg')
82+
self.mock_response(self.URL, 'image/jpeg')
7383
self.source.fetch_image(self.candidate, self.extra)
7484
self.assertNotEqual(self.candidate.path, None)
7585

7686
def test_extension_set_by_content_type(self):
77-
self.mock_response('image/png')
87+
self.mock_response(self.URL, 'image/png')
88+
self.source.fetch_image(self.candidate, self.extra)
89+
self.assertEqual(os.path.splitext(self.candidate.path)[1], b'.png')
90+
self.assertExists(self.candidate.path)
91+
92+
def test_does_not_rely_on_server_content_type(self):
93+
self.mock_response(self.URL, 'image/jpeg', 'image/png')
7894
self.source.fetch_image(self.candidate, self.extra)
7995
self.assertEqual(os.path.splitext(self.candidate.path)[1], b'.png')
8096
self.assertExists(self.candidate.path)
@@ -128,7 +144,7 @@ def test_precedence_amongst_correct_files(self):
128144
self.assertEqual(candidates, paths)
129145

130146

131-
class CombinedTest(UseThePlugin):
147+
class CombinedTest(FetchImageHelper, UseThePlugin):
132148
ASIN = 'xxxx'
133149
MBID = 'releaseid'
134150
AMAZON_URL = 'http://images.amazon.com/images/P/{0}.01.LZZZZZZZ.jpg' \
@@ -143,13 +159,6 @@ def setUp(self):
143159
self.dpath = os.path.join(self.temp_dir, b'arttest')
144160
os.mkdir(self.dpath)
145161

146-
@responses.activate
147-
def run(self, *args, **kwargs):
148-
super(CombinedTest, self).run(*args, **kwargs)
149-
150-
def mock_response(self, url, content_type='image/jpeg'):
151-
responses.add(responses.GET, url, content_type=content_type)
152-
153162
def test_main_interface_returns_amazon_art(self):
154163
self.mock_response(self.AMAZON_URL)
155164
album = _common.Bag(asin=self.ASIN)

0 commit comments

Comments
 (0)