A plugin to use tango.info with MusicBrainz picard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

158 lines
4.2 KiB

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Testing for tangoinfo.py, with some fixes to get around the wrong version
error when running headless.
python2 only since picard is not py3k ready yet.
Run with `pytest tests.py`
"""
# Python3 compatability
try:
unicode
except NameError:
unicode = str
try:
from urllib.request import urlopen # python3
except ImportError:
from urllib2 import urlopen # python2
class FakeModule(object):
"""
Fake the picard.metadata module so that the import does not fail
"""
@staticmethod
def register_track_metadata_processor(dummy):
return True
# Path manipulation
import sys
sys.path.insert(0, '/usr/lib/picard') # Linux
import picard
sys.modules['picard.metadata'] = FakeModule()
# Finally
import tangoinfo
class FakeTagger(tangoinfo.TangoInfoTagger):
"""
Fake the TangoInfoTagger class to intercept some calls
"""
def website_add_track(self, album, track, barcode, tint, zeros=0):
self.album_add_request(album)
if self.albumpage_queue.append(barcode, (track, album, tint)):
host = 'tango.info'
path = '/%s' % (barcode)
response = urlopen('https://%s%s' % (host, path)).read()
return self.website_process(barcode, zeros, response, None, False)
class FakeTrack(object):
"""
A fake track object providing dummy iterfiles() and metadata.
"""
def __init__(self, id_, metadata=None):
assert isinstance(metadata, dict)
self.metadata = metadata
self._id = id_
def iterfiles(self, dummy):
# No need to test writing to files
return []
def __repr__(self):
return '<FakeTrack %s> %s' % (self._id, unicode(self.metadata))
class FakeAlbum(object):
def __init__(self, id_, new_tracks):
self._id = id_
self._requests = 0
self._new_tracks = new_tracks
def _finalize_loading(self, dummy):
return
# Test album:
# 23 Grandes Exitos by Juan D'Arienzo
# http://musicbrainz.org/release/65617e20-d445-41a3-a476-9868e6154317
# Barcode: 7798145106390
# tango.info: https://tango.info/07798145106390
# Metadata:
# 23 tracks, genre and performance date given but no vocalists
def test_barcode_process_metadata():
"""
Test barcode_process_metadata()
"""
taggerclass = tangoinfo.TangoInfoTagger()
barcode = '7798145106390'
url = 'https://tango.info/07798145106390'
page = urlopen(url).read()
albuminfo = taggerclass.barcode_process_metadata(barcode, page)
assert albuminfo['07798145106390-1-19'].get('date') == u'1937-12-09'
assert len(albuminfo.keys()) == 23
return sorted(albuminfo.items())
def test_add_tangoinfo_data():
"""
Test add_tangoinfo_data()
"""
taggerclass = FakeTagger()
barcode = '7798145106390'
trackxmlnode = 'fakexmlnode'
releasexmlnode = 'fakereleasenode'
# Set up fake album
albumtracks = []
for i in range(1, 24):
fakealbumtrack = FakeTrack(id_='%s-1-%d' % (barcode, i),
metadata=dict(tracknumber=i, barcode=barcode,
discnumber=1))
albumtracks.append(fakealbumtrack)
album = FakeAlbum(id_='fakeid', new_tracks=albumtracks)
# Run plugin
loaded_tracks = []
for i in range(1, 24):
taggerclass.add_tangoinfo_data(album, album._new_tracks[-1].metadata,
trackxmlnode, releasexmlnode)
loaded_track = album._new_tracks.pop()
loaded_tracks.append(loaded_track)
loaded_tracks = sorted(loaded_tracks,
key=lambda track: track.metadata['tracknumber'])
# Start assertions
# Track 1: Don Juan
assert loaded_tracks[0].metadata.get('genre') == u'Tango'
# Track 2: Pasión
assert loaded_tracks[1].metadata.get('genre') == u'Vals'
# Track 22: Valsecito de antes
assert loaded_tracks[21].metadata.get('date') == u'1937-08-31'
# Output test data for visual inspection
return unicode('\n'.join([unicode(t) for t in loaded_tracks]))
if __name__ == '__main__':
pass
# Run these to debug
#print(test_add_tangoinfo_data())
#print(test_barcode_process_metadata())