Skip to content

Commit

Permalink
WIP work on refactor to parametrized pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
JOJ0 committed Jan 2, 2025
1 parent 2bea964 commit 8138708
Showing 1 changed file with 133 additions and 71 deletions.
204 changes: 133 additions & 71 deletions test/plugins/test_lastgenre.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from unittest.mock import Mock

import pytest

from beets import config
from beets.test import _common
from beets.test.helper import BeetsTestCase
Expand Down Expand Up @@ -163,77 +165,7 @@ def get_top_tags(self):
res = plugin._tags_for(MockPylastObj(), min_weight=50)
assert res == ["pop"]

def test_get_genre(self):
"""All possible (genre, label) pairs"""
mock_genres = {"track": "1", "album": "2", "artist": "3"}

def mock_fetch_track_genre(self, obj=None):
return mock_genres["track"]

def mock_fetch_album_genre(self, obj):
return mock_genres["album"]

def mock_fetch_artist_genre(self, obj):
return mock_genres["artist"]

lastgenre.LastGenrePlugin.fetch_track_genre = mock_fetch_track_genre
lastgenre.LastGenrePlugin.fetch_album_genre = mock_fetch_album_genre
lastgenre.LastGenrePlugin.fetch_artist_genre = mock_fetch_artist_genre

self._setup_config(whitelist=False)
item = _common.item()
item.genre = mock_genres["track"]

# The default setting
config["lastgenre"] = {"force": False, "keep_allowed": True}
res = self.plugin._get_genre(item)
assert res == (item.genre, "keep allowed")

# Not forcing and keeping any existing
config["lastgenre"] = {"force": False, "keep_allowed": False}
res = self.plugin._get_genre(item)
assert res == (item.genre, "keep any")

# Track
config["lastgenre"] = {"force": True, "keep_allowed": False, "source": "track"}
res = self.plugin._get_genre(item)
assert res == (mock_genres["track"], "track")

config["lastgenre"] = {"force": True, "keep_allowed": True, "source": "track"}
res = self.plugin._get_genre(item)
assert res == (mock_genres["track"], "keep + track")
print("res after track check:", res)

# Album
config["lastgenre"] = {"source": "album", "keep_allowed": False}
res = self.plugin._get_genre(item)
print("res is:", res)
print("mock_genres is:", mock_genres["album"])
assert res == (mock_genres["album"], "album")

config["lastgenre"] = {"source": "album", "keep_allowed": True}
res = self.plugin._get_genre(item)
assert res == (mock_genres["album"], "keep + album")

# Artist
config["lastgenre"] = {"source": "artist", "keep_allowed": False}
res = self.plugin._get_genre(item)
assert res == (mock_genres["artist"], "artist")

config["lastgenre"] = {"source": "artist", "keep_allowed": True}
res = self.plugin._get_genre(item)
assert res == (mock_genres["artist"], "keep + artist")

# Original
mock_genres["artist"] = None
res = self.plugin._get_genre(item)
assert res == (item.genre, "original")

# Fallback
config["lastgenre"] = {"fallback": "rap", "keep_allowed": False}
item.genre = None
res = self.plugin._get_genre(item)
assert res == (config["lastgenre"]["fallback"].get(), "fallback")
import pytest

Check failure on line 168 in test/plugins/test_lastgenre.py

View workflow job for this annotation

GitHub Actions / Check linting

Ruff (F401)

test/plugins/test_lastgenre.py:168:16: F401 `pytest` imported but unused

def test_sort_by_depth(self):
self._setup_config(canonical=True)
Expand All @@ -245,3 +177,133 @@ def test_sort_by_depth(self):
tags = ("electronic", "ambient", "chillout")
res = self.plugin._sort_by_depth(tags)
assert res == ["ambient", "electronic"]

@pytest.mark.parametrize(
"config_values, item_genre, mock_genres, expected_result",
[
# 0 - default setting. keep whitelisted exisiting, new for empty tags.
(
{
"force": False,
"keep_allowed": True,
"source": "album",
"whitelist": True,
},
"allowed genre",
{
"album": "another allowed genre",
},
("allowed genre", "keep allowed"),
),
# 1 - default setting when whitelisted+unknown genre existing
(
{
"force": False,
"keep_allowed": True,
"source": "album",
"whitelist": True,
},
"unknown genre, allowed genre",
{
"album": "another allowed genre",
},
("allowed genre", "keep allowed"),
),
# 2 - default setting when only unknown genre existing
# clears existing but does not add new genre. Not desired but expected.
(
{
"force": False,
"keep_allowed": True,
"source": "album",
"whitelist": True,
},
"unknown genre",
{
"album": "another allowed genre",
},
("", "keep allowed"),
),
# 3 - default setting on empty tag
(
{
"force": False,
"keep_allowed": True,
"source": "album",
"whitelist": True,
},
"",
{
"album": "another allowed genre",
},
("another allowed genre", "album"),
),
# 4 - force and keep whitelisted
(
{
"force": True,
"keep_allowed": True,
"source": "album",
"whitelist": True,
},
"allowed genre, unknown genre",
{
"album": "another allowed genre",
},
("allowed genre, another allowed genre", "keep + album"),
),
# 5 - force and keep whitelisted. artist fallback
(
{
"force": True,
"keep_allowed": True,
"source": "artist",
"whitelist": True,
},
"allowed genre, unknown genre",
{
"album": "also allowed genre",
"artist": "another allowed genre",
},
("allowed genre, another allowed genre", "keep + artist"),
),
]
)
def test_get_genre(config_values, item_genre, mock_genres, expected_result):
"""Test _get_genre with various configurations."""

def mock_fetch_track_genre(self, obj=None):
return mock_genres["track"]

def mock_fetch_album_genre(self, obj):
return mock_genres["album"]

def mock_fetch_artist_genre(self, obj):
return mock_genres["artist"]

# Mock the last.fm fetchers. When whitelist config option set, we can assume
# only whitelisted genres get returned, the plugin's _resolve_genre method
# ensures it.
lastgenre.LastGenrePlugin.fetch_track_genre = mock_fetch_track_genre
lastgenre.LastGenrePlugin.fetch_album_genre = mock_fetch_album_genre
lastgenre.LastGenrePlugin.fetch_artist_genre = mock_fetch_artist_genre

# Initialize plugin instance and item
plugin = lastgenre.LastGenrePlugin()
item = _common.item()
item.genre = item_genre

# Set configuration
config["lastgenre"] = config_values

# Mock the whitelist instance variable
plugin.whitelist = set([
"allowed genre",
"also allowed genre",
"another allowed genre",
]) if config_values.get("whitelist") else set([])

# Run the test and check the result
res = plugin._get_genre(item)
print(res)
assert res == expected_result

0 comments on commit 8138708

Please sign in to comment.