mirror of
https://github.com/beetbox/beets.git
synced 2026-01-30 20:13:37 +01:00
matching: add additional test cases and refactor tests
This commit is contained in:
parent
0d6393e712
commit
084cf6490e
1 changed files with 34 additions and 85 deletions
|
|
@ -498,85 +498,41 @@ class AlbumDistanceTest(BeetsTestCase):
|
|||
assert dist == 0
|
||||
|
||||
|
||||
class AssignmentTest(unittest.TestCase):
|
||||
def item(self, title, track):
|
||||
return Item(
|
||||
title=title,
|
||||
track=track,
|
||||
mb_trackid="",
|
||||
mb_albumid="",
|
||||
mb_artistid="",
|
||||
)
|
||||
class TestAssignment:
|
||||
A = "one"
|
||||
B = "two"
|
||||
C = "three"
|
||||
|
||||
def test_reorder_when_track_numbers_incorrect(self):
|
||||
items = []
|
||||
items.append(self.item("one", 1))
|
||||
items.append(self.item("three", 2))
|
||||
items.append(self.item("two", 3))
|
||||
trackinfo = []
|
||||
trackinfo.append(TrackInfo(title="one"))
|
||||
trackinfo.append(TrackInfo(title="two"))
|
||||
trackinfo.append(TrackInfo(title="three"))
|
||||
mapping, extra_items, extra_tracks = match.assign_items(
|
||||
items, trackinfo
|
||||
)
|
||||
assert extra_items == []
|
||||
assert extra_tracks == []
|
||||
assert mapping == {
|
||||
items[0]: trackinfo[0],
|
||||
items[1]: trackinfo[2],
|
||||
items[2]: trackinfo[1],
|
||||
}
|
||||
@pytest.mark.parametrize(
|
||||
# 'expected' is a tuple of expected (mapping, extra_items, extra_tracks)
|
||||
"item_titles, track_titles, expected",
|
||||
[
|
||||
# items ordering gets corrected
|
||||
([A, C, B], [A, B, C], ({A: A, B: B, C: C}, [], [])),
|
||||
# unmatched tracks are returned as 'extra_tracks'
|
||||
# the first track is unmatched
|
||||
([B, C], [A, B, C], ({B: B, C: C}, [], [A])),
|
||||
# the middle track is unmatched
|
||||
([A, C], [A, B, C], ({A: A, C: C}, [], [B])),
|
||||
# the last track is unmatched
|
||||
([A, B], [A, B, C], ({A: A, B: B}, [], [C])),
|
||||
# unmatched items are returned as 'extra_items'
|
||||
([A, C, B], [A, C], ({A: A, C: C}, [B], [])),
|
||||
],
|
||||
)
|
||||
def test_assign_tracks(self, item_titles, track_titles, expected):
|
||||
expected_mapping, expected_extra_items, expected_extra_tracks = expected
|
||||
|
||||
def test_order_works_with_invalid_track_numbers(self):
|
||||
items = []
|
||||
items.append(self.item("one", 1))
|
||||
items.append(self.item("three", 1))
|
||||
items.append(self.item("two", 1))
|
||||
trackinfo = []
|
||||
trackinfo.append(TrackInfo(title="one"))
|
||||
trackinfo.append(TrackInfo(title="two"))
|
||||
trackinfo.append(TrackInfo(title="three"))
|
||||
mapping, extra_items, extra_tracks = match.assign_items(
|
||||
items, trackinfo
|
||||
)
|
||||
assert extra_items == []
|
||||
assert extra_tracks == []
|
||||
assert mapping == {
|
||||
items[0]: trackinfo[0],
|
||||
items[1]: trackinfo[2],
|
||||
items[2]: trackinfo[1],
|
||||
}
|
||||
items = [Item(title=title) for title in item_titles]
|
||||
tracks = [TrackInfo(title=title) for title in track_titles]
|
||||
|
||||
def test_order_works_with_missing_tracks(self):
|
||||
items = []
|
||||
items.append(self.item("one", 1))
|
||||
items.append(self.item("two", 2))
|
||||
trackinfo = []
|
||||
trackinfo.append(TrackInfo(title="one"))
|
||||
trackinfo.append(TrackInfo(title="two"))
|
||||
trackinfo.append(TrackInfo(title="three"))
|
||||
mapping, extra_items, extra_tracks = match.assign_items(
|
||||
items, trackinfo
|
||||
)
|
||||
assert extra_items == []
|
||||
assert extra_tracks == [trackinfo[2]]
|
||||
assert mapping == {items[0]: trackinfo[0], items[1]: trackinfo[1]}
|
||||
mapping, extra_items, extra_tracks = match.assign_items(items, tracks)
|
||||
|
||||
def test_order_works_with_extra_tracks(self):
|
||||
items = []
|
||||
items.append(self.item("one", 1))
|
||||
items.append(self.item("two", 2))
|
||||
items.append(self.item("three", 3))
|
||||
trackinfo = []
|
||||
trackinfo.append(TrackInfo(title="one"))
|
||||
trackinfo.append(TrackInfo(title="three"))
|
||||
mapping, extra_items, extra_tracks = match.assign_items(
|
||||
items, trackinfo
|
||||
)
|
||||
assert extra_items == [items[1]]
|
||||
assert extra_tracks == []
|
||||
assert mapping == {items[0]: trackinfo[0], items[2]: trackinfo[1]}
|
||||
assert (
|
||||
{i.title: t.title for i, t in mapping.items()},
|
||||
[i.title for i in extra_items],
|
||||
[t.title for t in extra_tracks],
|
||||
) == (expected_mapping, expected_extra_items, expected_extra_tracks)
|
||||
|
||||
def test_order_works_when_track_names_are_entirely_wrong(self):
|
||||
# A real-world test case contributed by a user.
|
||||
|
|
@ -587,9 +543,6 @@ class AssignmentTest(unittest.TestCase):
|
|||
title=f"ben harper - Burn to Shine {i}",
|
||||
track=i,
|
||||
length=length,
|
||||
mb_trackid="",
|
||||
mb_albumid="",
|
||||
mb_artistid="",
|
||||
)
|
||||
|
||||
items = []
|
||||
|
|
@ -623,13 +576,9 @@ class AssignmentTest(unittest.TestCase):
|
|||
trackinfo.append(info(11, "Beloved One", 243.733))
|
||||
trackinfo.append(info(12, "In the Lord's Arms", 186.13300000000001))
|
||||
|
||||
mapping, extra_items, extra_tracks = match.assign_items(
|
||||
items, trackinfo
|
||||
)
|
||||
assert extra_items == []
|
||||
assert extra_tracks == []
|
||||
for item, info in mapping.items():
|
||||
assert items.index(item) == trackinfo.index(info)
|
||||
expected = dict(zip(items, trackinfo)), [], []
|
||||
|
||||
assert match.assign_items(items, trackinfo) == expected
|
||||
|
||||
|
||||
class ApplyTestUtil:
|
||||
|
|
|
|||
Loading…
Reference in a new issue