From 65fcb8a28c11970255f47dfcf3e262c11e4115ee Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Sat, 12 Apr 2014 16:08:57 +0200 Subject: [PATCH] Allow CLI tests to use :memory: db This significantly increases performance --- beets/ui/__init__.py | 31 ++++++++++++++++--------------- test/helper.py | 15 +++++++++++---- test/test_config_command.py | 25 +++++++++++++------------ test/test_convert.py | 4 ++-- test/test_echonest.py | 4 +--- test/test_replaygain.py | 9 ++++----- 6 files changed, 47 insertions(+), 41 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 95b5cadc5..edfd146b8 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -905,26 +905,27 @@ def _configure(args): return parser._parse_sub(args) -def _raw_main(args): +def _raw_main(args, lib=None): """A helper function for `main` without top-level exception handling. """ subcommand, suboptions, subargs = _configure(args) - # Open library file. - dbpath = config['library'].as_filename() - try: - lib = library.Library( - dbpath, - config['directory'].as_filename(), - get_path_formats(), - get_replacements(), - ) - except sqlite3.OperationalError: - raise UserError(u"database file {0} could not be opened".format( - util.displayable_path(dbpath) - )) - plugins.send("library_opened", lib=lib) + if lib is None: + # Open library file. + dbpath = config['library'].as_filename() + try: + lib = library.Library( + dbpath, + config['directory'].as_filename(), + get_path_formats(), + get_replacements(), + ) + except sqlite3.OperationalError: + raise UserError(u"database file {0} could not be opened".format( + util.displayable_path(dbpath) + )) + plugins.send("library_opened", lib=lib) # Configure the logger. if config['verbose'].get(bool): diff --git a/test/helper.py b/test/helper.py index 7d1e16a95..4850841c0 100644 --- a/test/helper.py +++ b/test/helper.py @@ -56,7 +56,7 @@ class TestHelper(object): """ # TODO automate teardown through hook registration - def setup_beets(self): + def setup_beets(self, disk=False): """Setup pristine global configuration and library for testing. Sets ``beets.config`` so we can safely use any functionality @@ -94,8 +94,11 @@ class TestHelper(object): os.mkdir(self.libdir) self.config['directory'] = self.libdir - self.lib = Library(self.config['library'].as_filename(), - self.libdir) + if disk: + dbpath = self.config['library'].as_filename() + else: + dbpath = ':memory:' + self.lib = Library(dbpath, self.libdir) def teardown_beets(self): del os.environ['BEETSDIR'] @@ -177,4 +180,8 @@ class TestHelper(object): os.remove(path) def run_command(self, *args): - beets.ui._raw_main(list(args)) + if hasattr(self, 'lib'): + lib = self.lib + else: + lib = Library(':memory:') + beets.ui._raw_main(list(args), lib) diff --git a/test/test_config_command.py b/test/test_config_command.py index b180d03dd..72e7bc05f 100644 --- a/test/test_config_command.py +++ b/test/test_config_command.py @@ -3,12 +3,14 @@ import yaml from beets import ui from beets import config +from beets.library import Library import _common from _common import unittest +from helper import TestHelper -class ConfigCommandTest(_common.TestCase): +class ConfigCommandTest(_common.TestCase, TestHelper): def setUp(self): super(ConfigCommandTest, self).setUp() @@ -35,31 +37,31 @@ class ConfigCommandTest(_common.TestCase): self.execlp_restore() def test_show_user_config(self): - ui._raw_main(['config']) + self.run_command('config') output = yaml.load(self.io.getoutput()) self.assertEqual(output['option'], 'value') def test_show_user_config_with_defaults(self): - ui._raw_main(['config', '-d']) + self.run_command('config', '-d') output = yaml.load(self.io.getoutput()) self.assertEqual(output['option'], 'value') self.assertEqual(output['library'], 'lib') self.assertEqual(output['import']['timid'], False) def test_show_user_config_with_cli(self): - ui._raw_main(['--config', self.cli_config_path, 'config']) + self.run_command('--config', self.cli_config_path, 'config') output = yaml.load(self.io.getoutput()) self.assertEqual(output['library'], 'lib') self.assertEqual(output['option'], 'cli overwrite') def test_config_paths(self): - ui._raw_main(['config', '-p']) + self.run_command('config', '-p') paths = self.io.getoutput().split('\n') self.assertEqual(len(paths), 2) self.assertEqual(paths[0], self.config_path) def test_config_paths_with_cli(self): - ui._raw_main(['--config', self.cli_config_path, 'config', '-p']) + self.run_command('--config', self.cli_config_path, 'config', '-p') paths = self.io.getoutput().split('\n') self.assertEqual(len(paths), 3) self.assertEqual(paths[0], self.cli_config_path) @@ -68,14 +70,14 @@ class ConfigCommandTest(_common.TestCase): self.execlp_stub() os.environ['EDITOR'] = 'myeditor' - ui._raw_main(['config', '-e']) + self.run_command('config', '-e') self.assertEqual(self._execlp_call, ['myeditor', self.config_path]) def test_edit_config_with_open(self): self.execlp_stub() with _common.system_mock('Darwin'): - ui._raw_main(['config', '-e']) + self.run_command('config', '-e') self.assertEqual(self._execlp_call, ['open', '-n', self.config_path]) @@ -83,14 +85,14 @@ class ConfigCommandTest(_common.TestCase): self.execlp_stub() with _common.system_mock('Linux'): - ui._raw_main(['config', '-e']) + self.run_command('config', '-e') self.assertEqual(self._execlp_call, ['xdg-open', self.config_path]) def test_edit_config_with_windows_exec(self): self.execlp_stub() with _common.system_mock('Windows'): - ui._raw_main(['config', '-e']) + self.run_command('config', '-e') self.assertEqual(self._execlp_call, [self.config_path]) def test_config_editor_not_found(self): @@ -98,11 +100,10 @@ class ConfigCommandTest(_common.TestCase): raise OSError os.execlp = raise_os_error with self.assertRaises(ui.UserError) as user_error: - ui._raw_main(['config', '-e']) + self.run_command('config', '-e') self.assertIn('Could not edit configuration', str(user_error.exception.args[0])) - def execlp_stub(self): self._execlp_call = None def _execlp_stub(file, *args): diff --git a/test/test_convert.py b/test/test_convert.py index f869727a4..3b4528a9c 100644 --- a/test/test_convert.py +++ b/test/test_convert.py @@ -19,7 +19,7 @@ from helper import TestHelper, controlStdin class ImportConvertTest(unittest.TestCase, TestHelper): def setUp(self): - self.setup_beets() + self.setup_beets(disk=True) # Converter is threaded self.importer = self.create_importer() self.load_plugins('convert') @@ -62,7 +62,7 @@ class ImportConvertTest(unittest.TestCase, TestHelper): class ConvertCliTest(unittest.TestCase, TestHelper): def setUp(self): - self.setup_beets() + self.setup_beets(disk=True) # Converter is threaded self.item, = self.add_item_fixtures(ext='ogg') self.load_plugins('convert') diff --git a/test/test_echonest.py b/test/test_echonest.py index e0f044a71..e77cacefe 100644 --- a/test/test_echonest.py +++ b/test/test_echonest.py @@ -13,14 +13,12 @@ # included in all copies or substantial portions of the Software. -import os.path from mock import Mock, patch -import _common from _common import unittest from helper import TestHelper -from beets.library import Item, Album +from beets.library import Item class EchonestCliTest(unittest.TestCase, TestHelper): diff --git a/test/test_replaygain.py b/test/test_replaygain.py index 02dbe8c63..6996d6525 100644 --- a/test/test_replaygain.py +++ b/test/test_replaygain.py @@ -21,7 +21,6 @@ import _common from _common import unittest from helper import TestHelper -from beets import ui from beets.library import Item, Album from beets.mediafile import MediaFile @@ -77,7 +76,7 @@ class ReplayGainCliTestBase(TestHelper): self.assertIsNone(mediafile.rg_track_peak) self.assertIsNone(mediafile.rg_track_gain) - ui._raw_main(['replaygain']) + self.run_command('replaygain') for item in self.lib.items(): self.assertIsNotNone(item.rg_track_peak) self.assertIsNotNone(item.rg_track_gain) @@ -88,11 +87,11 @@ class ReplayGainCliTestBase(TestHelper): mediafile.rg_track_gain, item.rg_track_gain, places=2) def test_cli_skips_calculated_tracks(self): - ui._raw_main(['replaygain']) + self.run_command('replaygain') item = self.lib.items()[0] peak = item.rg_track_peak item.rg_track_gain = 0.0 - ui._raw_main(['replaygain']) + self.run_command('replaygain') self.assertEqual(item.rg_track_gain, 0.0) self.assertEqual(item.rg_track_peak, peak) @@ -102,7 +101,7 @@ class ReplayGainCliTestBase(TestHelper): self.assertIsNone(mediafile.rg_album_peak) self.assertIsNone(mediafile.rg_album_gain) - ui._raw_main(['replaygain', '-a']) + self.run_command('replaygain', '-a') peaks = [] gains = []