73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from PySide6.QtCore import QSettings
|
|
from PySide6.QtWidgets import QApplication, QDialog
|
|
|
|
from bedit_gui.controllers.settings_controller import SettingsController
|
|
from bedit_gui.services.application_logging import get_logger
|
|
from bedit_gui.services.application_settings import ApplicationSettings
|
|
from bedit_gui.views.main_window import MainWindow
|
|
|
|
|
|
class FakeSettingsDialog:
|
|
def __init__(self, log_level: int) -> None:
|
|
self.initial_log_level = log_level
|
|
self.log_level = logging.ERROR
|
|
|
|
def exec(self) -> QDialog.DialogCode:
|
|
return QDialog.DialogCode.Accepted
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def qt_app() -> QApplication:
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_log_level_is_persisted(tmp_path: Path) -> None:
|
|
path = tmp_path / "settings.ini"
|
|
backend = QSettings(str(path), QSettings.Format.IniFormat)
|
|
settings = ApplicationSettings(backend)
|
|
|
|
settings.log_level = logging.DEBUG
|
|
backend.sync()
|
|
|
|
reloaded = ApplicationSettings(
|
|
QSettings(str(path), QSettings.Format.IniFormat)
|
|
)
|
|
assert reloaded.log_level == logging.DEBUG
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_settings_action_applies_log_level(
|
|
qt_app: QApplication,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
backend = QSettings(
|
|
str(tmp_path / "settings.ini"),
|
|
QSettings.Format.IniFormat,
|
|
)
|
|
settings = ApplicationSettings(backend)
|
|
window = MainWindow()
|
|
dialogs: list[FakeSettingsDialog] = []
|
|
|
|
def make_dialog(
|
|
log_level: int,
|
|
_window: MainWindow,
|
|
) -> FakeSettingsDialog:
|
|
dialog = FakeSettingsDialog(log_level)
|
|
dialogs.append(dialog)
|
|
return dialog
|
|
|
|
SettingsController(window, settings, make_dialog)
|
|
window.ui.actionSettings.trigger()
|
|
|
|
assert dialogs[0].initial_log_level == logging.INFO
|
|
assert settings.log_level == logging.ERROR
|
|
assert get_logger("tests").isEnabledFor(logging.ERROR)
|
|
assert not get_logger("tests").isEnabledFor(logging.WARNING)
|