Allow CLI tests to use :memory: db

This significantly increases performance
This commit is contained in:
Thomas Scholtes 2014-04-12 16:08:57 +02:00
parent 271a1627a5
commit 65fcb8a28c
6 changed files with 47 additions and 41 deletions

View file

@ -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):

View file

@ -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)

View file

@ -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):

View file

@ -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')

View file

@ -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):

View file

@ -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 = []