From 084cf6490e630962d7b40e7e8bf7600c13e4ffa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Tue, 31 Dec 2024 08:12:03 +0000 Subject: [PATCH] matching: add additional test cases and refactor tests --- test/test_autotag.py | 119 +++++++++++++------------------------------ 1 file changed, 34 insertions(+), 85 deletions(-) diff --git a/test/test_autotag.py b/test/test_autotag.py index d6f4606e1..31afc6a63 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -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: