refactor duplicate tests to take the whole task as an argument

This commit is contained in:
Adrian Sampson 2011-08-04 11:51:17 -07:00
parent 7f4f477c32
commit 58fb4392ee
2 changed files with 98 additions and 40 deletions

View file

@ -80,12 +80,21 @@ def _reopen_lib(lib):
else:
return lib
def _duplicate_check(lib, artist, album, recent=None):
def _duplicate_check(lib, task, recent=None):
"""Check whether an album already exists in the library. `recent`
should be a set of (artist, album) pairs that will be built up
with every call to this function and checked along with the
library.
"""
if task.choice_flag is action.ASIS:
artist = task.cur_artist
album = task.cur_album
elif task.choice_flag is action.APPLY:
artist = task.info['artist']
album = task.info['album']
else:
return False
if artist is None:
# As-is import with no artist. Skip check.
return False
@ -103,8 +112,17 @@ def _duplicate_check(lib, artist, album, recent=None):
return False
def _item_duplicate_check(lib, artist, title, recent=None):
def _item_duplicate_check(lib, task, recent=None):
"""Check whether an item already exists in the library."""
if task.choice_flag is action.ASIS:
artist = task.item.artist
title = task.item.title
elif task.choice_flag is action.APPLY:
artist = task.info['artist']
title = task.info['title']
else:
return False
# Try recent items.
if recent is not None:
if (artist, title) in recent:
@ -451,17 +469,10 @@ def user_query(config):
task = pipeline.multiple(item_tasks)
# Check for duplicates if we have a match (or ASIS).
if choice is action.ASIS or isinstance(choice, tuple):
if choice is action.ASIS:
artist = task.cur_artist
album = task.cur_album
else:
artist = task.info['artist']
album = task.info['album']
if _duplicate_check(lib, artist, album, recent):
tag_log(config.logfile, 'duplicate', task.path)
log.warn("This album is already in the library!")
task.set_choice(action.SKIP)
if _duplicate_check(lib, task, recent):
tag_log(config.logfile, 'duplicate', task.path)
log.warn("This album is already in the library!")
task.set_choice(action.SKIP)
def show_progress(config):
"""This stage replaces the initial_lookup and user_query stages
@ -628,17 +639,10 @@ def item_query(config):
log_choice(config, task)
# Duplicate check.
if task.choice_flag in (action.ASIS, action.APPLY):
if choice is action.ASIS:
artist = task.item.artist
title = task.item.title
else:
artist = task.info['artist']
title = task.info['title']
if _item_duplicate_check(lib, artist, title, recent):
tag_log(config.logfile, 'duplicate', task.item.path)
log.warn("This item is already in the library!")
task.set_choice(action.SKIP)
if _item_duplicate_check(lib, task, recent):
tag_log(config.logfile, 'duplicate', task.item.path)
log.warn("This item is already in the library!")
task.set_choice(action.SKIP)
def item_progress(config):
"""Skips the lookup and query stages in a non-autotagged singleton

View file

@ -434,40 +434,94 @@ class DuplicateCheckTest(unittest.TestCase):
self.i = _common.item()
self.album = self.lib.add_album([self.i])
def test_duplicate_album(self):
res = importer._duplicate_check(self.lib, self.i.albumartist,
self.i.album)
def _album_task(self, asis, artist=None, album=None):
artist = artist or self.i.albumartist
album = album or self.i.album
task = importer.ImportTask(path='a path', toppath='top path',
items=[self.i])
task.set_match(artist, album, None, None)
if asis:
task.set_choice(importer.action.ASIS)
else:
task.set_choice(({'artist': artist, 'album': album}, [self.i]))
return task
def _item_task(self, asis, artist=None, title=None):
artist = artist or self.i.artist
title = title or self.i.title
task = importer.ImportTask.item_task(self.i)
if asis:
self.i.artist = artist
self.i.title = title
task.set_choice(importer.action.ASIS)
else:
task.set_choice({'artist': artist, 'title': title})
return task
def test_duplicate_album_apply(self):
res = importer._duplicate_check(self.lib, self._album_task(False))
self.assertTrue(res)
def test_different_album(self):
res = importer._duplicate_check(self.lib, 'xxx', 'yyy')
def test_different_album_apply(self):
res = importer._duplicate_check(self.lib,
self._album_task(False, 'xxx', 'yyy'))
self.assertFalse(res)
def test_duplicate_album_asis(self):
res = importer._duplicate_check(self.lib, self._album_task(True))
self.assertTrue(res)
def test_different_album_asis(self):
res = importer._duplicate_check(self.lib,
self._album_task(True, 'xxx', 'yyy'))
self.assertFalse(res)
def test_duplicate_va_album(self):
self.album.albumartist = 'an album artist'
res = importer._duplicate_check(self.lib, 'an album artist',
self.i.album)
res = importer._duplicate_check(self.lib,
self._album_task(False, 'an album artist'))
self.assertTrue(res)
def test_duplicate_item(self):
res = importer._item_duplicate_check(self.lib, self.i.artist,
self.i.title)
def test_duplicate_item_apply(self):
res = importer._item_duplicate_check(self.lib,
self._item_task(False))
self.assertTrue(res)
def test_different_item(self):
res = importer._item_duplicate_check(self.lib, 'xxx', 'yyy')
def test_different_item_apply(self):
res = importer._item_duplicate_check(self.lib,
self._item_task(False, 'xxx', 'yyy'))
self.assertFalse(res)
def test_duplicate_item_asis(self):
res = importer._item_duplicate_check(self.lib,
self._item_task(True))
self.assertTrue(res)
def test_different_item_asis(self):
res = importer._item_duplicate_check(self.lib,
self._item_task(True, 'xxx', 'yyy'))
self.assertFalse(res)
def test_recent_item(self):
recent = set()
importer._item_duplicate_check(self.lib, 'xxx', 'yyy', recent)
res = importer._item_duplicate_check(self.lib, 'xxx', 'yyy', recent)
importer._item_duplicate_check(self.lib,
self._item_task(False, 'xxx', 'yyy'),
recent)
res = importer._item_duplicate_check(self.lib,
self._item_task(False, 'xxx', 'yyy'),
recent)
self.assertTrue(res)
def test_recent_album(self):
recent = set()
importer._duplicate_check(self.lib, 'xxx', 'yyy', recent)
res = importer._duplicate_check(self.lib, 'xxx', 'yyy', recent)
importer._duplicate_check(self.lib,
self._album_task(False, 'xxx', 'yyy'),
recent)
res = importer._duplicate_check(self.lib,
self._album_task(False, 'xxx', 'yyy'),
recent)
self.assertTrue(res)
def suite():