combine TempConfigTestCase and ExtraAsserts

This commit is contained in:
Adrian Sampson 2012-12-15 13:21:09 -08:00
parent 39cd1f6c63
commit cbd538de7b
7 changed files with 53 additions and 50 deletions

View file

@ -81,10 +81,11 @@ def import_session(lib=None, logfile=None, paths=[], query=[], cli=False):
return cls(lib, logfile, paths, query)
# Temporary config modifications.
class TempConfigTestCase(unittest.TestCase):
"""A TestCase subclass that saves and restores beets' global
configuration. This allows tests to make temporary modifications
that will then be automatically removed when the test completes.
class TestCase(unittest.TestCase):
"""A unittest.TestCase subclass that saves and restores beets'
global configuration. This allows tests to make temporary
modifications that will then be automatically removed when the test
completes. Also provides some additional assertion methods.
"""
def setUp(self):
self.old_sources = copy.deepcopy(beets.config.sources)
@ -94,6 +95,15 @@ class TempConfigTestCase(unittest.TestCase):
beets.config.sources = self.old_sources
beets.config.overlay = self.old_overlay
def assertExists(self, path):
self.assertTrue(os.path.exists(path),
'file does not exist: %s' % path)
def assertNotExists(self, path):
self.assertFalse(os.path.exists(path),
'file exists: %s' % path)
# Mock timing.
@ -185,17 +195,6 @@ class DummyIO(object):
sys.stdout = sys.__stdout__
# Mixin for additional assertions.
class ExtraAsserts(object):
def assertExists(self, path):
self.assertTrue(os.path.exists(path),
'file does not exist: %s' % path)
def assertNotExists(self, path):
self.assertFalse(os.path.exists(path),
'file exists: %s' % path)
# Utility.
def touch(path):

View file

@ -179,7 +179,7 @@ class AAOTest(unittest.TestCase):
res = fetchart.aao_art('x')
self.assertEqual(res, None)
class ArtImporterTest(_common.TempConfigTestCase, _common.ExtraAsserts):
class ArtImporterTest(_common.TestCase):
def setUp(self):
super(ArtImporterTest, self).setUp()

View file

@ -503,7 +503,7 @@ class ApplyTestUtil(object):
config['per_disc_numbering'] = per_disc_numbering
autotag.apply_metadata(info, mapping)
class ApplyTest(_common.TempConfigTestCase, ApplyTestUtil):
class ApplyTest(_common.TestCase, ApplyTestUtil):
def setUp(self):
super(ApplyTest, self).setUp()
@ -617,7 +617,7 @@ class ApplyTest(_common.TempConfigTestCase, ApplyTestUtil):
self.assertEqual(self.items[1].albumartist_sort, 'albumArtistSort')
self.assertEqual(self.items[1].artist_sort, 'albumArtistSort')
class ApplyCompilationTest(_common.TempConfigTestCase, ApplyTestUtil):
class ApplyCompilationTest(_common.TestCase, ApplyTestUtil):
def setUp(self):
super(ApplyCompilationTest, self).setUp()

View file

@ -25,7 +25,7 @@ from _common import item, touch
import beets.library
from beets import util
class MoveTest(unittest.TestCase, _common.ExtraAsserts):
class MoveTest(_common.TestCase):
def setUp(self):
# make a temporary file
self.path = join(_common.RSRC, 'temp.mp3')
@ -125,7 +125,7 @@ class MoveTest(unittest.TestCase, _common.ExtraAsserts):
self.assertEqual(os.path.dirname(self.i.path),
os.path.dirname(dest))
class HelperTest(unittest.TestCase):
class HelperTest(_common.TestCase):
def test_ancestry_works_on_file(self):
p = '/a/b/c'
a = ['/','/a','/a/b']
@ -152,7 +152,7 @@ class HelperTest(unittest.TestCase):
a = ['a', 'b', 'c']
self.assertEqual(util.components(p), a)
class AlbumFileTest(unittest.TestCase):
class AlbumFileTest(_common.TestCase):
def setUp(self):
# Make library and item.
self.lib = beets.library.Library(':memory:')
@ -205,7 +205,7 @@ class AlbumFileTest(unittest.TestCase):
self.lib.load(self.i)
self.assertTrue('testotherdir' in self.i.path)
class ArtFileTest(unittest.TestCase, _common.ExtraAsserts):
class ArtFileTest(_common.TestCase):
def setUp(self):
# Make library and item.
self.lib = beets.library.Library(':memory:')
@ -379,7 +379,7 @@ class ArtFileTest(unittest.TestCase, _common.ExtraAsserts):
self.assertEqual(artpath, oldartpath)
self.assertExists(oldartpath)
class RemoveTest(unittest.TestCase, _common.ExtraAsserts):
class RemoveTest(_common.TestCase):
def setUp(self):
# Make library and item.
self.lib = beets.library.Library(':memory:')
@ -440,7 +440,7 @@ class RemoveTest(unittest.TestCase, _common.ExtraAsserts):
self.assertNotExists(parent)
# Tests that we can "delete" nonexistent files.
class SoftRemoveTest(unittest.TestCase, _common.ExtraAsserts):
class SoftRemoveTest(_common.TestCase):
def setUp(self):
self.path = os.path.join(_common.RSRC, 'testfile')
touch(self.path)
@ -458,7 +458,7 @@ class SoftRemoveTest(unittest.TestCase, _common.ExtraAsserts):
except OSError:
self.fail('OSError when removing path')
class SafeMoveCopyTest(unittest.TestCase, _common.ExtraAsserts):
class SafeMoveCopyTest(_common.TestCase):
def setUp(self):
self.path = os.path.join(_common.RSRC, 'testfile')
touch(self.path)
@ -499,7 +499,7 @@ class SafeMoveCopyTest(unittest.TestCase, _common.ExtraAsserts):
util.copy(self.path, self.path)
self.assertExists(self.path)
class PruneTest(unittest.TestCase, _common.ExtraAsserts):
class PruneTest(_common.TestCase):
def setUp(self):
self.base = os.path.join(_common.RSRC, 'testdir')
os.mkdir(self.base)
@ -519,7 +519,7 @@ class PruneTest(unittest.TestCase, _common.ExtraAsserts):
self.assertExists(self.base)
self.assertNotExists(self.sub)
class WalkTest(unittest.TestCase):
class WalkTest(_common.TestCase):
def setUp(self):
self.base = os.path.join(_common.RSRC, 'testdir')
os.mkdir(self.base)
@ -559,7 +559,7 @@ class WalkTest(unittest.TestCase):
self.assertEqual(res[0],
(self.base, [], []))
class UniquePathTest(unittest.TestCase):
class UniquePathTest(_common.TestCase):
def setUp(self):
self.base = os.path.join(_common.RSRC, 'testdir')
os.mkdir(self.base)

View file

@ -27,7 +27,7 @@ from beets.autotag import AlbumInfo, TrackInfo, AlbumMatch, TrackMatch
from beets import config
TEST_TITLES = ('The Opener', 'The Second Track', 'The Last Track')
class NonAutotaggedImportTest(_common.TempConfigTestCase):
class NonAutotaggedImportTest(_common.TestCase):
def setUp(self):
super(NonAutotaggedImportTest, self).setUp()
@ -183,7 +183,7 @@ def _call_stages(session, items, choice_or_info,
return task
class ImportApplyTest(_common.TempConfigTestCase, _common.ExtraAsserts):
class ImportApplyTest(_common.TestCase):
def setUp(self):
super(ImportApplyTest, self).setUp()
@ -342,7 +342,7 @@ class ImportApplyTest(_common.TempConfigTestCase, _common.ExtraAsserts):
stages=[importer.manipulate_files])
self.assertExists(self.i.path)
class AsIsApplyTest(unittest.TestCase):
class AsIsApplyTest(_common.TestCase):
def setUp(self):
self.dbpath = os.path.join(_common.RSRC, 'templib.blb')
self.lib = library.Library(self.dbpath)
@ -387,7 +387,7 @@ class AsIsApplyTest(unittest.TestCase):
self.assertFalse(alb.comp)
self.assertEqual(alb.albumartist, self.items[2].artist)
class ApplyExistingItemsTest(_common.TempConfigTestCase, _common.ExtraAsserts):
class ApplyExistingItemsTest(_common.TestCase):
def setUp(self):
super(ApplyExistingItemsTest, self).setUp()
@ -567,8 +567,10 @@ class ApplyExistingItemsTest(_common.TempConfigTestCase, _common.ExtraAsserts):
self.assertEqual(len(list(self.lib.items())), 1)
self.assertEqual(len(list(self.lib.albums())), 1)
class InferAlbumDataTest(unittest.TestCase):
class InferAlbumDataTest(_common.TestCase):
def setUp(self):
super(InferAlbumDataTest, self).setUp()
i1 = _common.item()
i2 = _common.item()
i3 = _common.item()
@ -659,8 +661,10 @@ class InferAlbumDataTest(unittest.TestCase):
self.assertFalse(self.items[1].comp)
self.assertEqual(self.items[1].albumartist, self.items[2].artist)
class DuplicateCheckTest(unittest.TestCase):
class DuplicateCheckTest(_common.TestCase):
def setUp(self):
super(DuplicateCheckTest, self).setUp()
self.lib = library.Library(':memory:')
self.i = _common.item()
self.album = self.lib.add_album([self.i])
@ -754,7 +758,7 @@ class DuplicateCheckTest(unittest.TestCase):
self._item_task(False, existing=True))
self.assertFalse(res)
class TagLogTest(unittest.TestCase):
class TagLogTest(_common.TestCase):
def test_tag_log_line(self):
sio = StringIO.StringIO()
session = _common.import_session(logfile=sio)

View file

@ -6,7 +6,7 @@ from beets import config
from beetsplug.the import ThePlugin, PATTERN_A, PATTERN_THE, FORMAT
class ThePluginTest(_common.TempConfigTestCase):
class ThePluginTest(_common.TestCase):
def test_unthe_with_default_patterns(self):
self.assertEqual(ThePlugin().unthe('', PATTERN_THE), '')

View file

@ -33,7 +33,7 @@ from beets.mediafile import MediaFile
from beets import config
from beets.util import confit
class ListTest(unittest.TestCase):
class ListTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -116,7 +116,7 @@ class ListTest(unittest.TestCase):
self.assertTrue(u'the genre' in out)
self.assertTrue(u'the album' not in out)
class RemoveTest(unittest.TestCase):
class RemoveTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -147,7 +147,7 @@ class RemoveTest(unittest.TestCase):
self.assertEqual(len(list(items)), 0)
self.assertFalse(os.path.exists(self.i.path))
class ModifyTest(unittest.TestCase):
class ModifyTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -226,7 +226,7 @@ class ModifyTest(unittest.TestCase):
item.read()
self.assertFalse('newAlbum' in item.path)
class MoveTest(unittest.TestCase, _common.ExtraAsserts):
class MoveTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -297,7 +297,7 @@ class MoveTest(unittest.TestCase, _common.ExtraAsserts):
self.assertExists(self.i.path)
self.assertNotExists(self.itempath)
class UpdateTest(unittest.TestCase, _common.ExtraAsserts):
class UpdateTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -401,7 +401,7 @@ class UpdateTest(unittest.TestCase, _common.ExtraAsserts):
item = self.lib.items().next()
self.assertEqual(item.title, 'full')
class PrintTest(unittest.TestCase):
class PrintTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -441,7 +441,7 @@ class PrintTest(unittest.TestCase):
else:
del os.environ['LC_CTYPE']
class AutotagTest(unittest.TestCase):
class AutotagTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -468,14 +468,14 @@ class AutotagTest(unittest.TestCase):
self.io.addinput('u')
self._no_candidates_test(importer.action.ASIS)
class ImportTest(_common.TempConfigTestCase):
class ImportTest(_common.TestCase):
def test_quiet_timid_disallowed(self):
config['import']['quiet'] = True
config['import']['timid'] = True
self.assertRaises(ui.UserError, commands.import_files, None, [],
None)
class InputTest(unittest.TestCase):
class InputTest(_common.TestCase):
def setUp(self):
self.io = _common.DummyIO()
self.io.install()
@ -489,7 +489,7 @@ class InputTest(unittest.TestCase):
self.assertEqual(artist, u'\xc2me')
self.assertEqual(album, u'\xc2me')
class ConfigTest(_common.TempConfigTestCase):
class ConfigTest(_common.TestCase):
def setUp(self):
super(ConfigTest, self).setUp()
self.io = _common.DummyIO()
@ -568,7 +568,7 @@ class ConfigTest(_common.TempConfigTestCase):
- foo: bar
""", func)
class ShowdiffTest(_common.TempConfigTestCase):
class ShowdiffTest(_common.TestCase):
def setUp(self):
super(ShowdiffTest, self).setUp()
self.io = _common.DummyIO()
@ -624,7 +624,7 @@ class ShowdiffTest(_common.TempConfigTestCase):
self.assertEqual(complete_diff, partial_diff)
AN_ID = "28e32c71-1450-463e-92bf-e0a46446fc11"
class ManualIDTest(unittest.TestCase):
class ManualIDTest(_common.TestCase):
def setUp(self):
_common.log.setLevel(logging.CRITICAL)
self.io = _common.DummyIO()
@ -647,7 +647,7 @@ class ManualIDTest(unittest.TestCase):
out = commands.manual_id(False)
self.assertEqual(out, AN_ID)
class ShowChangeTest(_common.TempConfigTestCase):
class ShowChangeTest(_common.TestCase):
def setUp(self):
super(ShowChangeTest, self).setUp()
self.io = _common.DummyIO()
@ -716,7 +716,7 @@ class ShowChangeTest(_common.TempConfigTestCase):
self.assertTrue(u'caf\xe9.mp3 -> the title' in msg
or u'caf.mp3 ->' in msg)
class PathFormatTest(_common.TempConfigTestCase):
class PathFormatTest(_common.TestCase):
def test_custom_paths_prepend(self):
default_formats = ui.get_path_formats()