Skip to content

Commit

Permalink
fix Codacy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dvirtz committed Feb 18, 2024
1 parent a1d0903 commit ae67dbd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 60 deletions.
7 changes: 3 additions & 4 deletions plugins/add_to_collection/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from picard.plugins.add_to_collection.manifest import *
from picard.plugins.add_to_collection.options import register_options
from picard.plugins.add_to_collection.post_save_processor import register_processor
from picard.plugins.add_to_collection import options, post_save_processor

register_options()
register_processor()
options.register_options()
post_save_processor.register_processor()
8 changes: 4 additions & 4 deletions plugins/add_to_collection/override_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


@contextmanager
def override_module(object: object) -> Generator[None, None, None]:
def override_module(obj: object) -> Generator[None, None, None]:
# picard expects hooks to be defined at module level
module = object.__module__
object.__module__ = ".".join(module.split(".")[:-1])
module = obj.__module__
obj.__module__ = ".".join(module.split(".")[:-1])
yield
object.__module__ = module
obj.__module__ = module
2 changes: 1 addition & 1 deletion plugins/add_to_collection/post_save_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def post_save_processor(file: File) -> None:
collection_id = settings.collection_id()
if not collection_id:
log.error(f"cannot find collection ID setting")
log.error("cannot find collection ID setting")
return
collection: Collection = user_collections.get(collection_id)
if not collection:
Expand Down
3 changes: 1 addition & 2 deletions test/plugin_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import shutil
import sys
import unittest
from os import PathLike
from tempfile import mkdtemp, mkstemp
from types import ModuleType
from typing import Any, Callable, Optional
Expand All @@ -38,7 +37,7 @@
from picard import config, log
from picard.i18n import setup_gettext
from picard.plugin import _PLUGIN_MODULE_PREFIX, _unregister_module_extensions
from picard.pluginmanager import PluginManager, plugin_dirs
from picard.pluginmanager import PluginManager
from picard.releasegroup import ReleaseGroup
from PyQt5 import QtCore

Expand Down
81 changes: 32 additions & 49 deletions test/test_add_to_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@


class TestAddToCollection(PluginTestCase):
SAVE_FILE_SETTINGS = {
"dont_write_tags": True,
"rename_files": False,
"move_files": False,
"delete_empty_dirs": False,
"save_images_to_files": False,
"clear_existing_tags": False,
"compare_ignore_tags": [],
}

def install_plugin(self) -> None:
self.plugin = self._test_plugin_install(
"Add to collection", "add_to_collection"
"Add to Collection", "add_to_collection"
)

def create_file(self, file_name: str, album_id: str) -> File:
file_path = os.path.join(self.tmp_directory, file_name)
file = File(file_path)
file.metadata["musicbrainz_albumid"] = album_id
self.tagger.files[file_path] = file
return file

def tearDown(self) -> None:
user_collections.clear()
return super().tearDown()

def test_hooks_installed(self) -> None:
self.install_plugin()

Expand All @@ -32,21 +53,12 @@ def test_file_save(self) -> None:

self.set_config_values(
setting={
**self.SAVE_FILE_SETTINGS,
"add_to_collection_id": "test_collection_id",
"dont_write_tags": True,
"rename_files": False,
"move_files": False,
"delete_empty_dirs": False,
"save_images_to_files": False,
"clear_existing_tags": False,
"compare_ignore_tags": [],
}
)

file_path = os.path.join(self.tmp_directory, "test.mp3")
file = File(file_path)
self.tagger.files[file_path] = file
file.metadata["musicbrainz_albumid"] = "test_album_id"
file = self.create_file(file_name="test.mp3", album_id="test_album_id")
user_collections["test_collection_id"] = Collection(
collection_id="test_collection_id", name="Test Collection", size=0
)
Expand All @@ -59,14 +71,8 @@ def test_two_files_save(self) -> None:

self.set_config_values(
setting={
**self.SAVE_FILE_SETTINGS,
"add_to_collection_id": "test_collection_id",
"dont_write_tags": True,
"rename_files": False,
"move_files": False,
"delete_empty_dirs": False,
"save_images_to_files": False,
"clear_existing_tags": False,
"compare_ignore_tags": [],
}
)

Expand All @@ -79,33 +85,19 @@ def test_two_files_save(self) -> None:
side_effect=lambda ids, callback: collection.releases.update(ids),
) as add_releases:
for i in range(2):
file_path = os.path.join(self.tmp_directory, "test{}.mp3".format(i))
file = File(file_path)
self.tagger.files[file_path] = file
file.metadata["musicbrainz_albumid"] = "test_album_id"
file = self.create_file(
file_name=f"test{i}.mp3", album_id="test_album_id"
)
file.save()
# only added once
add_releases.assert_called_once_with(set(["test_album_id"]), callback=ANY)

def test_no_collection_id_setting(self) -> None:
self.install_plugin()

self.set_config_values(
setting={
"dont_write_tags": True,
"rename_files": False,
"move_files": False,
"delete_empty_dirs": False,
"save_images_to_files": False,
"clear_existing_tags": False,
"compare_ignore_tags": [],
}
)
self.set_config_values(setting=self.SAVE_FILE_SETTINGS)

file_path = os.path.join(self.tmp_directory, "test.mp3")
file = File(file_path)
self.tagger.files[file_path] = file
file.metadata["musicbrainz_albumid"] = "test_album_id"
file = self.create_file(file_name="test.mp3", album_id="test_album_id")
user_collections["test_collection_id"] = Collection(
collection_id="test_collection_id", name="Test Collection", size=0
)
Expand All @@ -118,21 +110,12 @@ def test_no_user_collection(self) -> None:

self.set_config_values(
setting={
**self.SAVE_FILE_SETTINGS,
"add_to_collection_id": "test_collection_id",
"dont_write_tags": True,
"rename_files": False,
"move_files": False,
"delete_empty_dirs": False,
"save_images_to_files": False,
"clear_existing_tags": False,
"compare_ignore_tags": [],
}
)

file_path = os.path.join(self.tmp_directory, "test.mp3")
file = File(file_path)
self.tagger.files[file_path] = file
file.metadata["musicbrainz_albumid"] = "test_album_id"
file = self.create_file(file_name="test.mp3", album_id="test_album_id")
with patch("picard.collection.Collection.add_releases") as add_releases:
file.save()
add_releases.assert_not_called()

0 comments on commit ae67dbd

Please sign in to comment.