From 989b4719a6108c0d9fea6c7fafde198a2b2a34e3 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 3 Jan 2016 11:07:02 -0800 Subject: [PATCH] Rearrange test_play.py to minimize duplication --- test/test_play.py | 64 +++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/test/test_play.py b/test/test_play.py index 6d0986102..cccf1fbc1 100644 --- a/test/test_play.py +++ b/test/test_play.py @@ -26,17 +26,35 @@ class PlayPluginTest(unittest.TestCase, TestHelper): self.teardown_beets() self.unload_plugins() - def test_basic(self, open_mock): - self.run_command('play', 'title:aNiceTitle') + def do_test(self, open_mock, args=('title:aNiceTitle',), expected_cmd=None, expected_playlist='{}\n'): + self.run_command('play', *args) - open_mock.assert_called_once_with(ANY, None) - self.assertPlaylistCorrect(open_mock) + open_mock.assert_called_once_with(ANY, expected_cmd) + expected_playlist_content = expected_playlist.format(self.item.path.decode('utf-8')) + with open(open_mock.call_args[0][0][0], 'r') as playlist: + self.assertEqual(expected_playlist_content, playlist.read().decode('utf-8')) + + def test_basic(self, open_mock): + self.do_test(open_mock) def test_album_option(self, open_mock): - self.run_command('play', '-a', 'nice') + self.do_test(open_mock, ['-a', 'nice']) - open_mock.assert_called_once_with(ANY, None) - self.assertPlaylistCorrect(open_mock) + def test_args_option(self, open_mock): + self.config['play']['command'] = 'echo' + + self.do_test(open_mock, ['-A', 'foo', 'title:aNiceTitle'], 'echo foo') + + def test_args_option_in_middle(self, open_mock): + self.config['play']['command'] = 'echo $args other' + + self.do_test(open_mock, ['-A', 'foo', 'title:aNiceTitle'], 'echo foo other') + + def test_relative_to(self, open_mock): + self.config['play']['command'] = 'echo' + self.config['play']['relative_to'] = '/something' + + self.do_test(open_mock, expected_cmd='echo', expected_playlist='..{}\n') def test_use_folders(self, open_mock): self.config['play']['use_folders'] = True @@ -48,41 +66,22 @@ class PlayPluginTest(unittest.TestCase, TestHelper): os.path.dirname(self.item.path.decode('utf-8'))), playlist.read().decode('utf-8')) - def test_args_option(self, open_mock): - self.config['play']['command'] = 'echo' - self.run_command('play', '-A', 'foo', 'title:aNiceTitle') - - open_mock.assert_called_once_with(ANY, 'echo foo') - self.assertPlaylistCorrect(open_mock) - - def test_args_option_in_middle(self, open_mock): - self.config['play']['command'] = 'echo $args other' - self.run_command('play', '-A', 'foo', 'title:aNiceTitle') - - open_mock.assert_called_once_with(ANY, 'echo foo other') - self.assertPlaylistCorrect(open_mock) - - def test_relative_to(self, open_mock): - self.config['play']['command'] = 'echo' - self.config['play']['relative_to'] = '/something' - self.run_command('play', 'title:aNiceTitle') - - open_mock.assert_called_once_with(ANY, 'echo') - self.assertPlaylistCorrect(open_mock, '..{}\n') - def test_raw(self, open_mock): self.config['play']['raw'] = True + self.run_command('play', 'nice') open_mock.assert_called_once_with([self.item.path], None) def test_not_found(self, open_mock): self.run_command('play', 'not found') + open_mock.assert_not_called() def test_warning_threshold(self, open_mock): self.config['play']['warning_treshold'] = 1 item2 = self.add_item(title='another NiceTitle') + with control_stdin("a"): self.run_command('play', 'nice') @@ -90,15 +89,10 @@ class PlayPluginTest(unittest.TestCase, TestHelper): def test_command_failed(self, open_mock): open_mock.side_effect = OSError("some reason") + with self.assertRaises(UserError): self.run_command('play', 'title:aNiceTitle') - def assertPlaylistCorrect(self, open_mock, expected='{}\n'): - playlist = open(open_mock.call_args[0][0][0], 'r') - self.assertEqual(expected.format(self.item.path.decode('utf-8')), - playlist.read().decode('utf-8')) - - def suite(): return unittest.TestLoader().loadTestsFromName(__name__)