Code cleanup/comments, more test coverage (92%)

This commit is contained in:
Miguel Jacq 2025-11-07 11:42:29 +11:00
parent 66950eeff5
commit 74177f2cd3
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
19 changed files with 463 additions and 22 deletions

View file

@ -9,6 +9,7 @@ from bouquin.settings import APP_NAME, APP_ORG
from bouquin.key_prompt import KeyPrompt
from bouquin.theme import Theme, ThemeManager, ThemeConfig
@pytest.fixture(scope="module")
def app():
a = QApplication.instance()
@ -18,6 +19,7 @@ def app():
a.setOrganizationName(APP_ORG)
return a
@pytest.fixture
def db(tmp_path):
cfg = DBConfig(path=tmp_path / "s.db", key="abc")
@ -25,6 +27,7 @@ def db(tmp_path):
assert m.connect()
return m
def test_theme_radio_initialisation_dark_and_light(app, db, monkeypatch, qtbot):
# Dark preselection
parent = _ParentWithThemes(app)
@ -44,6 +47,7 @@ def test_theme_radio_initialisation_dark_and_light(app, db, monkeypatch, qtbot):
dlg2._save()
assert dlg2.config.theme == Theme.LIGHT.value
def test_change_key_cancel_branches(app, db, monkeypatch, qtbot):
parent = _ParentWithThemes(app)
qtbot.addWidget(parent)
@ -56,17 +60,20 @@ def test_change_key_cancel_branches(app, db, monkeypatch, qtbot):
assert dlg.key == ""
# First OK, second cancelled -> early return at the second branch
state = {'calls': 0}
state = {"calls": 0}
def _exec(self):
state['calls'] += 1
return QDialog.Accepted if state['calls'] == 1 else QDialog.Rejected
monkeypatch.setattr(KeyPrompt, 'exec', _exec)
state["calls"] += 1
return QDialog.Accepted if state["calls"] == 1 else QDialog.Rejected
monkeypatch.setattr(KeyPrompt, "exec", _exec)
# Also monkeypatch to control key() values
monkeypatch.setattr(KeyPrompt, "key", lambda self: "new-secret")
dlg._change_key()
# Because the second prompt was rejected, key should remain unchanged
assert dlg.key == ""
def test_save_key_checkbox_cancel_restores_checkbox(app, db, monkeypatch, qtbot):
parent = _ParentWithThemes(app)
qtbot.addWidget(parent)
@ -92,7 +99,9 @@ def test_change_key_exception_path(app, db, monkeypatch, qtbot):
monkeypatch.setattr(KeyPrompt, "key", lambda self: "boom")
# Force DB rekey to raise to exercise the except-branch
monkeypatch.setattr(db, "rekey", lambda new_key: (_ for _ in ()).throw(RuntimeError("fail")))
monkeypatch.setattr(
db, "rekey", lambda new_key: (_ for _ in ()).throw(RuntimeError("fail"))
)
# Should not raise; error is handled internally
dlg._change_key()
@ -102,4 +111,3 @@ class _ParentWithThemes(QWidget):
def __init__(self, app):
super().__init__()
self.themes = ThemeManager(app, ThemeConfig())