Skip to content

Commit

Permalink
refactor(tests): enhance QThread coverage and improve audio file tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmozhijin committed Dec 11, 2024
1 parent e144784 commit 06d1532
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
15 changes: 11 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:

Expand All @@ -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)
24 changes: 22 additions & 2 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/test_gui_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 06d1532

Please sign in to comment.