added some more tests to test_album module. created wsgi_test_app module and data dir to intercept urllib2 requests and serve from local files.
This commit is contained in:
parent
f08c12a1ff
commit
0df01e9ada
18
test/data/3a233553cfa62e6fab129ec23144e97b
Normal file
18
test/data/3a233553cfa62e6fab129ec23144e97b
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<lfm status="ok">
|
||||
<album>
|
||||
<name>Supersonic</name>
|
||||
<artist>Oasis</artist>
|
||||
<id>2038492</id>
|
||||
<mbid>2c2a24de-67b6-483b-b597-9c6b7891ba90</mbid>
|
||||
<url>http://www.last.fm/music/Oasis/Supersonic</url>
|
||||
<releasedate> 28 Jul 1994, 00:00</releasedate>
|
||||
<image size="small">http://userserve-ak.last.fm/serve/34/11846565.jpg</image>
|
||||
<image size="medium">http://userserve-ak.last.fm/serve/64/11846565.jpg</image>
|
||||
<image size="large">http://userserve-ak.last.fm/serve/174s/11846565.jpg</image>
|
||||
<image size="extralarge">http://userserve-ak.last.fm/serve/300x300/11846565.jpg</image>
|
||||
<listeners>14286</listeners>
|
||||
<playcount>39594</playcount>
|
||||
<toptags>
|
||||
</toptags>
|
||||
</album></lfm>
|
@ -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()
|
||||
|
46
test/wsgi_test_app.py
Normal file
46
test/wsgi_test_app.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user