Skip to content

Commit

Permalink
Allow to use flexible attributes in duplicate_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jcassette committed Jan 22, 2022
1 parent 3fdfaaa commit 6ce29a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion beets/importer.py
Expand Up @@ -691,7 +691,12 @@ def find_duplicates(self, lib):
keys = config['import']['duplicate_keys'].as_str().split()
info = self.chosen_info().copy()
info['albumartist'] = artist
subqueries = [ dbcore.MatchQuery(k, info.get(k)) for k in keys ]
album = library.Album(None, **info)
subqueries = []
for key in keys:
value = album.get(key)
fast = key in library.Album.item_keys
subqueries.append(dbcore.MatchQuery(key, value, fast))
duplicate_query = dbcore.AndQuery(subqueries)

for album in lib.albums(duplicate_query):
Expand Down
20 changes: 20 additions & 0 deletions test/test_importer.py
Expand Up @@ -1224,6 +1224,7 @@ def test_album_info(*args, **kwargs):
tracks=[track_info],
album_id='albumid',
artist_id='artistid',
flex='flex',
)
return iter([album_info])

Expand All @@ -1241,6 +1242,7 @@ def setUp(self):
# Create import session
self.importer = self.create_importer()
config['import']['autotag'] = True
config['import']['duplicate_keys'] = 'albumartist album'

def tearDown(self):
self.teardown_beets()
Expand Down Expand Up @@ -1309,6 +1311,24 @@ def test_merge_duplicate_album(self):

def test_twice_in_import_dir(self):
self.skipTest('write me')

def test_keep_when_extra_key_is_different(self):
config['import']['duplicate_keys'] = 'albumartist album flex'

item = self.lib.items().get()
import_file = MediaFile(os.path.join(
self.importer.paths[0], b'album 0', b'track 0.mp3'))
import_file.artist = item['artist']
import_file.albumartist = item['artist']
import_file.album = item['album']
import_file.title = item['title']
import_file.flex = 'different'

self.importer.default_resolution = self.importer.Resolution.SKIP
self.importer.run()

self.assertEqual(len(self.lib.albums()), 2)
self.assertEqual(len(self.lib.items()), 2)

def add_album_fixture(self, **kwargs):
# TODO move this into upstream
Expand Down

0 comments on commit 6ce29a6

Please sign in to comment.