Skip to content

Commit dd61c89

Browse files
committed
lastgenre: Refactor test_pretend to pytest
1 parent b84df46 commit dd61c89

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

test/plugins/test_lastgenre.py

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Tests for the 'lastgenre' plugin."""
1616

17-
from unittest.mock import Mock, patch
17+
from unittest.mock import Mock
1818

1919
import pytest
2020

@@ -131,44 +131,6 @@ def test_prefer_specific_without_canonical(self):
131131
"math rock",
132132
]
133133

134-
def test_pretend_option_skips_library_updates(self):
135-
item = self.create_item(
136-
album="Pretend Album",
137-
albumartist="Pretend Artist",
138-
artist="Pretend Artist",
139-
title="Pretend Track",
140-
genre="Original Genre",
141-
)
142-
album = self.lib.add_album([item])
143-
144-
command = self.plugin.commands()[0]
145-
opts, args = command.parser.parse_args(["--pretend"])
146-
147-
with patch.object(lastgenre.ui, "should_write", return_value=True):
148-
with patch.object(
149-
self.plugin,
150-
"_get_genre",
151-
return_value=("Mock Genre", "mock stage"),
152-
) as mock_get_genre:
153-
with patch.object(self.plugin._log, "info") as log_info:
154-
# Mock try_write to verify it's never called in pretend mode
155-
with patch.object(item, "try_write") as mock_try_write:
156-
command.func(self.lib, opts, args)
157-
158-
mock_get_genre.assert_called_once()
159-
160-
assert any(
161-
call.args[0].startswith("Pretend:")
162-
for call in log_info.call_args_list
163-
)
164-
165-
# Verify that try_write was never called (file operations skipped)
166-
mock_try_write.assert_not_called()
167-
168-
stored_album = self.lib.get_album(album.id)
169-
assert stored_album.genre == "Original Genre"
170-
assert stored_album.items()[0].genre == "Original Genre"
171-
172134
def test_no_duplicate(self):
173135
"""Remove duplicated genres."""
174136
self._setup_config(count=99)
@@ -210,6 +172,53 @@ def test_sort_by_depth(self):
210172
assert res == ["ambient", "electronic"]
211173

212174

175+
def test_pretend_option_skips_library_updates(mocker):
176+
"""Test that pretend mode logs actions but skips library updates."""
177+
178+
# Setup
179+
test_case = BeetsTestCase()
180+
test_case.setUp()
181+
plugin = lastgenre.LastGenrePlugin()
182+
item = test_case.create_item(
183+
album="Album",
184+
albumartist="Artist",
185+
artist="Artist",
186+
title="Track",
187+
genre="Original Genre",
188+
)
189+
album = test_case.lib.add_album([item])
190+
command = plugin.commands()[0]
191+
opts, args = command.parser.parse_args(["--pretend"])
192+
193+
# Mocks
194+
mocker.patch.object(lastgenre.ui, "should_write", return_value=True)
195+
mock_get_genre = mocker.patch.object(
196+
plugin, "_get_genre", return_value=("New Genre", "log label")
197+
)
198+
mock_log = mocker.patch.object(plugin._log, "info")
199+
mock_write = mocker.patch.object(item, "try_write")
200+
201+
# Run lastgenre
202+
command.func(test_case.lib, opts, args)
203+
mock_get_genre.assert_called_once()
204+
205+
# Test logging
206+
assert any(
207+
call.args[0].startswith("Pretend:")
208+
for call in mock_log.call_args_list
209+
), "Expected pretend mode logging messages"
210+
211+
# Test file operations should be skipped
212+
mock_write.assert_not_called()
213+
214+
# Test database should remain unchanged
215+
stored_album = test_case.lib.get_album(album.id)
216+
assert stored_album.genre == "Original Genre"
217+
assert stored_album.items()[0].genre == "Original Genre"
218+
219+
test_case.tearDown()
220+
221+
213222
@pytest.mark.parametrize(
214223
"config_values, item_genre, mock_genres, expected_result",
215224
[

0 commit comments

Comments
 (0)