1
|
|
|
import unittest |
|
|
|
|
2
|
|
|
import coverpy |
3
|
|
|
import httpretty |
4
|
|
|
import os |
5
|
|
|
import requests |
6
|
|
|
|
7
|
|
|
class TestCoverpy(unittest.TestCase): |
|
|
|
|
8
|
|
|
def setUp(self): |
9
|
|
|
self.coverpy = coverpy.CoverPy() |
10
|
|
|
# Set up paths... |
11
|
|
|
self.ok_computer = os.path.join(os.path.dirname(__file__), 'mocks/OKComputer.json') |
12
|
|
|
self.sugar = os.path.join(os.path.dirname(__file__), 'mocks/Maroon5Sugar.json') |
13
|
|
|
self.no_kind = os.path.join(os.path.dirname(__file__), 'mocks/SugarNoKindType.json') |
14
|
|
|
self.empty_response = os.path.join(os.path.dirname(__file__), 'mocks/SuchFakeAlbumPls.json') |
15
|
|
|
|
16
|
|
|
@httpretty.activate |
17
|
|
|
def test_get_cover_parse_album_result(self): |
|
|
|
|
18
|
|
|
# OK Computer response |
19
|
|
|
httpretty.register_uri(httpretty.GET, |
20
|
|
|
"https://itunes.apple.com/search/?limit=1&entity=musicArtist%2CmusicTrack%2Calbum%2Cmix%2Csong&term=OK+Computer&media=music", |
21
|
|
|
body=open(self.ok_computer).read(), |
22
|
|
|
content_type="application/json") |
23
|
|
|
|
24
|
|
|
# Request the content from the mock |
25
|
|
|
result = self.coverpy.get_cover("OK Computer") |
26
|
|
|
|
27
|
|
|
# Begin assertions |
28
|
|
|
self.assertEquals(result.name, "OK Computer") |
29
|
|
|
self.assertEquals(result.type, "album") |
30
|
|
|
|
31
|
|
|
@httpretty.activate |
32
|
|
|
def test_get_cover_parse_song_result(self): |
|
|
|
|
33
|
|
|
# Maroon 5 Sugar Response |
34
|
|
|
httpretty.register_uri(httpretty.GET, |
35
|
|
|
"https://itunes.apple.com/search/?limit=1&entity=musicArtist%2CmusicTrack%2Calbum%2Cmix%2Csong&term=Sugar Maroon 5&media=music", |
36
|
|
|
body=open(self.sugar).read(), |
37
|
|
|
content_type="application/json") |
38
|
|
|
|
39
|
|
|
# Request the content from the mock |
40
|
|
|
result = self.coverpy.get_cover("Sugar Maroon 5") |
41
|
|
|
|
42
|
|
|
# Begin assertions |
43
|
|
|
self.assertEquals(result.name, "Sugar") |
44
|
|
|
self.assertEquals(result.type, "song") |
45
|
|
|
|
46
|
|
|
@httpretty.activate |
47
|
|
|
def test_get_cover_no_results(self): |
|
|
|
|
48
|
|
|
# No results response |
49
|
|
|
httpretty.register_uri(httpretty.GET, |
50
|
|
|
"https://itunes.apple.com/search/?limit=1&entity=musicArtist%2CmusicTrack%2Calbum%2Cmix%2Csong&term=Such+Fake+Album+Pls&media=music", |
51
|
|
|
body=open(self.empty_response).read(), |
52
|
|
|
content_type="application/json") |
53
|
|
|
|
54
|
|
|
# Assert a NoResultsException |
55
|
|
|
with self.assertRaises(coverpy.exceptions.NoResultsException): |
56
|
|
|
self.coverpy.get_cover("Such Fake Album Pls") |
57
|
|
|
|
58
|
|
|
@httpretty.activate |
59
|
|
|
def test_get_cover_unknown_kind(self): |
|
|
|
|
60
|
|
|
# No results response |
61
|
|
|
httpretty.register_uri(httpretty.GET, |
62
|
|
|
"https://itunes.apple.com/search/?limit=1&entity=musicArtist%2CmusicTrack%2Calbum%2Cmix%2Csong&term=Sugar&media=music", |
63
|
|
|
body=open(self.no_kind).read(), |
64
|
|
|
content_type="application/json") |
65
|
|
|
|
66
|
|
|
# Assert if type is returned unknown |
67
|
|
|
result = self.coverpy.get_cover("Sugar") |
68
|
|
|
self.assertEquals(result.type, 'unknown') |
69
|
|
|
|
70
|
|
|
@httpretty.activate |
71
|
|
|
def test_for_http_status(self): |
|
|
|
|
72
|
|
|
# Test for a 500 Internal Server Error |
73
|
|
|
httpretty.register_uri(httpretty.GET, |
74
|
|
|
"https://itunes.apple.com/search/?limit=1&entity=musicArtist%2CmusicTrack%2Calbum%2Cmix%2Csong&term=OK+Computer&media=music", |
75
|
|
|
body="501 Internal Server Error", |
76
|
|
|
status=500) |
77
|
|
|
|
78
|
|
|
with self.assertRaises(requests.exceptions.HTTPError): |
79
|
|
|
self.coverpy.get_cover("OK Computer") |
80
|
|
|
|
81
|
|
|
@httpretty.activate |
82
|
|
|
def test_result_artwork_generator(self): |
|
|
|
|
83
|
|
|
# OK Computer response |
84
|
|
|
httpretty.register_uri(httpretty.GET, |
85
|
|
|
"https://itunes.apple.com/search/?limit=1&entity=musicArtist%2CmusicTrack%2Calbum%2Cmix%2Csong&term=OK+Computer&media=music", |
86
|
|
|
body=open(self.ok_computer).read(), |
87
|
|
|
content_type="application/json") |
88
|
|
|
|
89
|
|
|
# Request the content from the mock |
90
|
|
|
result = self.coverpy.get_cover("OK Computer") |
91
|
|
|
|
92
|
|
|
# Begin assertions |
93
|
|
|
self.assertTrue("600x600" in result.artwork(600)) |
94
|
|
|
|
95
|
|
|
if __name__ == '__main__': |
96
|
|
|
unittest.main() |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.