Files
manim-slides/tests/test_wizard.py
Jérome Eertmans 2853ed08e1 chore(tests): adding tests for Qt widgets (#300)
* chore(tests): adding tests for Qt widgets

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: reset WINDOW name

* chore(tests): addign tests

* chore: adding more tests

* fix how bin existence is checked

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: which takes str

* more tests!

* todo: fix

* change verbosity in tests

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-10-27 16:40:05 +02:00

182 lines
4.9 KiB
Python

from pathlib import Path
from click.testing import CliRunner
from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
QApplication,
QMessageBox,
)
from pytest import MonkeyPatch
from pytestqt.qtbot import QtBot
from manim_slides.config import Config, Key
from manim_slides.defaults import CONFIG_PATH
from manim_slides.wizard import KeyInput, Wizard, init, wizard
class TestKeyInput:
def test_default_is_none(self, qtbot: QtBot) -> None:
widget = KeyInput()
widget.show()
qtbot.addWidget(widget)
assert widget.key is None
def test_send_key(self, qtbot: QtBot) -> None:
widget = KeyInput()
widget.show()
qtbot.addWidget(widget)
qtbot.keyPress(widget, Qt.Key_Q)
assert widget.key is Qt.Key_Q.value
class TestWizard:
def test_close_without_saving(self, qtbot: QtBot) -> None:
wizard = Wizard(Config())
wizard.show()
qtbot.addWidget(wizard)
wizard.button_box.rejected.emit()
assert wizard.closed_without_saving
def test_save_valid_config(self, qtbot: QtBot) -> None:
widget = Wizard(Config())
widget.show()
qtbot.addWidget(widget)
widget.button_box.accepted.emit()
assert not widget.closed_without_saving
def test_save_invalid_config(self, qtbot: QtBot, monkeypatch: MonkeyPatch) -> None:
wizard = Wizard(Config())
wizard.show()
qtbot.addWidget(wizard)
def open_dialog(button_number: int, key: Key) -> None:
button = wizard.buttons[button_number]
dialog = KeyInput()
qtbot.addWidget(dialog)
qtbot.keyPress(dialog, Qt.Key_Q)
assert dialog.key is not None
key.set_ids(dialog.key)
button.setText("Q")
assert button.text() == "Q"
dialog.close()
message_boxes = []
def exec_patched(self: QMessageBox) -> None:
self.show()
message_boxes.append(self)
monkeypatch.setattr(QMessageBox, "exec", exec_patched)
for i, (key, _) in enumerate(wizard.config.keys.dict().items()):
open_dialog(i, getattr(wizard.config.keys, key))
wizard.button_box.accepted.emit()
message_box = message_boxes.pop()
qtbot.addWidget(message_box)
assert message_box.isVisible()
def test_init() -> None:
runner = CliRunner()
with runner.isolated_filesystem():
assert not CONFIG_PATH.exists()
results = runner.invoke(
init,
)
assert results.exit_code == 0
assert CONFIG_PATH.exists()
assert Config().dict() == Config.from_file(CONFIG_PATH).dict()
def test_init_custom_path() -> None:
runner = CliRunner()
custom_path = Path("config.toml")
with runner.isolated_filesystem():
assert not custom_path.exists()
results = runner.invoke(
init,
["--config", str(custom_path)],
)
assert results.exit_code == 0
assert not CONFIG_PATH.exists()
assert custom_path.exists()
assert Config().dict() == Config.from_file(custom_path).dict()
def test_init_path_exists() -> None:
runner = CliRunner()
with runner.isolated_filesystem():
assert not CONFIG_PATH.exists()
results = runner.invoke(
init,
)
assert results.exit_code == 0
assert CONFIG_PATH.exists()
assert Config().dict() == Config.from_file(CONFIG_PATH).dict()
results = runner.invoke(init, input="o")
assert results.exit_code == 0
results = runner.invoke(init, input="m")
assert results.exit_code == 0
results = runner.invoke(init, input="q")
assert results.exit_code == 0
def test_wizard(monkeypatch: MonkeyPatch) -> None:
runner = CliRunner()
with runner.isolated_filesystem():
assert not CONFIG_PATH.exists()
def show(self: Wizard) -> None:
self.button_box.accepted.emit()
def exec_patched(self: QApplication) -> None:
pass
monkeypatch.setattr(Wizard, "show", show)
monkeypatch.setattr(QApplication, "exec", exec_patched)
results = runner.invoke(
wizard,
)
assert results.exit_code == 0
assert CONFIG_PATH.exists()
assert Config().dict() == Config.from_file(CONFIG_PATH).dict()
def test_wizard_closed_without_saving(monkeypatch: MonkeyPatch) -> None:
runner = CliRunner()
with runner.isolated_filesystem():
assert not CONFIG_PATH.exists()
def show(self: Wizard) -> None:
self.button_box.rejected.emit()
def exec_patched(self: QApplication) -> None:
pass
monkeypatch.setattr(Wizard, "show", show)
monkeypatch.setattr(QApplication, "exec", exec_patched)
results = runner.invoke(
wizard,
)
assert results.exit_code == 0
assert not CONFIG_PATH.exists()