7
7
8
8
from cloudbot .bot import bot
9
9
from cloudbot .config import Config
10
+ from plugins import lastfm
10
11
from plugins .lastfm import api_request
11
12
12
13
@@ -20,37 +21,220 @@ def __init__(self, config):
20
21
self .config = MockConfig (self , config )
21
22
22
23
24
+ def test_get_account (mock_db ):
25
+ lastfm .table .create (mock_db .engine )
26
+ mock_db .add_row (lastfm .table , nick = "foo" , acc = "bar" )
27
+ lastfm .load_cache (mock_db .session ())
28
+
29
+ assert lastfm .get_account ("foo" , "baz" ) == "bar"
30
+ assert lastfm .get_account ("FOO" , "baz" ) == "bar"
31
+ assert lastfm .get_account ("foo1" , "baz" ) == "baz"
32
+ assert lastfm .get_account ("foo1" , "baa" ) == "baa"
33
+
34
+
23
35
def test_api (unset_bot ):
24
36
bot .set (MockBot ({"api_keys" : {"lastfm" : "hunter20" }}))
25
37
26
38
with RequestsMock () as reqs :
27
39
with pytest .raises (requests .ConnectionError ):
28
- api_request (' track.getTopTags' )
40
+ api_request (" track.getTopTags" )
29
41
30
- reqs .add (
31
- reqs .GET , "http://ws.audioscrobbler.com/2.0/" ,
32
- json = {"data" : "thing" }
33
- )
42
+ reqs .add (reqs .GET , "http://ws.audioscrobbler.com/2.0/" , json = {"data" : "thing" })
34
43
35
- res , _ = api_request (' track.getTopTags' )
44
+ res , _ = api_request (" track.getTopTags" )
36
45
37
46
assert res ["data" ] == "thing"
38
47
39
48
with RequestsMock () as reqs :
40
- reqs .add (
41
- reqs .GET , "http://ws.audioscrobbler.com/2.0/" ,
42
- body = "<html></html>"
43
- )
49
+ reqs .add (reqs .GET , "http://ws.audioscrobbler.com/2.0/" , body = "<html></html>" )
44
50
45
51
with pytest .raises (JSONDecodeError ):
46
52
api_request ("track.getTopTags" )
47
53
48
54
with RequestsMock () as reqs :
49
55
reqs .add (
50
- reqs .GET , "http://ws.audioscrobbler.com/2.0/" ,
56
+ reqs .GET ,
57
+ "http://ws.audioscrobbler.com/2.0/" ,
51
58
body = "<html></html>" ,
52
- status = 403
59
+ status = 403 ,
53
60
)
54
61
55
62
with pytest .raises (HTTPError ):
56
63
api_request ("track.getTopTags" )
64
+
65
+
66
+ def test_api_error_message (mock_requests , mock_api_keys ):
67
+ mock_requests .add (
68
+ "GET" ,
69
+ "http://ws.audioscrobbler.com/2.0/" ,
70
+ json = {"error" : 10 , "message" : "Invalid API Key" ,},
71
+ )
72
+
73
+ _ , error = api_request ("track.getTopTags" )
74
+
75
+ assert error == "Error: Invalid API Key."
76
+
77
+
78
+ def test_getartisttags (mock_requests , mock_api_keys ):
79
+ url = "http://ws.audioscrobbler.com/2.0/?format=json&artist=foobar&autocorrect=1&method=artist.getTopTags&api_key=APIKEY"
80
+ mock_requests .add (
81
+ "GET" , url , json = {"toptags" : {},}, match_querystring = True ,
82
+ )
83
+ res = lastfm .getartisttags ("foobar" )
84
+ assert res == "no tags"
85
+
86
+
87
+ class TestGetArtistTags :
88
+ url = "http://ws.audioscrobbler.com/2.0/?format=json&artist=foobar&autocorrect=1&method=artist.getTopTags&api_key=APIKEY"
89
+
90
+ def get_tags (self ):
91
+ return lastfm .getartisttags ("foobar" )
92
+
93
+ def test_missing_tags (self , mock_requests , mock_api_keys ):
94
+ mock_requests .add (
95
+ "GET" , self .url , json = {"toptags" : {},}, match_querystring = True ,
96
+ )
97
+ res = self .get_tags ()
98
+ assert res == "no tags"
99
+
100
+ def test_no_tags (self , mock_requests , mock_api_keys ):
101
+ mock_requests .add (
102
+ "GET" , self .url , json = {"toptags" : {"tags" : []},}, match_querystring = True ,
103
+ )
104
+ res = self .get_tags ()
105
+ assert res == "no tags"
106
+
107
+ def test_non_existent_artist (self , mock_requests , mock_api_keys ):
108
+ mock_requests .add (
109
+ "GET" ,
110
+ self .url ,
111
+ json = {"error" : 6 , "message" : "Missing artist." },
112
+ match_querystring = True ,
113
+ )
114
+ res = self .get_tags ()
115
+ assert res == "no tags"
116
+
117
+ def test_tags (self , mock_requests , mock_api_keys ):
118
+ mock_requests .add (
119
+ "GET" ,
120
+ self .url ,
121
+ json = {
122
+ "toptags" : {
123
+ "tag" : [
124
+ {"name" : name }
125
+ for name in [
126
+ "foobar" ,
127
+ "tag2" ,
128
+ "seen live" ,
129
+ "tag4" ,
130
+ "tag5" ,
131
+ "tag6" ,
132
+ "tag7" ,
133
+ ]
134
+ ]
135
+ },
136
+ },
137
+ match_querystring = True ,
138
+ )
139
+ res = self .get_tags ()
140
+ assert res == "tag2, tag4, tag5, tag6"
141
+
142
+
143
+ def test_gettracktags (mock_requests , mock_api_keys ):
144
+ url = "http://ws.audioscrobbler.com/2.0/?format=json&artist=foobar&autocorrect=1&track=foobaz&method=track.getTopTags&api_key=APIKEY"
145
+ mock_requests .add (
146
+ "GET" , url , json = {"toptags" : {}}, match_querystring = True ,
147
+ )
148
+ res = lastfm .gettracktags ("foobar" , "foobaz" )
149
+ assert res == "no tags"
150
+
151
+
152
+ class TestCheckKeyAndUser :
153
+ def test_text (self , mock_api_keys , mock_requests , mock_db ):
154
+ lastfm .table .create (mock_db .engine )
155
+ mock_db .add_row (lastfm .table , nick = "foo" , acc = "bar" )
156
+ lastfm .load_cache (mock_db .session ())
157
+
158
+ res , err = lastfm .check_key_and_user ("foo" , "baz" )
159
+ assert err is None
160
+ assert res == "baz"
161
+
162
+ def test_db_lookup (self , mock_api_keys , mock_requests , mock_db ):
163
+ lastfm .table .create (mock_db .engine )
164
+ mock_db .add_row (lastfm .table , nick = "foo" , acc = "bar" )
165
+ lastfm .load_cache (mock_db .session ())
166
+
167
+ res , err = lastfm .check_key_and_user ("foo" , "" )
168
+ assert err is None
169
+ assert res == "bar"
170
+
171
+ def test_missing_user (self , mock_api_keys , mock_requests , mock_db ):
172
+ lastfm .table .create (mock_db .engine )
173
+ mock_db .add_row (lastfm .table , nick = "foo" , acc = "bar" )
174
+ lastfm .load_cache (mock_db .session ())
175
+
176
+ res , err = lastfm .check_key_and_user ("foo1" , "" )
177
+ assert res is None
178
+ expected = "No last.fm username specified and no last.fm username is set in the database."
179
+ assert err == expected
180
+
181
+ def test_no_key (self , mock_api_keys , mock_requests , mock_db ):
182
+ bot .config .get_api_key .return_value = None
183
+ res , err = lastfm .check_key_and_user ("foo" , "baz" )
184
+ assert res is None
185
+ assert err == "Error: No API key set."
186
+
187
+
188
+ class TestTopArtists :
189
+ def test_topweek_self (self , mock_api_keys , mock_requests , mock_db ):
190
+ lastfm .table .create (mock_db .engine )
191
+ mock_db .add_row (lastfm .table , nick = "foo" , acc = "bar" )
192
+ lastfm .load_cache (mock_db .session ())
193
+
194
+ mock_requests .add (
195
+ "GET" ,
196
+ "http://ws.audioscrobbler.com/2.0/?format=json&user=bar&limit=10&period=7day&method=user.gettopartists&api_key=APIKEY" ,
197
+ match_querystring = True ,
198
+ json = {
199
+ "topartists" : {
200
+ "artist" : [
201
+ {"name" : "foo" , "playcount" : 5 },
202
+ {"name" : "bar" , "playcount" : 2 },
203
+ ]
204
+ }
205
+ },
206
+ )
207
+
208
+ out = lastfm .topweek ("" , "foo" )
209
+
210
+ assert out == "b\u200b ar's favorite artists: foo [5] bar [2] "
211
+
212
+
213
+ class TestTopTrack :
214
+ def test_toptrack_self (self , mock_api_keys , mock_requests , mock_db ):
215
+ lastfm .table .create (mock_db .engine )
216
+ mock_db .add_row (lastfm .table , nick = "foo" , acc = "bar" )
217
+ lastfm .load_cache (mock_db .session ())
218
+
219
+ mock_requests .add (
220
+ "GET" ,
221
+ "http://ws.audioscrobbler.com/2.0/?format=json&user=bar&limit=5&method=user.gettoptracks&api_key=APIKEY" ,
222
+ match_querystring = True ,
223
+ json = {
224
+ "toptracks" : {
225
+ "track" : [
226
+ {
227
+ "name" : "some song" ,
228
+ "artist" : {"name" : "some artist" },
229
+ "playcount" : 10 ,
230
+ }
231
+ ]
232
+ }
233
+ },
234
+ )
235
+
236
+ out = lastfm .toptrack ("" , "foo" )
237
+
238
+ expected = "b\u200b ar's favorite songs: some song by some artist listened to 10 times. "
239
+
240
+ assert out == expected
0 commit comments