-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_pyjamendo.py
206 lines (177 loc) · 5.66 KB
/
test_pyjamendo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Tests for pyjamendo
# Requires py.test
import pytest
import pyjamendo
from pyjamendo import call_api, jam_radios_to_m3u, main
def fake_urlopen(url):
from io import BytesIO
if "client_id=56d30c95" not in url:
result = BytesIO(SAMPLE_INVALID_CLIENT_ID)
elif "radios/?" in url:
result = BytesIO(SAMPLE_RADIOS_RESP)
elif "name=bestof" in url:
result = BytesIO(SAMPLE_RADIO_ENTRY_BESTOF)
elif "name=electro" in url:
result = BytesIO(SAMPLE_RADIO_ENTRY_ELECTRO)
return result
@pytest.fixture(scope="function")
def jamendo_api(request, monkeypatch):
"""This fixture simulates the Jamendo API.
Replaces 'urlopen' temporarily.
"""
monkeypatch.setattr(pyjamendo, 'urlopen', fake_urlopen)
SAMPLE_RADIOS_RESP = b'''
{
"headers":{
"status":"success",
"code":0,
"error_message":"",
"warnings":"",
"results_count":10
},
"results":[
{
"id":1,
"name":"bestof",
"dispname":"Best Of Jamendo Radio",
"type":"www",
"image":"https:\/\/imgjam2.jamendo.com\/new_jamendo_radios\/bestof150.jpg"
},
{
"id":2,
"name":"electro",
"dispname":"Electronic Radio",
"type":"www",
"image":"https:\/\/imgjam1.jamendo.com\/new_jamendo_radios\/electro150.jpg"
}
]
}
'''
SAMPLE_RADIO_ENTRY_BESTOF = b'''
{
"headers":{
"status":"success",
"code":0,
"error_message":"",
"warnings":"",
"results_count":1
},
"results":[
{
"id":1,
"name":"bestof",
"dispname":"Best Of Jamendo Radio",
"type":"www",
"image":"https:\/\/imgjam1.jamendo.com\/new_jamendo_radios\/bestof150.jpg",
"stream":"https:\/\/streaming.jamendo.com\/JamBestOf",
"playingnow":{
"track_id":"132859",
"artist_id":"338094",
"album_id":"18775",
"album_name":"royal goulasch",
"track_name":"La petite reine",
"track_image":
"https:\/\/imgjam.jamendo.com\/albums\/s18\/18775\/covers\/1.200.jpg",
"artist_name":"Royal Goulasch"
},
"callmeback":"82000"
}
]
}
'''
SAMPLE_RADIO_ENTRY_ELECTRO = b'''
{
"headers":{
"status":"success",
"code":0,
"error_message":"",
"warnings":"",
"results_count":1
},
"results":[
{
"id":2,
"name":"electro",
"dispname":"Electronic Radio",
"type":"www",
"image":"https:\/\/imgjam1.jamendo.com\/new_jamendo_radios\/electro150.jpg",
"stream":"https:\/\/streaming.jamendo.com\/JamElectro",
"playingnow":{
"track_id":"552164",
"artist_id":"358851",
"album_id":"64251",
"album_name":"Gotta hold On",
"track_name":"Star (EM's Radio-Friendly Mix)",
"track_image":
"https:\/\/imgjam.jamendo.com\/albums\/s64\/64251\/covers\/1.200.jpg",
"artist_name":"eddie e feat eliza"
},
"callmeback":"54000"
}
]
}
'''
SAMPLE_INVALID_CLIENT_ID = b'''{
"headers":{
"status":"failed",
"code":5,
"error_message":
"Jamendo Api Invalid Client Id Error: Your credential is not authorized.",
"warnings":"",
"results_count":0
},
"results":[
]
}
'''
def test_jam_radios_to_m3u(capfd, jamendo_api):
# we can generate a list of jamendo radio stations
jam_radios_to_m3u()
out, err = capfd.readouterr()
assert out.startswith("#EXTM3U")
assert "EXTINF:-1, " in out
def test_allow_https_links_option_default(capfd, jamendo_api):
# https_lins option default works
jam_radios_to_m3u() # default
out, err = capfd.readouterr()
assert "https:" in out
assert "http:" not in out
def test_allow_https_links_option_on(capfd, jamendo_api):
# https_lins option can be switched on
jam_radios_to_m3u(allow_https_links=True)
out, err = capfd.readouterr()
assert "https:" in out
assert "http:" not in out
def test_allow_https_links_option_off(capfd, jamendo_api):
# https_lins option can be switched off
jam_radios_to_m3u(allow_https_links=False)
out, err = capfd.readouterr()
assert "https:" not in out
assert "http:" in out
def test_call_api_delivers_parsed_json_data(jamendo_api):
# we can call the jamendo API and will get parsed JSON data.
result = call_api(
"radios/stream", dict(name="electro"))
assert isinstance(result, dict)
assert "headers" in result
assert isinstance(result["results"], list)
def test_call_api_invalid_client_id(jamendo_api):
# we can see if a client id is invalid
result = call_api(
"radios/stream", dict(name="electro"), client_id="nonsense")
assert result["headers"]["status"] == "failed"
assert result["headers"]["code"] == 5
assert result["headers"]["results_count"] == 0
def test_call_api_valid_client_id(jamendo_api):
# we can see if a client id is valid
result = call_api(
"radios/stream", dict(name="electro"), client_id="56d30c95")
assert result["headers"]["status"] == "success"
assert result["headers"]["code"] == 0
assert result["headers"]["results_count"] > 0
def test_main(capfd, jamendo_api):
# we can call main() w/o hassle
main()
out, err = capfd.readouterr()
assert "EXTINF:-1, " in out
assert "https:" not in out