diff --git a/beetsplug/parentwork.py b/beetsplug/parentwork.py index b3e464e60..a5e042344 100644 --- a/beetsplug/parentwork.py +++ b/beetsplug/parentwork.py @@ -96,7 +96,7 @@ class ParentWorkPlugin(BeetsPlugin): item.try_write() command = ui.Subcommand( 'parentwork', - help=u'fetche parent works, composers and dates') + help=u'fetch parent works, composers and dates') command.parser.add_option( u'-f', u'--force', dest='force', @@ -129,6 +129,7 @@ class ParentWorkPlugin(BeetsPlugin): if 'artist-relation-list' in work_info['work']: for artist in work_info['work']['artist-relation-list']: if artist['type'] == 'composer': + composer_exists = True parent_composer.append(artist['artist']['name']) parent_composer_sort.append(artist['artist']['sort-name']) if 'end' in artist.keys(): diff --git a/test/test_parentwork.py b/test/test_parentwork.py index df6a98d79..b124785bb 100644 --- a/test/test_parentwork.py +++ b/test/test_parentwork.py @@ -20,12 +20,52 @@ from __future__ import division, absolute_import, print_function import os import unittest from test.helper import TestHelper +from mock import patch from beets.library import Item from beetsplug import parentwork -class ParentWorkTest(unittest.TestCase, TestHelper): +work = {'work': {'id': '1', + 'title': 'work', + 'work-relation-list': [{'type': 'parts', + 'direction': 'backward', + 'work': {'id': '2'}}], + 'artist-relation-list': [{'type': 'composer', + 'artist': {'name': + 'random composer', + 'sort-name': + 'composer, random'}}]}} +dp_work = {'work': {'id': '2', + 'title': 'directparentwork', + 'work-relation-list': [{'type': 'parts', + 'direction': 'backward', + 'work': {'id': '3'}}], + 'artist-relation-list': [{'type': 'composer', + 'artist': {'name': + 'random composer', + 'sort-name': + 'composer, random' + }}]}} +p_work = {'work': {'id': '3', + 'title': 'parentwork', + 'artist-relation-list': [{'type': 'composer', + 'artist': {'name': + 'random composer', + 'sort-name': + 'composer, random'}}]}} + + +def mock_workid_response(mbid, includes): + if mbid == '1': + return work + elif mbid == '2': + return dp_work + elif mbid == '3': + return p_work + + +class ParentWorkIntegrationTest(unittest.TestCase, TestHelper): def setUp(self): """Set up configuration""" self.setup_beets() @@ -35,12 +75,15 @@ class ParentWorkTest(unittest.TestCase, TestHelper): self.unload_plugins() self.teardown_beets() + # test how it works with real musicbrainz data @unittest.skipUnless( os.environ.get('INTEGRATION_TEST', '0') == '1', 'integration testing not enabled') - def test_normal_case(self): + def test_normal_case_real(self): item = Item(path='/file', - mb_workid=u'e27bda6e-531e-36d3-9cd7-b8ebc18e8c53') + mb_workid=u'e27bda6e-531e-36d3-9cd7-b8ebc18e8c53', + parentwork_workid_current=u'e27bda6e-531e-36d3-9cd7-\ + b8ebc18e8c53') item.add(self.lib) self.run_command('parentwork') @@ -52,11 +95,13 @@ class ParentWorkTest(unittest.TestCase, TestHelper): @unittest.skipUnless( os.environ.get('INTEGRATION_TEST', '0') == '1', 'integration testing not enabled') - def test_force(self): + def test_force_real(self): self.config['parentwork']['force'] = True item = Item(path='/file', mb_workid=u'e27bda6e-531e-36d3-9cd7-b8ebc18e8c53', - mb_parentworkid=u'XXX') + mb_parentworkid=u'XXX', + parentwork_workid_current=u'e27bda6e-531e-36d3-9cd7-\ + b8ebc18e8c53', parentwork='whatever') item.add(self.lib) self.run_command('parentwork') @@ -68,10 +113,12 @@ class ParentWorkTest(unittest.TestCase, TestHelper): @unittest.skipUnless( os.environ.get('INTEGRATION_TEST', '0') == '1', 'integration testing not enabled') - def test_no_force(self): - self.config['parentwork']['force'] = True + def test_no_force_real(self): + self.config['parentwork']['force'] = False item = Item(path='/file', mb_workid=u'e27bda6e-531e-36d3-9cd7-\ - b8ebc18e8c53', mb_parentworkid=u'XXX') + b8ebc18e8c53', mb_parentworkid=u'XXX', + parentwork_workid_current=u'e27bda6e-531e-36d3-9cd7-\ + b8ebc18e8c53', parentwork='whatever') item.add(self.lib) self.run_command('parentwork') @@ -85,7 +132,7 @@ class ParentWorkTest(unittest.TestCase, TestHelper): @unittest.skipUnless( os.environ.get('INTEGRATION_TEST', '0') == '1', 'integration testing not enabled') - def test_direct_parent_work(self): + def test_direct_parent_work_real(self): mb_workid = u'2e4a3668-458d-3b2a-8be2-0b08e0d8243a' self.assertEqual(u'f04b42df-7251-4d86-a5ee-67cfa49580d1', parentwork.direct_parent_id(mb_workid)[0]) @@ -93,6 +140,56 @@ class ParentWorkTest(unittest.TestCase, TestHelper): parentwork.work_parent_id(mb_workid)[0]) +class ParentWorkTest(unittest.TestCase, TestHelper): + def setUp(self): + """Set up configuration""" + self.setup_beets() + self.load_plugins('parentwork') + self.patcher = patch('musicbrainzngs.get_work_by_id', + side_effect=mock_workid_response) + self.patcher.start() + + def tearDown(self): + self.unload_plugins() + self.teardown_beets() + self.patcher.stop() + + def test_normal_case(self): + item = Item(path='/file', mb_workid='1', parentwork_workid_current='1') + item.add(self.lib) + + self.run_command('parentwork') + + item.load() + self.assertEqual(item['mb_parentworkid'], '3') + + def test_force(self): + self.config['parentwork']['force'] = True + item = Item(path='/file', mb_workid='1', mb_parentworkid=u'XXX', + parentwork_workid_current='1', parentwork='parentwork') + item.add(self.lib) + + self.run_command('parentwork') + + item.load() + self.assertEqual(item['mb_parentworkid'], '3') + + def test_no_force(self): + self.config['parentwork']['force'] = False + item = Item(path='/file', mb_workid='1', mb_parentworkid=u'XXX', + parentwork_workid_current='1', parentwork='parentwork') + item.add(self.lib) + + self.run_command('parentwork') + + item.load() + self.assertEqual(item['mb_parentworkid'], u'XXX') + + def test_direct_parent_work(self): + self.assertEqual('2', parentwork.direct_parent_id('1')[0]) + self.assertEqual('3', parentwork.work_parent_id('1')[0]) + + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)