Skip to content

Commit 4b012e5

Browse files
author
Thomas Scholtes
committed
Import tests use TestHelper
Removes duplicate code, increases speed and makes some files clake8 clean.
1 parent 4e227d6 commit 4b012e5

File tree

4 files changed

+216
-167
lines changed

4 files changed

+216
-167
lines changed

setup.cfg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ verbosity=1
33
logging-clear-handlers=1
44

55
[flake8]
6-
# E231 missing whitespace after ',' (used to align visually)
7-
ignore=E241
6+
# E241 missing whitespace after ',' (used to align visually)
7+
# E221 multiple spaces before operator (used to align visually)
8+
ignore=E241,E221
89

910
# List of files that have not been cleand up yet. We will try to reduce
1011
# this with each commit
11-
exclude=test/*
12+
exclude=test/test_config_command.py,test/test_files.py,test/test_art.py,test/test_dbcore.py,test/test_web.py,test/test_zero.py,test/rsrc/beetsplug/test.py,test/test_template.py,test/test_importfeeds.py,test/test_echonest.py,test/test_datequery.py,test/test_mb.py,test/test_convert.py,test/testall.py,test/test_player.py,test/test_query.py,test/test_mediafile.py,test/test_keyfinder.py,test/test_the.py,test/test_library.py,test/test_lyrics.py,test/test_ihate.py,test/test_vfs.py,test/test_replaygain.py,test/__init__.py,test/_common.py,test/test_mediafile_edge.py,test/test_ui.py,test/helper.py,test/test_autotag.py,test/test_pipeline.py

test/helper.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
from beets import config
2626
import beets.plugins
2727
from beets.library import Library, Item
28+
from beets import importer
2829

29-
# TODO Move this here, along with AutotagMock
30-
from test_importer import TestImportSession
30+
# TODO Move AutotagMock here
3131
import _common
3232

3333

@@ -120,6 +120,7 @@ def setup_beets(self, disk=False):
120120
self.lib = Library(dbpath, self.libdir)
121121

122122
def teardown_beets(self):
123+
self.lib._connection().close()
123124
del os.environ['BEETSDIR']
124125
# FIXME somehow close all open fd to the ilbrary
125126
self.remove_temp_dir()
@@ -219,3 +220,46 @@ def remove_temp_dir(self):
219220
"""Delete the temporary directory created by `create_temp_dir`.
220221
"""
221222
shutil.rmtree(self.temp_dir)
223+
224+
225+
class TestImportSession(importer.ImportSession):
226+
"""ImportSession that can be controlled programaticaly.
227+
228+
>>> lib = Library(':memory:')
229+
>>> importer = TestImportSession(lib, paths=['/path/to/import'])
230+
>>> importer.add_choice(importer.action.SKIP)
231+
>>> importer.add_choice(importer.action.ASIS)
232+
>>> importer.default_choice = importer.action.APPLY
233+
>>> importer.run()
234+
235+
This imports ``/path/to/import`` into `lib`. It skips the first
236+
album and imports thesecond one with metadata from the tags. For the
237+
remaining albums, the metadata from the autotagger will be applied.
238+
"""
239+
240+
def __init__(self, *args, **kwargs):
241+
super(TestImportSession, self).__init__(*args, **kwargs)
242+
self._choices = []
243+
244+
default_choice = importer.action.APPLY
245+
246+
def add_choice(self, choice):
247+
self._choices.append(choice)
248+
249+
def clear_choices(self):
250+
self._choices = []
251+
252+
def choose_match(self, task):
253+
try:
254+
choice = self._choices.pop(0)
255+
except IndexError:
256+
choice = self.default_choice
257+
258+
if choice == importer.action.APPLY:
259+
return task.candidates[0]
260+
elif isinstance(choice, int):
261+
return task.candidates[choice-1]
262+
else:
263+
return choice
264+
265+
choose_item = choose_match

0 commit comments

Comments
 (0)