Skip to content

Commit

Permalink
Collection get methods for all collection entities (#175)
Browse files Browse the repository at this point in the history
Signed-off-by: Alastair Porter <[email protected]>
  • Loading branch information
alastair committed Dec 29, 2015
1 parent cffa983 commit d6d21d4
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 21 deletions.
8 changes: 6 additions & 2 deletions musicbrainzngs/mbxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ def parse_collection(collection):
result = {}
attribs = ["id", "type", "entity-type"]
elements = ["name", "editor"]
# TODO: add event-list:
inner_els = {"release-list": parse_release_list}
inner_els = {"release-list": parse_release_list,
"artist-list": parse_artist_list,
"event-list": parse_event_list,
"place-list": parse_place_list,
"recording-list": parse_recording_list,
"work-list": parse_work_list}
result.update(parse_attributes(attribs, collection))
result.update(parse_elements(elements, inner_els, collection))

Expand Down
51 changes: 47 additions & 4 deletions musicbrainzngs/musicbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,16 +1182,59 @@ def get_collections():
# Missing <release-list count="n"> the count in the reply
return _do_mb_query("collection", '')

def _do_collection_query(collection, collection_type, limit, offset):
params = {}
if limit: params["limit"] = limit
if offset: params["offset"] = offset
return _do_mb_query("collection", "%s/%s" % (collection, collection_type), [], params)

def get_artists_in_collection(collection, limit=None, offset=None):
"""List the artists in a collection.
Returns a dict with a 'collection' key, which again has a 'artist-list'.
See `Browsing`_ for how to use `limit` and `offset`.
"""
return _do_collection_query(collection, "artists", limit, offset)

def get_releases_in_collection(collection, limit=None, offset=None):
"""List the releases in a collection.
Returns a dict with a 'collection' key, which again has a 'release-list'.
See `Browsing`_ for how to use `limit` and `offset`.
"""
params = {}
if limit: params["limit"] = limit
if offset: params["offset"] = offset
return _do_mb_query("collection", "%s/releases" % collection, [], params)
return _do_collection_query(collection, "releases", limit, offset)

def get_events_in_collection(collection, limit=None, offset=None):
"""List the events in a collection.
Returns a dict with a 'collection' key, which again has a 'event-list'.
See `Browsing`_ for how to use `limit` and `offset`.
"""
return _do_collection_query(collection, "events", limit, offset)

def get_places_in_collection(collection, limit=None, offset=None):
"""List the places in a collection.
Returns a dict with a 'collection' key, which again has a 'place-list'.
See `Browsing`_ for how to use `limit` and `offset`.
"""
return _do_collection_query(collection, "places", limit, offset)

def get_recordings_in_collection(collection, limit=None, offset=None):
"""List the recordings in a collection.
Returns a dict with a 'collection' key, which again has a 'recording-list'.
See `Browsing`_ for how to use `limit` and `offset`.
"""
return _do_collection_query(collection, "recordings", limit, offset)

def get_works_in_collection(collection, limit=None, offset=None):
"""List the works in a collection.
Returns a dict with a 'collection' key, which again has a 'work-list'.
See `Browsing`_ for how to use `limit` and `offset`.
"""
return _do_collection_query(collection, "works", limit, offset)


# Submission methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><collection entity-type="event" id="20562e36-c7cc-44fb-96b4-486d51a1174b" type="Event"><name>event collection</name><editor>alastairp</editor><event-list count="1"><event id="fe4bce99-6e9b-4050-9681-eac48cdf3199"><name>T on the Fringe 2006</name><life-span><begin>2006-08-04</begin><end>2006-08-30</end></life-span></event></event-list></collection></metadata>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><collection id="2326c2e8-be4b-4300-acc6-dbd0adf5645b" type="Work" entity-type="work"><name>work collection</name><editor>alastairp</editor><work-list count="1"><work id="541f07e2-389d-4a0c-8a97-896b6752dd35"><title>Maggot Brain</title></work></work-list></collection></metadata>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><collection entity-type="artist" type="Artist" id="29611d8b-b3ad-4ffb-acb5-27f77342a5b0"><name>artist collection</name><editor>alastairp</editor><artist-list count="1"><artist id="0383dadf-2a4e-4d10-a46a-e9e041da8eb3"><name>Queen</name><sort-name>Queen</sort-name><disambiguation>UK rock group</disambiguation><life-span><begin>1970-06-27</begin></life-span></artist></artist-list></collection></metadata>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><collection entity-type="place" type="Place" id="855b134e-9a3b-4717-8df8-8c4838d89924"><name>place collection</name><editor>alastairp</editor><place-list count="1"><place id="f75dd832-8cac-4068-8432-de0472866d33"><name>San Francisco Bath House</name><disambiguation>aka 'San Fran'</disambiguation><address>171 Cuba Street, Wellington, New Zealand</address></place></place-list></collection></metadata>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><collection type="Recording" id="a91320b2-fd2f-4a93-9e4e-603d16d514b6" entity-type="recording"><name>recording collection</name><editor>alastairp</editor><recording-list count="1"><recording id="be462d72-b71b-4cb0-8f65-3434f99822a7"><title>Maggot Brain</title><length>1201000</length></recording></recording-list></collection></metadata>
92 changes: 77 additions & 15 deletions test/test_mbxml_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,98 @@ def setUp(self):
def testGetCollection(self):
musicbrainzngs.get_releases_in_collection("0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac")
self.assertEqual("http://musicbrainz.org/ws/2/collection/0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac/releases", self.opener.get_url())
# TODO: get events_in_collection

musicbrainzngs.get_works_in_collection("898676a6-bc79-4fe2-98ae-79c5940fe1a2")
self.assertEqual("http://musicbrainz.org/ws/2/collection/898676a6-bc79-4fe2-98ae-79c5940fe1a2/works", self.opener.get_url())

musicbrainzngs.get_events_in_collection("65cb5dda-44aa-44a8-9c0d-4f99a14ab944")
self.assertEqual("http://musicbrainz.org/ws/2/collection/65cb5dda-44aa-44a8-9c0d-4f99a14ab944/events", self.opener.get_url())

musicbrainzngs.get_places_in_collection("9dde4c3c-520a-4bfd-9aae-446c3a04ce0c")
self.assertEqual("http://musicbrainz.org/ws/2/collection/9dde4c3c-520a-4bfd-9aae-446c3a04ce0c/places", self.opener.get_url())

musicbrainzngs.get_recordings_in_collection("42bc6dd9-8deb-4bd7-83eb-5dacdb218b38")
self.assertEqual("http://musicbrainz.org/ws/2/collection/42bc6dd9-8deb-4bd7-83eb-5dacdb218b38/recordings", self.opener.get_url())

musicbrainzngs.get_artists_in_collection("7e582256-b3ce-421f-82ba-451b0ab080eb")
self.assertEqual("http://musicbrainz.org/ws/2/collection/7e582256-b3ce-421f-82ba-451b0ab080eb/artists", self.opener.get_url())


class GetCollectionTest(unittest.TestCase):
def setUp(self):
self.datadir = os.path.join(os.path.dirname(__file__), "data", "collection")

def testCollectionType(self):
"""
Test if the type of the collection is parsed correctly.
"""

res = _common.open_and_parse_test_data(self.datadir, "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac-releases.xml")
self.assertEqual(res["collection"]["entity-type"], "release")
self.assertEqual(res["collection"]["type"], "Release")
# TODO: example for Event type

def testCollectionInfo(self):
"""
Test that the id, name and author are given.
"""
res = _common.open_and_parse_test_data(self.datadir, "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac-releases.xml")
self.assertEqual(res["collection"]["id"], "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac")
self.assertEqual(res["collection"]["name"], "My Collection")
self.assertEqual(res["collection"]["editor"], "JonnyJD")

def testCollectionReleases(self):
"""
Test that the list of releases is given.
"""
res = _common.open_and_parse_test_data(self.datadir, "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac-releases.xml")
self.assertEqual(res["collection"]["release-count"], 400)
coll = res["collection"]
self.assertEqual(coll["id"], "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac")
self.assertEqual(coll["name"], "My Collection")
self.assertEqual(coll["editor"], "JonnyJD")
self.assertEqual(coll["entity-type"], "release")
self.assertEqual(coll["type"], "Release")
self.assertEqual(coll["release-count"], 400)
self.assertTrue("release-list" in res["collection"])

def testCollectionWorks(self):
res = _common.open_and_parse_test_data(self.datadir, "2326c2e8-be4b-4300-acc6-dbd0adf5645b-works.xml")
coll = res["collection"]
self.assertEqual(coll["id"], "2326c2e8-be4b-4300-acc6-dbd0adf5645b")
self.assertEqual(coll["name"], "work collection")
self.assertEqual(coll["editor"], "alastairp")
self.assertEqual(coll["entity-type"], "work")
self.assertEqual(coll["type"], "Work")
self.assertEqual(coll["work-count"], 1)
self.assertEqual(len(coll["work-list"]), 1)

def testCollectionArtists(self):
res = _common.open_and_parse_test_data(self.datadir, "29611d8b-b3ad-4ffb-acb5-27f77342a5b0-artists.xml")
coll = res["collection"]
self.assertEqual(coll["id"], "29611d8b-b3ad-4ffb-acb5-27f77342a5b0")
self.assertEqual(coll["name"], "artist collection")
self.assertEqual(coll["editor"], "alastairp")
self.assertEqual(coll["entity-type"], "artist")
self.assertEqual(coll["type"], "Artist")
self.assertEqual(coll["artist-count"], 1)
self.assertEqual(len(coll["artist-list"]), 1)

def testCollectionEvents(self):
res = _common.open_and_parse_test_data(self.datadir, "20562e36-c7cc-44fb-96b4-486d51a1174b-events.xml")
coll = res["collection"]
self.assertEqual(coll["id"], "20562e36-c7cc-44fb-96b4-486d51a1174b")
self.assertEqual(coll["name"], "event collection")
self.assertEqual(coll["editor"], "alastairp")
self.assertEqual(coll["entity-type"], "event")
self.assertEqual(coll["type"], "Event")
self.assertEqual(coll["event-count"], 1)
self.assertEqual(len(coll["event-list"]), 1)

def testCollectionPlaces(self):
res = _common.open_and_parse_test_data(self.datadir, "855b134e-9a3b-4717-8df8-8c4838d89924-places.xml")
coll = res["collection"]
self.assertEqual(coll["id"], "855b134e-9a3b-4717-8df8-8c4838d89924")
self.assertEqual(coll["name"], "place collection")
self.assertEqual(coll["editor"], "alastairp")
self.assertEqual(coll["entity-type"], "place")
self.assertEqual(coll["type"], "Place")
self.assertEqual(coll["place-count"], 1)
self.assertEqual(len(coll["place-list"]), 1)

def testCollectionRecordings(self):
res = _common.open_and_parse_test_data(self.datadir, "a91320b2-fd2f-4a93-9e4e-603d16d514b6-recordings.xml")
coll = res["collection"]
self.assertEqual(coll["id"], "a91320b2-fd2f-4a93-9e4e-603d16d514b6")
self.assertEqual(coll["name"], "recording collection")
self.assertEqual(coll["editor"], "alastairp")
self.assertEqual(coll["entity-type"], "recording")
self.assertEqual(coll["type"], "Recording")
self.assertEqual(coll["recording-count"], 1)
self.assertEqual(len(coll["recording-list"]), 1)

0 comments on commit d6d21d4

Please sign in to comment.