Code cleanup, more tests

This commit is contained in:
Miguel Jacq 2025-11-11 13:12:30 +11:00
parent 1c0052a0cf
commit bfd0314109
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
16 changed files with 1212 additions and 478 deletions

View file

@ -1,4 +1,6 @@
import pytest
import bouquin.settings_dialog as sd
from bouquin.db import DBManager, DBConfig
from bouquin.key_prompt import KeyPrompt
from bouquin.settings_dialog import SettingsDialog
@ -6,6 +8,7 @@ from bouquin.theme import ThemeManager, ThemeConfig, Theme
from PySide6.QtCore import QTimer
from PySide6.QtWidgets import QApplication, QMessageBox, QWidget
@pytest.mark.gui
def test_settings_dialog_config_roundtrip(qtbot, tmp_db_cfg, fresh_db, tmp_path):
# Provide a parent that exposes a real ThemeManager (dialog calls parent().themes.set(...))
@ -161,3 +164,64 @@ def test_change_key_success(qtbot, tmp_path, app):
assert db2.connect()
assert "seed" in db2.get_entry("2001-01-01")
db2.close()
def test_settings_compact_error(qtbot, app, monkeypatch, tmp_db_cfg, fresh_db):
# Parent with ThemeManager (dialog uses parent().themes.set(...))
parent = QWidget()
parent.themes = ThemeManager(app, ThemeConfig(theme=Theme.LIGHT))
dlg = SettingsDialog(tmp_db_cfg, fresh_db, parent=parent)
qtbot.addWidget(dlg)
dlg.show()
# Monkeypatch db.compact to raise
def boom():
raise RuntimeError("nope")
dlg._db.compact = boom # type: ignore
called = {"critical": False, "title": None, "text": None}
class DummyMB:
@staticmethod
def information(*args, **kwargs):
return 0
@staticmethod
def critical(parent, title, text, *rest):
called["critical"] = True
called["title"] = title
called["text"] = str(text)
return 0
# Swap QMessageBox used inside the dialog module so signature mismatch can't occur
monkeypatch.setattr(sd, "QMessageBox", DummyMB, raising=True)
# Invoke
dlg._compact_btn_clicked()
assert called["critical"]
assert called["title"]
assert called["text"]
@pytest.mark.gui
def test_settings_browse_sets_path(qtbot, app, tmp_path, fresh_db, monkeypatch):
parent = QWidget()
parent.themes = ThemeManager(app, ThemeConfig(theme=Theme.LIGHT))
cfg = DBConfig(
path=tmp_path / "x.db", key="k", idle_minutes=0, theme="light", move_todos=True
)
dlg = SettingsDialog(cfg, fresh_db, parent=parent)
qtbot.addWidget(dlg)
dlg.show()
p = tmp_path / "new_file.db"
monkeypatch.setattr(
sd.QFileDialog,
"getSaveFileName",
staticmethod(lambda *a, **k: (str(p), "DB Files (*.db)")),
raising=False,
)
dlg._browse()
assert dlg.path_edit.text().endswith("new_file.db")