diff --git a/test/data/3a233553cfa62e6fab129ec23144e97b b/test/data/3a233553cfa62e6fab129ec23144e97b new file mode 100644 index 0000000..7e899b3 --- /dev/null +++ b/test/data/3a233553cfa62e6fab129ec23144e97b @@ -0,0 +1,18 @@ + + + + Supersonic + Oasis + 2038492 + 2c2a24de-67b6-483b-b597-9c6b7891ba90 + http://www.last.fm/music/Oasis/Supersonic + 28 Jul 1994, 00:00 + http://userserve-ak.last.fm/serve/34/11846565.jpg + http://userserve-ak.last.fm/serve/64/11846565.jpg + http://userserve-ak.last.fm/serve/174s/11846565.jpg + http://userserve-ak.last.fm/serve/300x300/11846565.jpg + 14286 + 39594 + + + diff --git a/test/test_album.py b/test/test_album.py index 6edaafd..84d4440 100644 --- a/test/test_album.py +++ b/test/test_album.py @@ -1,40 +1,67 @@ import unittest import datetime import sys + +from wsgi_intercept.urllib2_intercept import install_opener +import wsgi_intercept +from wsgi_test_app import create_wsgi_app + +install_opener() +wsgi_intercept.add_wsgi_intercept('ws.audioscrobbler.com', 80, create_wsgi_app) + sys.path.append("..") - from src import Api -apikey = "152a230561e72192b8b0f3e42362c6ff" - class TestAlbum(unittest.TestCase): """ A test class for the Album module. """ def setUp(self): - self.api_test = Api(apikey) - self.album_test = self.api_test.getAlbum("Oasis", "Supersonic") - + apikey = "152a230561e72192b8b0f3e42362c6ff" + self.api = Api(apikey, no_cache = True) + self.album = self.api.getAlbum("Oasis", "Supersonic") + + def tearDown(self): + pass + def testAlbumName(self): - self.assertEqual(self.album_test.name, "Supersonic") + self.assertEqual(self.album.name, "Supersonic") def testAlbumArtist(self): - self.assertEqual(self.album_test.artist.name, "Oasis") - - def testAlbumUrl(self): - url = 'http://www.last.fm/music/Oasis/Supersonic' - self.assertEqual(self.album_test.url, url) - - def testAlbumReleaseDate(self): - date = datetime.datetime(1994, 7, 28, 0, 0) - self.assertEqual(self.album_test.releaseDate, date) + self.assertEqual(self.album.artist.name, "Oasis") + + def testAlbumId(self): + self.assertEqual(self.album.id, 2038492) def testAlbumMbid(self): mbid = '2c2a24de-67b6-483b-b597-9c6b7891ba90' - self.assertEqual(self.album_test.mbid, mbid) + self.assertEqual(self.album.mbid, mbid) - def testAlbumId(self): - id = 2038492 - self.assertEqual(self.album_test.id, id) + def testAlbumUrl(self): + url = 'http://www.last.fm/music/Oasis/Supersonic' + self.assertEqual(self.album.url, url) + + def testAlbumReleaseDate(self): + date = datetime.datetime(1994, 7, 28, 0, 0) + self.assertEqual(self.album.releaseDate, date) + + def testAlbumImage(self): + self.assertEqual(self.album.image['small'], "http://userserve-ak.last.fm/serve/34/11846565.jpg") + self.assertEqual(self.album.image['medium'], "http://userserve-ak.last.fm/serve/64/11846565.jpg") + self.assertEqual(self.album.image['large'], "http://userserve-ak.last.fm/serve/174s/11846565.jpg") + self.assertEqual(self.album.image['extralarge'], "http://userserve-ak.last.fm/serve/300x300/11846565.jpg") + + def testAlbumStats(self): + self.assertEqual(self.album.stats.listeners, 14286) + self.assertEqual(self.album.stats.playcount, 39594) + + def testAlbumTopTags(self): + pass + + def testAlbumTopTag(self): + pass + + def testAlbumPlaylist(self): + self.assertEqual(self.album.playlist.url, "lastfm://playlist/album/2038492") if __name__ == '__main__': unittest.main() diff --git a/test/wsgi_test_app.py b/test/wsgi_test_app.py new file mode 100644 index 0000000..44e352e --- /dev/null +++ b/test/wsgi_test_app.py @@ -0,0 +1,46 @@ +from urlparse import urlunparse +import md5 +import os +""" +A simple WSGI application for testing. +""" + +_app_was_hit = False + +def success(): + return _app_was_hit + +def test_app(environ, start_response): + """Simplest possible application object""" + url = urlunparse(( + environ['wsgi.url_scheme'], + environ['HTTP_HOST'], + environ['PATH_INFO'], + '', + environ['QUERY_STRING'], + '' + )) + #print "intercepted: %s" % url + key = md5.new(url).hexdigest() + status = '200 OK' + response_headers = [('Content-type','text/xml')] + start_response(status, response_headers) + + global _app_was_hit + _app_was_hit = True + + filedata = unicode(open(os.path.join('data', key)).read()) + return [filedata] + +def create_wsgi_app(): + global _app_was_hit + _app_was_hit = False + return test_app + +if __name__ == "__main__": + from wsgi_intercept.urllib2_intercept import install_opener + install_opener() + import wsgi_intercept + wsgi_intercept.add_wsgi_intercept('ws.audioscrobbler.com', 80, create_wsgi_app) + import urllib2 + print urllib2.urlopen('http://ws.audioscrobbler.com/2.0/?album=Supersonic&api_key=152a230561e72192b8b0f3e42362c6ff&artist=Oasis&method=album.getInfo').read() \ No newline at end of file