From dca27572aa1dd22462e07edb86d1069d311b3562 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Fri, 22 Jul 2016 23:10:35 -0700 Subject: [PATCH] Convert mock.patch.start() into annotation in test_edit --- test/test_edit.py | 50 ++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/test/test_edit.py b/test/test_edit.py index 5a46ece90..d45bc0a13 100644 --- a/test/test_edit.py +++ b/test/test_edit.py @@ -106,6 +106,7 @@ class EditMixin(object): @_common.slow_test() +@patch('beets.library.Item.write') class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): """Black box tests for `beetsplug.edit`. Command line interaction is simulated using `test.helper.control_stdin()`, and yaml editing via an @@ -123,26 +124,21 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): self.items_orig = [{f: item[f] for f in item._fields} for item in self.album.items()] - # Keep track of write()s. - self.write_patcher = patch('beets.library.Item.write') - self.mock_write = self.write_patcher.start() - def tearDown(self): EditPlugin.listeners = None - self.write_patcher.stop() self.teardown_beets() self.unload_plugins() - def assertCounts(self, album_count=ALBUM_COUNT, track_count=TRACK_COUNT, # noqa + def assertCounts(self, mock_write, album_count=ALBUM_COUNT, track_count=TRACK_COUNT, # noqa write_call_count=TRACK_COUNT, title_starts_with=''): """Several common assertions on Album, Track and call counts.""" self.assertEqual(len(self.lib.albums()), album_count) self.assertEqual(len(self.lib.items()), track_count) - self.assertEqual(self.mock_write.call_count, write_call_count) + self.assertEqual(mock_write.call_count, write_call_count) self.assertTrue(all(i.title.startswith(title_starts_with) for i in self.lib.items())) - def test_title_edit_discard(self): + def test_title_edit_discard(self, m): """Edit title for all items in the library, then discard changes.""" # Edit track titles. self.run_mocked_command({'replacements': {u't\u00eftle': @@ -150,11 +146,11 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): # Cancel. ['c']) - self.assertCounts(write_call_count=0, + self.assertCounts(m, write_call_count=0, title_starts_with=u't\u00eftle') self.assertItemFieldsModified(self.album.items(), self.items_orig, []) - def test_title_edit_apply(self): + def test_title_edit_apply(self, m): """Edit title for all items in the library, then apply changes.""" # Edit track titles. self.run_mocked_command({'replacements': {u't\u00eftle': @@ -162,12 +158,12 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): # Apply changes. ['a']) - self.assertCounts(write_call_count=self.TRACK_COUNT, + self.assertCounts(m, write_call_count=self.TRACK_COUNT, title_starts_with=u'modified t\u00eftle') self.assertItemFieldsModified(self.album.items(), self.items_orig, ['title']) - def test_single_title_edit_apply(self): + def test_single_title_edit_apply(self, m): """Edit title for one item in the library, then apply changes.""" # Edit one track title. self.run_mocked_command({'replacements': {u't\u00eftle 9': @@ -175,25 +171,25 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): # Apply changes. ['a']) - self.assertCounts(write_call_count=1,) + self.assertCounts(m, write_call_count=1,) # No changes except on last item. self.assertItemFieldsModified(list(self.album.items())[:-1], self.items_orig[:-1], []) self.assertEqual(list(self.album.items())[-1].title, u'modified t\u00eftle 9') - def test_noedit(self): + def test_noedit(self, m): """Do not edit anything.""" # Do not edit anything. self.run_mocked_command({'contents': None}, # No stdin. []) - self.assertCounts(write_call_count=0, + self.assertCounts(m, write_call_count=0, title_starts_with=u't\u00eftle') self.assertItemFieldsModified(self.album.items(), self.items_orig, []) - def test_album_edit_apply(self): + def test_album_edit_apply(self, m): """Edit the album field for all items in the library, apply changes. By design, the album should not be updated."" """ @@ -203,14 +199,14 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): # Apply changes. ['a']) - self.assertCounts(write_call_count=self.TRACK_COUNT) + self.assertCounts(m, write_call_count=self.TRACK_COUNT) self.assertItemFieldsModified(self.album.items(), self.items_orig, ['album']) # Ensure album is *not* modified. self.album.load() self.assertEqual(self.album.album, u'\u00e4lbum') - def test_single_edit_add_field(self): + def test_single_edit_add_field(self, m): """Edit the yaml file appending an extra field to the first item, then apply changes.""" # Append "foo: bar" to item with id == 1. @@ -220,10 +216,10 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): ['a']) self.assertEqual(self.lib.items(u'id:1')[0].foo, 'bar') - self.assertCounts(write_call_count=1, + self.assertCounts(m, write_call_count=1, title_starts_with=u't\u00eftle') - def test_a_album_edit_apply(self): + def test_a_album_edit_apply(self, m): """Album query (-a), edit album field, apply changes.""" self.run_mocked_command({'replacements': {u'\u00e4lbum': u'modified \u00e4lbum'}}, @@ -232,12 +228,12 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): args=['-a']) self.album.load() - self.assertCounts(write_call_count=self.TRACK_COUNT) + self.assertCounts(m, write_call_count=self.TRACK_COUNT) self.assertEqual(self.album.album, u'modified \u00e4lbum') self.assertItemFieldsModified(self.album.items(), self.items_orig, ['album']) - def test_a_albumartist_edit_apply(self): + def test_a_albumartist_edit_apply(self, m): """Album query (-a), edit albumartist field, apply changes.""" self.run_mocked_command({'replacements': {u'album artist': u'modified album artist'}}, @@ -246,12 +242,12 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): args=['-a']) self.album.load() - self.assertCounts(write_call_count=self.TRACK_COUNT) + self.assertCounts(m, write_call_count=self.TRACK_COUNT) self.assertEqual(self.album.albumartist, u'the modified album artist') self.assertItemFieldsModified(self.album.items(), self.items_orig, ['albumartist']) - def test_malformed_yaml(self): + def test_malformed_yaml(self, m): """Edit the yaml file incorrectly (resulting in a malformed yaml document).""" # Edit the yaml file to an invalid file. @@ -259,10 +255,10 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): # Edit again to fix? No. ['n']) - self.assertCounts(write_call_count=0, + self.assertCounts(m, write_call_count=0, title_starts_with=u't\u00eftle') - def test_invalid_yaml(self): + def test_invalid_yaml(self, m): """Edit the yaml file incorrectly (resulting in a well-formed but invalid yaml document).""" # Edit the yaml file to an invalid but parseable file. @@ -270,7 +266,7 @@ class EditCommandTest(unittest.TestCase, TestHelper, EditMixin): # No stdin. []) - self.assertCounts(write_call_count=0, + self.assertCounts(m, write_call_count=0, title_starts_with=u't\u00eftle')