From 06d15326b1acf44ce76513b8925347f736d9a3bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=89=E9=BB=98=E3=81=AE=E9=87=91?= <110812055+chenmozhijin@users.noreply.github.com> Date: Thu, 12 Dec 2024 01:32:54 +0800 Subject: [PATCH] refactor(tests): enhance QThread coverage and improve audio file tagging --- tests/conftest.py | 15 +++++++++++---- tests/helper.py | 24 ++++++++++++++++++++++-- tests/test_gui_search.py | 1 + 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index cc75111..709788c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,7 +8,7 @@ from typing import Any import pytest -from PySide6.QtCore import QRunnable +from PySide6.QtCore import QRunnable, QThread from LDDC.utils.cache import cache from LDDC.utils.logger import logger @@ -20,14 +20,14 @@ def init() -> None: cache.clear() -@pytest.fixture(scope="function", autouse=True) +@pytest.fixture(autouse=True) def cover_qthreadpool(monkeypatch: pytest.MonkeyPatch) -> None: - def run_with_trace(self) -> None: + def run_with_trace(self: QRunnable | QThread) -> None: if "coverage" in sys.modules: # https://github.com/nedbat/coveragepy/issues/686#issuecomment-634932753 sys.settrace(threading._trace_hook) # type: ignore[reportAttributeAccessIssue] # noqa: SLF001 - self._base_run() + self._base_run() # type: ignore[reportAttributeAccessIssue] def _start(worker: QRunnable | Callable, *args: Any, **kwargs: Any) -> None: @@ -41,3 +41,10 @@ def _start(worker: QRunnable | Callable, *args: Any, **kwargs: Any) -> None: threadpool.no_patch_start = threadpool.start # type: ignore[reportAttributeAccessIssue] monkeypatch.setattr(threadpool, "start", _start) + + def _init(self: QThread, *args: Any, **kwargs: Any) -> None: + QThread.__init__(self, *args, **kwargs) + self._base_run = self.run # type: ignore[reportAttributeAccessIssue] + self.run = partial(run_with_trace, self) + + monkeypatch.setattr(QThread, "__init__", _init) diff --git a/tests/helper.py b/tests/helper.py index edc43e2..b32c79f 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -3,7 +3,8 @@ import os from mutagen import File, FileType # type: ignore[reportPrivateImportUsage] mutagen中的File被误定义为私有 quodlibet/mutagen#647 -from mutagen.id3 import USLT # type: ignore[reportPrivateImportUsage] +from mutagen.easyid3 import EasyID3 +from mutagen.id3 import ID3, USLT # type: ignore[reportPrivateImportUsage] from pydub import AudioSegment from PySide6.QtWidgets import QApplication, QFileDialog, QMessageBox, QWidget @@ -65,7 +66,26 @@ def create_audio_file(path: str, audio_format: str, duration: int, tags: dict[st if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path), exist_ok=True) silent_audio = AudioSegment.silent(duration=duration * 1000, frame_rate=44100) - silent_audio.export(path, format=audio_format, tags=tags) + silent_audio.export(path, format=audio_format) + if tags: + audio = File(path, easy=True) + if isinstance(audio, FileType): + if audio.tags is None: + audio.add_tags() + + if isinstance(audio.tags, ID3): + # 对于ID3类型的标签(wave文件),使用mutagen.easyid3.EasyID3来转换 + easy_tags = EasyID3() + for tag, value in tags.items(): + easy_tags[tag] = value + id3: ID3 = easy_tags._EasyID3__id3 # id3是EasyID3的一个私有变量 # type: ignore[reportAttributeAccessIssue] # noqa: SLF001 + for value in id3.values(): + audio.tags.add(value) + else: + for tag, value in tags.items(): + audio[tag] = value + + audio.save() def verify_audio_lyrics(path: str) -> None: diff --git a/tests/test_gui_search.py b/tests/test_gui_search.py index cf74372..75b63ba 100644 --- a/tests/test_gui_search.py +++ b/tests/test_gui_search.py @@ -162,6 +162,7 @@ def check_preview_result() -> bool: main_window.search_widget.preview_plainTextEdit.toPlainText() != orig_text) qtbot.waitUntil(check_preview_result, timeout=15000) + qtbot.wait(20) grab(main_window, os.path.join(screenshot_path, f"preview_{lyrics_format.name.lower()}.png")) return main_window.search_widget.preview_plainTextEdit.toPlainText()