Skip to content

Commit

Permalink
feat(tests): add conftest and enhance gui search tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmozhijin committed Dec 11, 2024
1 parent eb0d746 commit 4204b47
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 51 deletions.
43 changes: 43 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: Copyright (C) 2024 沉默の金 <cmzj@cmzj.org>
# SPDX-License-Identifier: GPL-3.0-only

import sys
import threading
from collections.abc import Callable
from functools import partial
from typing import Any

import pytest
from PySide6.QtCore import QRunnable

from LDDC.utils.cache import cache
from LDDC.utils.logger import logger
from LDDC.utils.thread import threadpool


@pytest.fixture(scope="session", autouse=True)
def init() -> None:
cache.clear()


@pytest.fixture(scope="function", autouse=True)
def cover_qthreadpool(monkeypatch: pytest.MonkeyPatch) -> None:

def run_with_trace(self) -> 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()

def _start(worker: QRunnable | Callable, *args: Any, **kwargs: Any) -> None:

if isinstance(worker, QRunnable):
worker._base_run = worker.run # type: ignore[reportAttributeAccessIssue] # noqa: SLF001
worker.run = partial(run_with_trace, worker)
threadpool.no_patch_start(worker, *args, **kwargs) # type: ignore[reportAttributeAccessIssue]
else:
threadpool.no_patch_start(worker, *args, **kwargs) # type: ignore[reportAttributeAccessIssue]
logger.warning("No support coverage for start a function in QThreadPool")

threadpool.no_patch_start = threadpool.start # type: ignore[reportAttributeAccessIssue]
monkeypatch.setattr(threadpool, "start", _start)
84 changes: 33 additions & 51 deletions tests/test_gui_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ def check_table() -> bool:
qtbot.waitUntil(check_table)
qtbot.wait(20)
grab(main_window, os.path.join(screenshot_path, f"search_{search_type.name.lower()}_{Source(i).name.lower()}.png"))

# preview
# 双击第一行
for _ in range(2):
QTest.mouseDClick(main_window.search_widget.results_tableWidget.viewport(),
Qt.MouseButton.LeftButton,
pos=QPoint(int(main_window.search_widget.results_tableWidget.width() * 0.5),
int(main_window.search_widget.results_tableWidget.horizontalHeader().height())))

orig_lyrics = main_window.search_widget.preview_lyric_result

def check_preview_lyric() -> bool:
return main_window.search_widget.preview_lyric_result != orig_lyrics and len(main_window.search_widget.preview_plainTextEdit.toPlainText()) > 15 # noqa: B023

def check_album_table() -> bool:
return main_window.search_widget.results_tableWidget.property("result_type")[0] == "album"

def check_song_list_table() -> bool:
return main_window.search_widget.results_tableWidget.property("result_type")[0] == "songlist"

match search_type:
case SearchType.SONG:
qtbot.waitUntil(check_preview_lyric)
grab(main_window, os.path.join(screenshot_path, "preview_lyrics.png"))
verify_lyrics(main_window.search_widget.preview_plainTextEdit.toPlainText())
case SearchType.ALBUM:
qtbot.waitUntil(check_album_table)
grab(main_window, os.path.join(screenshot_path, "show_album_song.png"))
assert main_window.search_widget.results_tableWidget.rowCount() > 0
case SearchType.SONGLIST:
qtbot.waitUntil(check_song_list_table)
grab(main_window, os.path.join(screenshot_path, "show_songlist_song.png"))
assert main_window.search_widget.results_tableWidget.rowCount() > 0
qtbot.wait(200)

Expand All @@ -53,25 +85,6 @@ def test_search_song(qtbot: QtBot) -> None:
search(qtbot, SearchType.SONG, "鈴木このみ - アルカテイル")


def test_preview(qtbot: QtBot) -> None:
from LDDC.view.main_window import main_window

assert main_window.search_widget.results_tableWidget.rowCount() > 0
# 双击第一行
for _ in range(2):
QTest.mouseDClick(main_window.search_widget.results_tableWidget.viewport(),
Qt.MouseButton.LeftButton,
pos=QPoint(int(main_window.search_widget.results_tableWidget.width() * 0.5),
int(main_window.search_widget.results_tableWidget.horizontalHeader().height())))

def check_preview_text() -> bool:
return main_window.search_widget.preview_lyric_result is not None

qtbot.waitUntil(check_preview_text)
grab(main_window, os.path.join(screenshot_path, "preview.png"))
verify_lyrics(main_window.search_widget.preview_plainTextEdit.toPlainText())


def test_save_to_dir() -> None:
from LDDC.view.main_window import main_window

Expand Down Expand Up @@ -177,25 +190,6 @@ def test_search_song_list(qtbot: QtBot) -> None:
search(qtbot, SearchType.SONGLIST, "key社")


def test_open_song_list(qtbot: QtBot) -> None:
from LDDC.view.main_window import main_window

assert main_window.search_widget.results_tableWidget.rowCount() > 0
# 双击第一行
for _ in range(2):
QTest.mouseDClick(main_window.search_widget.results_tableWidget.viewport(),
Qt.MouseButton.LeftButton,
pos=QPoint(int(main_window.search_widget.results_tableWidget.width() * 0.5),
int(main_window.search_widget.results_tableWidget.horizontalHeader().height())))

def check_table() -> bool:
return main_window.search_widget.results_tableWidget.property("result_type")[0] == "songlist"

qtbot.waitUntil(check_table)
grab(main_window, os.path.join(screenshot_path, "show_songlist.png"))
assert main_window.search_widget.results_tableWidget.rowCount() > 0


def test_search_album(qtbot: QtBot) -> None:
search(qtbot, SearchType.ALBUM, "アスタロア/青き此方/夏の砂時計")

Expand All @@ -204,19 +198,7 @@ def test_save_album_lyrics(qtbot: QtBot) -> None:
from LDDC.view.main_window import main_window

assert main_window.search_widget.results_tableWidget.rowCount() > 0
# 双击第一行
for _ in range(2):
QTest.mouseDClick(main_window.search_widget.results_tableWidget.viewport(),
Qt.MouseButton.LeftButton,
pos=QPoint(int(main_window.search_widget.results_tableWidget.width() * 0.5),
int(main_window.search_widget.results_tableWidget.horizontalHeader().height())))

def check_table() -> bool:
return main_window.search_widget.results_tableWidget.property("result_type")[0] == "album"

qtbot.waitUntil(check_table)
grab(main_window, os.path.join(screenshot_path, "show_album.png"))
assert main_window.search_widget.results_tableWidget.rowCount() > 0
assert main_window.search_widget.results_tableWidget.property("result_type")[0] == "album"

# 保存歌词
orig_path = main_window.search_widget.save_path_lineEdit.text()
Expand Down

0 comments on commit 4204b47

Please sign in to comment.