Refactor tests using pytest

This commit is contained in:
Vrihub 2025-09-30 15:54:33 +02:00
parent 80ffa4879d
commit 638afc3d2c

View file

@ -1,5 +1,4 @@
# This file is part of beets.
# Copyright 2016, Jan-Erik Dahlin.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@ -14,129 +13,87 @@
"""Tests for the fromfilename plugin."""
import unittest
from unittest.mock import Mock
import pytest
from beetsplug import fromfilename
class FromfilenamePluginTest(unittest.TestCase):
def setUp(self):
"""Create mock objects for import session and task."""
self.session = Mock()
item1config = {"path": "", "track": 0, "artist": "", "title": ""}
self.item1 = Mock(**item1config)
item2config = {"path": "", "track": 0, "artist": "", "title": ""}
self.item2 = Mock(**item2config)
taskconfig = {"is_album": True, "items": [self.item1, self.item2]}
self.task = Mock(**taskconfig)
def tearDown(self):
del self.session, self.task, self.item1, self.item2
def test_sep_sds(self):
"""Test filenames that use " - " as separator."""
self.item1.path = "/music/files/01 - Artist Name - Song One.m4a"
self.item2.path = "/music/files/02. - Artist Name - Song Two.m4a"
f = fromfilename.FromFilenamePlugin()
f.filename_task(self.task, self.session)
assert self.task.items[0].track == 1
assert self.task.items[1].track == 2
assert self.task.items[0].artist == "Artist Name"
assert self.task.items[1].artist == "Artist Name"
assert self.task.items[0].title == "Song One"
assert self.task.items[1].title == "Song Two"
def test_sep_dash(self):
"""Test filenames that use "-" as separator."""
self.item1.path = "/music/files/01-Artist_Name-Song_One.m4a"
self.item2.path = "/music/files/02.-Artist_Name-Song_Two.m4a"
f = fromfilename.FromFilenamePlugin()
f.filename_task(self.task, self.session)
assert self.task.items[0].track == 1
assert self.task.items[1].track == 2
assert self.task.items[0].artist == "Artist_Name"
assert self.task.items[1].artist == "Artist_Name"
assert self.task.items[0].title == "Song_One"
assert self.task.items[1].title == "Song_Two"
def test_track_title(self):
"""Test filenames including track and title."""
self.item1.path = "/music/files/01 - Song_One.m4a"
self.item2.path = "/music/files/02. Song_Two.m4a"
f = fromfilename.FromFilenamePlugin()
f.filename_task(self.task, self.session)
assert self.task.items[0].track == 1
assert self.task.items[1].track == 2
assert self.task.items[0].artist == ""
assert self.task.items[1].artist == ""
assert self.task.items[0].title == "Song_One"
assert self.task.items[1].title == "Song_Two"
def test_title_by_artist(self):
"""Test filenames including title by artist."""
self.item1.path = "/music/files/Song One by The Artist.m4a"
self.item2.path = "/music/files/Song Two by The Artist.m4a"
f = fromfilename.FromFilenamePlugin()
f.filename_task(self.task, self.session)
assert self.task.items[0].track == 0
assert self.task.items[1].track == 0
assert self.task.items[0].artist == "The Artist"
assert self.task.items[1].artist == "The Artist"
assert self.task.items[0].title == "Song One"
assert self.task.items[1].title == "Song Two"
def test_track_only(self):
"""Test filenames including only track."""
self.item1.path = "/music/files/01.m4a"
self.item2.path = "/music/files/02.m4a"
f = fromfilename.FromFilenamePlugin()
f.filename_task(self.task, self.session)
assert self.task.items[0].track == 1
assert self.task.items[1].track == 2
assert self.task.items[0].artist == ""
assert self.task.items[1].artist == ""
assert self.task.items[0].title == "01"
assert self.task.items[1].title == "02"
def test_title_only(self):
"""Test filenames including only title."""
self.item1.path = "/music/files/Song One.m4a"
self.item2.path = "/music/files/Song Two.m4a"
f = fromfilename.FromFilenamePlugin()
f.filename_task(self.task, self.session)
assert self.task.items[0].track == 0
assert self.task.items[1].track == 0
assert self.task.items[0].artist == ""
assert self.task.items[1].artist == ""
assert self.task.items[0].title == "Song One"
assert self.task.items[1].title == "Song Two"
class Session:
pass
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
class Item:
def __init__(self, path):
self.path = path
self.track = 0
self.artist = ""
self.title = ""
if __name__ == "__main__":
unittest.main(defaultTest="suite")
class Task:
def __init__(self, items):
self.items = items
self.is_album = True
@pytest.mark.parametrize(
"song1, song2",
[
(
(
"/tmp/01 - The Artist - Song One.m4a",
1,
"The Artist",
"Song One",
),
(
"/tmp/02. - The Artist - Song Two.m4a",
2,
"The Artist",
"Song Two",
),
),
(
("/tmp/01-The_Artist-Song_One.m4a", 1, "The_Artist", "Song_One"),
("/tmp/02.-The_Artist-Song_Two.m4a", 2, "The_Artist", "Song_Two"),
),
(
("/tmp/01 - Song_One.m4a", 1, "", "Song_One"),
("/tmp/02. - Song_Two.m4a", 2, "", "Song_Two"),
),
(
("/tmp/Song One by The Artist.m4a", 0, "The Artist", "Song One"),
("/tmp/Song Two by The Artist.m4a", 0, "The Artist", "Song Two"),
),
(("/tmp/01.m4a", 1, "", "01"), ("/tmp/02.m4a", 2, "", "02")),
(
("/tmp/Song One.m4a", 0, "", "Song One"),
("/tmp/Song Two.m4a", 0, "", "Song Two"),
),
],
)
def test_fromfilename(song1, song2):
"""
Each "song" is a tuple of path, expected track number, expected artist,
expected title.
We use two songs for each test for two reasons:
- The plugin needs more than one item to look for uniform strings in paths
in order to guess if the string describes an artist or a title.
- Sometimes we allow for an optional "." after the track number in paths.
"""
session = Session()
item1 = Item(song1[0])
item2 = Item(song2[0])
task = Task([item1, item2])
f = fromfilename.FromFilenamePlugin()
f.filename_task(task, session)
assert task.items[0].track == song1[1]
assert task.items[0].artist == song1[2]
assert task.items[0].title == song1[3]
assert task.items[1].track == song2[1]
assert task.items[1].artist == song2[2]
assert task.items[1].title == song2[3]