-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
97 lines (80 loc) · 4.02 KB
/
tests.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
import unittest
from libfm import LibFM
from libfm import LibFMError
class TestLibFM(unittest.TestCase):
def setUp(self):
self.libFM = LibFM('2b3268cef9e10b81c4359cd03b4e6373',
'f935eb35203409a4fc3f7fc37318ab3e')
#for generating not-found errors
self.fake_user = 'erheherhreherhe'
self.fake_artist = 'wdehgehrhrhrh'
#see http://www.last.fm/api/desktopauth for how to generate your own sk
self.libFM.session_key = None
def session(method):
"""Skip methods that require authentication when sk is not hardcoded"""
def wrapper(self, *args, **kwargs):
if self.libFM.session_key != None:
return method(self, *args, **kwargs)
else:
return
return wrapper
def test_named_parameters(self):
response = self.libFM.read('user.getRecentTracks', limit=5, user='rj')
self.assertTrue('recenttracks' in response,
'Named parameters should work on generated methods')
def test_no_parameters(self):
"""Making sure methods w/o parameters work fine"""
response = self.libFM.read('geo.getMetroWeeklyChartlist')
self.assertTrue('weeklychartlist' in response, 'Error in method w/o \
parameters')
def test_response_error_handling(self):
"""Checking exceptions raised from error codes"""
self.assertRaises(LibFMError, self.libFM.read, 'user.getShouts',
user=self.fake_user)
@session
def test_write_method(self):
response = self.libFM.write('artist.addTags', artist='Pearl Jam',
tags='Grunge')
if 'status' in response:
if response['status'] == 'ok':
return
self.assertTrue(False, 'Method failed to write')
def test_xml_eq_json_normal_response(self):
"""Check if XML and JSON responses are seamless"""
xml_response = self.libFM.read('artist.getTopFans', artist='Pearl Jam')
self.libFM.force_xml_responses = True
json_response = self.libFM.read('artist.getTopFans',
artist='Pearl Jam')
self.assertEqual(xml_response, json_response,
'XML and JSON requests produce different results')
def test_xml_eq_json_error_response(self):
"""Check if error containing XML and JSON responses are seamless"""
try:
self.libFM.read('artist.getTopTracks', artist=self.fake_artist)
self.fail('Call of artist.getTopTracks with fake artist name %s \
should have raised an error.' % self.fake_artist)
except LibFMError, xml_error:
self.libFM.force_xml_responses = True
try:
self.libFM.read('artist.getTopTracks', artist=self.fake_artist)
self.fail('Call of artist.getTopTracks with fake artist name \
%s should have raised an error.' % self.fake_artist)
except LibFMError, json_error:
self.assertEqual(xml_error, json_error,
'XML and JSON responses raise different errors')
class TestLibFMError(unittest.TestCase):
def test_libfmerror(self):
err0 = LibFMError('1', 'Error message')
err1 = LibFMError('1', 'Error message')
err2 = LibFMError(1, 'Error message')
err3 = LibFMError('2', 'Error message')
err4 = LibFMError('1', 'Message')
self.assertTrue(err0 == err1, 'Identical errors should be equal')
self.assertTrue(err0 == err2,
'Error codes specified as str or int should be equal')
self.assertFalse(err0 == err3,
'Errors with different error codes should be different')
self.assertFalse(err0 == err4,
'Errors with different messages should be different')
if __name__ == "__main__":
unittest.main()