Browse Source

Fix tests for py3/picard2, add test data, offline

master
Felix 5 months ago
parent
commit
81106d1233
  1. 8
      README.md
  2. 121
      testdata/23-grandes-exitos.html
  3. 131
      tests.py

8
README.md

@ -32,6 +32,14 @@ See [tango.info wiki: TINT](https://tango.info/wiki/TINT)
for a Product* is a 14-digit numeric tango.info code used by tango.info and
others.
## Testing
Run `pytest tests.py`.
Sample data obtained from tango.info via:
```
wget 'https://tango.info/07798145106390' -O testdata/23-grandes-exitos.html
```
## Future
Provided tango.info's [Tobias Conradi](https://tango.info/tangoinfo/eng/contact)
does not change the page structure in a manner that breaks this plugin, you

121
testdata/23-grandes-exitos.html vendored

File diff suppressed because one or more lines are too long

131
tests.py

@ -1,23 +1,13 @@
# -*- coding: utf-8 -*-
# TODO Fix these
"""
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
import os
from urllib.request import urlopen
class FakeModule(object):
@ -29,13 +19,46 @@ class FakeModule(object):
return True
# Path manipulation
import sys
sys.path.insert(0, '/usr/lib/picard') # Linux
import picard
import sys # noqa
import picard # noqa
sys.modules['picard.metadata'] = FakeModule()
# Finally
import tangoinfo
# Importing tangoinfo plugin relies on (mocked) picard module being present
import tangoinfo # noqa
sample_albums = {
# Key = barcode
'7798145106390': {
'shortname': '23-grandes-exitos',
'offline_file': '23-grandes-exitos.html',
'url': 'https://tango.info/07798145106390',
'barcode': '7798145106390',
'tinp': '07798145106390', # NOT TINT!
'nr_of_tracks': 23,
},
}
class FakeDataObject(): # noqa
def __init__(self, data):
self._data = data
def data(self): # noqa
return self._data
def get_album(barcode, offline=True):
if offline:
fp = os.path.join("testdata", sample_albums[barcode]['offline_file'])
# Read in binary mode
with open(fp, 'rb') as f:
fakedataobj = FakeDataObject(f.read())
# return bytes (emulating a QByteArray)
return fakedataobj
else:
url = sample_albums[barcode]['url']
# urlopen.read() will already return a stream of bytes
response = urlopen(url).read()
fakedataobj = FakeDataObject(response)
return fakedataobj
class FakeTagger(tangoinfo.TangoInfoTagger):
@ -46,12 +69,16 @@ class FakeTagger(tangoinfo.TangoInfoTagger):
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 = get_album(barcode, offline=True)
response = urlopen('https://%s%s' % (host, path)).read()
return self.website_process(barcode, zeros, response, None, False)
if self.albumpage_queue.append(barcode, (track, album, tint)):
return self.website_process(
barcode=barcode,
zeros=zeros,
response_bytes=response,
reply=None,
error=False
)
class FakeTrack(object):
@ -64,16 +91,16 @@ class FakeTrack(object):
self.metadata = metadata
self._id = id_
def iterfiles(self, dummy):
def iterfiles(self):
# No need to test writing to files
return []
def __repr__(self):
fmtstr = u"Disc: %s, Track: %2s, Barcode: %s, Genre: %7s, Date: %10s"
fmtstr = "Disc: %s, Track: %2s, Barcode: %s, Genre: %7s, Date: %10s"
return '<FakeTrack %-18s> %s' % (
self._id,
fmtstr % tuple(map(
lambda x: unicode(self.metadata.get(x, '')),
lambda x: str(self.metadata.get(x, '')),
('discnumber', 'tracknumber', 'barcode', 'genre', 'date')
))
)
@ -97,20 +124,20 @@ class FakeAlbum(object):
# Metadata:
# 23 tracks, genre and performance date given but no vocalists
def test_barcode_process_metadata():
def test_extract_data():
"""
Test barcode_process_metadata()
Test extract_data()
"""
taggerclass = tangoinfo.TangoInfoTagger()
barcode = '7798145106390'
url = 'https://tango.info/07798145106390'
page = urlopen(url).read()
page = get_album(barcode, offline=True).data().decode('utf-8')
albuminfo = taggerclass.barcode_process_metadata(barcode, page)
albuminfo = taggerclass.extract_data(barcode, page)
assert albuminfo['07798145106390-1-19'].get('date') == u'1937-12-09'
assert albuminfo['7798145106390-1-19'].get('date') == '1937-12-09'
assert len(albuminfo.keys()) == 23
# For visual inspection
return "\n".join(str(i) for i in sorted(albuminfo.items()))
@ -120,24 +147,29 @@ def test_add_tangoinfo_data():
"""
taggerclass = FakeTagger()
barcode = '7798145106390'
trackxmlnode = 'fakexmlnode'
releasexmlnode = 'fakereleasenode'
track = 'faketrack'
release = 'fakerelease'
nr_of_tracks = sample_albums[barcode]['nr_of_tracks']
disc_nr = 1
# 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))
for i in range(1, (nr_of_tracks + 1)):
fakealbumtrack = FakeTrack(
id_='%s-%d-%d' % (barcode, disc_nr, i),
metadata=dict(tracknumber=i,
barcode=barcode,
discnumber=disc_nr),
)
albumtracks.append(fakealbumtrack)
album = FakeAlbum(id_='fakeid', new_tracks=albumtracks)
# Run plugin
loaded_tracks = []
for i in range(1, 24):
for i in range(1, (nr_of_tracks + 1)):
taggerclass.add_tangoinfo_data(album, album._new_tracks[-1].metadata,
trackxmlnode, releasexmlnode)
track, release)
loaded_track = album._new_tracks.pop()
loaded_tracks.append(loaded_track)
@ -147,25 +179,22 @@ def test_add_tangoinfo_data():
# Start assertions
# Track 1: Don Juan
assert loaded_tracks[0].metadata.get('genre') == u'Tango'
assert loaded_tracks[0].metadata.get('genre') == 'Tango'
# Track 2: Pasión
assert loaded_tracks[1].metadata.get('genre') == u'Vals'
assert loaded_tracks[1].metadata.get('genre') == 'Vals'
# Track 22: Valsecito de antes
assert loaded_tracks[21].metadata.get('date') == u'1937-08-31'
assert loaded_tracks[21].metadata.get('date') == '1937-08-31'
# Output test data for visual inspection
return unicode('\n'.join([unicode(t) for t in loaded_tracks]))
return '\n'.join([str(t) for t in loaded_tracks])
if __name__ == '__main__':
pass
# Run these to debug
print("Testing extract_data()")
print(test_extract_data())
#print("Testing add_tangoinfo_data()")
#print(test_add_tangoinfo_data())
#print("")
print("")
#print("Testing barcode_process_metadata()")
#print(test_barcode_process_metadata())
print("Testing add_tangoinfo_data()")
print(test_add_tangoinfo_data())

Loading…
Cancel
Save