From ed8002bed5202ce29638ae3b4d2b2d853992cb6d Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 12 Apr 2014 15:00:17 -0700 Subject: [PATCH] tests: close databases to avoid errors on Windows Fixes #655 and makes progress on #670. --- test/_common.py | 8 +++++++- test/test_art.py | 1 + test/test_dbcore.py | 13 +++++++++++++ test/test_importer.py | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/test/_common.py b/test/_common.py index a912703ed..b9e6dde4a 100644 --- a/test/_common.py +++ b/test/_common.py @@ -122,7 +122,10 @@ class TestCase(unittest.TestCase): def tearDown(self): if os.path.isdir(self.temp_dir): shutil.rmtree(self.temp_dir) - os.environ['HOME'] = self._old_home + if self._old_home is None: + del os.environ['HOME'] + else: + os.environ['HOME'] = self._old_home self.io.restore() def assertExists(self, path): @@ -142,6 +145,9 @@ class LibTestCase(TestCase): self.lib = beets.library.Library(':memory:') self.i = item(self.lib) + def tearDown(self): + self.lib._connection().close() + super(LibTestCase, self).tearDown() diff --git a/test/test_art.py b/test/test_art.py index 1f32c29df..2a4293a8f 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -233,6 +233,7 @@ class ArtImporterTest(_common.TestCase): self.task.set_choice(AlbumMatch(0, info, {}, set(), set())) def tearDown(self): + self.lib._connection().close() super(ArtImporterTest, self).tearDown() fetchart.art_for_album = self.old_afa diff --git a/test/test_dbcore.py b/test/test_dbcore.py index 2f4af7472..bfcd1581a 100644 --- a/test/test_dbcore.py +++ b/test/test_dbcore.py @@ -40,10 +40,12 @@ class TestModel1(dbcore.Model): def _template_funcs(self): return {} + class TestDatabase1(dbcore.Database): _models = (TestModel1,) pass + class TestModel2(TestModel1): _fields = { 'id': dbcore.types.Id(), @@ -51,10 +53,12 @@ class TestModel2(TestModel1): 'field_two': dbcore.types.Integer(), } + class TestDatabase2(dbcore.Database): _models = (TestModel2,) pass + class TestModel3(TestModel1): _fields = { 'id': dbcore.types.Id(), @@ -63,10 +67,12 @@ class TestModel3(TestModel1): 'field_three': dbcore.types.Integer(), } + class TestDatabase3(dbcore.Database): _models = (TestModel3,) pass + class TestModel4(TestModel1): _fields = { 'id': dbcore.types.Id(), @@ -76,10 +82,12 @@ class TestModel4(TestModel1): 'field_four': dbcore.types.Integer(), } + class TestDatabase4(dbcore.Database): _models = (TestModel4,) pass + class AnotherTestModel(TestModel1): _table = 'another' _flex_table = 'anotherflex' @@ -88,6 +96,7 @@ class AnotherTestModel(TestModel1): 'foo': dbcore.types.Integer(), } + class TestDatabaseTwoModels(dbcore.Database): _models = (TestModel2, AnotherTestModel) pass @@ -153,6 +162,10 @@ class ModelTest(_common.TestCase): dbfile = os.path.join(self.temp_dir, 'temp.db') self.db = TestDatabase1(dbfile) + def tearDown(self): + self.db._connection().close() + super(ModelTest, self).tearDown() + def test_add_model(self): model = TestModel1() model.add(self.db) diff --git a/test/test_importer.py b/test/test_importer.py index 06992c7d0..df57d12f6 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -124,6 +124,9 @@ class ImportHelper(object): ('comp:true', os.path.join('compilations','$album', '$title')), ] + def _close_library(self): + self.lib._connection().close() + def _create_import_dir(self, count=3): """Creates a directory with media files to import. Sets ``self.import_dir`` to the path of the directory. Also sets @@ -240,6 +243,10 @@ class NonAutotaggedImportTest(_common.TestCase, ImportHelper): self._create_import_dir(2) self._setup_import_session(autotag=False) + def tearDown(self): + self._close_library() + super(NonAutotaggedImportTest, self).tearDown() + def test_album_created_with_track_artist(self): self.importer.run() albums = self.lib.albums() @@ -337,6 +344,7 @@ class ImportSingletonTest(_common.TestCase, ImportHelper): self.matcher = AutotagStub().install() def tearDown(self): + self._close_library() super(ImportSingletonTest, self).tearDown() self.matcher.restore() @@ -424,6 +432,7 @@ class ImportTest(_common.TestCase, ImportHelper): self.matcher.macthin = AutotagStub.GOOD def tearDown(self): + self._close_library() super(ImportTest, self).tearDown() self.matcher.restore() @@ -507,6 +516,7 @@ class ImportTracksTest(_common.TestCase, ImportHelper): self.matcher = AutotagStub().install() def tearDown(self): + self._close_library() super(ImportTracksTest, self).tearDown() self.matcher.restore() @@ -541,6 +551,7 @@ class ImportCompilationTest(_common.TestCase, ImportHelper): self.matcher = AutotagStub().install() def tearDown(self): + self._close_library() super(ImportCompilationTest, self).tearDown() self.matcher.restore() @@ -620,6 +631,7 @@ class ImportExistingTest(_common.TestCase, ImportHelper): self._setup_import_session(import_dir=self.libdir) def tearDown(self): + self._close_library() super(ImportExistingTest, self).tearDown() self.matcher.restore() @@ -732,6 +744,7 @@ class GroupAlbumsImportTest(_common.TestCase, ImportHelper): self.importer.add_choice(importer.action.ASIS) def tearDown(self): + self._close_library() super(GroupAlbumsImportTest, self).tearDown() self.matcher.restore() @@ -799,6 +812,7 @@ class ChooseCandidateTest(_common.TestCase, ImportHelper): self.matcher.matching = AutotagStub.BAD def tearDown(self): + self._close_library() super(ChooseCandidateTest, self).tearDown() self.matcher.restore()