Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pyqt6-webengine = "^6.5.0"
pytest = "^7.1.3"
pytest-cov = "^4.0.0"
invoke = "^2.0.0"
pytest-qt = "^4.2.0"


[tool.poetry.group.dev.dependencies]
Expand Down Expand Up @@ -72,7 +73,10 @@ testpaths = [
markers = [
"crud: marks CRUD integration tests using an in-memory database (deselect with '-m \"not crud\"')",
"api",
"frontend",
]
qt_qapp_name = "Dfacto-tests"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
1 change: 1 addition & 0 deletions src/dfacto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def _(_text: str) -> str:

IS_FROZEN: Final[bool] = getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS")
DEV_MODE: Final[bool] = os.environ.get("DFACTO_DEV", "0") != "0"
TEST_MODE: Final[bool] = os.environ.get("DFACTO_TEST", "0") != "0"


def except_hook(exc_type, exc_value, _exc_traceback): # type: ignore[no-untyped-def]
Expand Down
2 changes: 0 additions & 2 deletions src/dfacto/frontend/globals_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,12 @@ def is_valid(self) -> bool:

return due_delta >= 0 and penalty >= 0.0 and discount >= 0.0

@QtCore.pyqtSlot(str)
def check_spn_text(self, spn: QtWidgets.QSpinBox, text: str) -> None:
if text == spn.suffix():
spn.setValue(0)

self._enable_buttons(self.has_changed, self.is_valid)

@QtCore.pyqtSlot(str)
def check_spn_value(self, _value: Union[int, float]) -> None:
self._enable_buttons(self.has_changed, self.is_valid)

Expand Down
22 changes: 12 additions & 10 deletions src/dfacto/frontend/settingsview.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,23 @@ def __init__(self, *args, parent=None, **kwargs):
self.scale_spn.valueChanged.connect(self.select_qt_scale_factor_from_float)
self.scale_sld.valueChanged.connect(self.select_qt_scale_factor_from_int)

reset_btn = QtWidgets.QPushButton(_("Reset to defaults"))
reset_btn.clicked.connect(self.reset_to_defaults)
self.reset_btn = QtWidgets.QPushButton(_("Reset to defaults"))
self.reset_btn.clicked.connect(self.reset_to_defaults)

button_box = QtWidgets.QDialogButtonBox(
self.button_box = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.StandardButton.Ok
| QtWidgets.QDialogButtonBox.StandardButton.Cancel,
QtCore.Qt.Orientation.Horizontal,
self,
)
button_box.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setText(_("OK"))
button_box.button(QtWidgets.QDialogButtonBox.StandardButton.Cancel).setText(
_("Cancel")
self.button_box.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setText(
_("OK")
)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
self.button_box.button(
QtWidgets.QDialogButtonBox.StandardButton.Cancel
).setText(_("Cancel"))
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)

edit_layout = QtWidgets.QGridLayout()
edit_layout.addWidget(self.default_dir_selector, 0, 0, 1, 3)
Expand All @@ -153,8 +155,8 @@ def __init__(self, *args, parent=None, **kwargs):
QtWidgets.QSizePolicy.Policy.Expanding,
)
edit_layout.addItem(vertical_spacer, 7, 0, 1, 1)
edit_layout.addWidget(reset_btn, 8, 0, 1, 1)
edit_layout.addWidget(button_box, 9, 1, 1, 2)
edit_layout.addWidget(self.reset_btn, 8, 0, 1, 1)
edit_layout.addWidget(self.button_box, 9, 1, 1, 2)

self.setLayout(edit_layout)

Expand Down
23 changes: 13 additions & 10 deletions src/dfacto/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pathlib import Path
from typing import TYPE_CHECKING

from dfacto import DEV_MODE, IS_FROZEN
from dfacto import DEV_MODE, IS_FROZEN, TEST_MODE
from dfacto.backend import naming
from dfacto.util.settings import Setting, Settings, get_app_dirs

Expand Down Expand Up @@ -60,7 +60,11 @@ class DfactoSettings(Settings):
resources: Path
templates: Path

_DEFAULT_LOGLEVEL = "INFO"
DEFAULT_COMPANY_FOLDER = "C:/Users/T0018179/MyApp/Git/home/portable/DFacto"
# DEFAULT_COMPANY_FOLDER = "F:/Users/Documents/Dfacto"
DEFAULT_LOG_LEVEL = "INFO"
DEFAULT_QT_SCALE_FACTOR = "1.0"
DEFAULT_FONT_SIZE = 2

last_profile: Setting = Setting(default_value=None)
profiles: Setting = Setting(default_value=None)
Expand All @@ -70,15 +74,12 @@ class DfactoSettings(Settings):
lastDestinationNamingTemplate: Setting = Setting(
default_value=naming.NamingTemplates.default_destination_naming_template
)
# default_company_folder = Setting(
# default_value="C:/Users/T0018179/MyApp/Git/home/portable/DFacto"
# )
default_company_folder = Setting(default_value="F:/Users/Documents/Dfacto")
log_level: Setting = Setting(default_value=_DEFAULT_LOGLEVEL)
default_company_folder = Setting(default_value=DEFAULT_COMPANY_FOLDER)
log_level: Setting = Setting(default_value=DEFAULT_LOG_LEVEL)
window_position: Setting = Setting(default_value=(0, 0))
window_size: Setting = Setting(default_value=(1600, 800))
qt_scale_factor: Setting = Setting(default_value="1.0")
font_size: Setting = Setting(default_value=2)
qt_scale_factor: Setting = Setting(default_value=DEFAULT_QT_SCALE_FACTOR)
font_size: Setting = Setting(default_value=DEFAULT_FONT_SIZE)
# locale: Setting = Setting(default_value="en_US")
# locale: Setting = Setting(default_value="fr_FR")
locale: Setting = Setting(default_value=None)
Expand Down Expand Up @@ -116,7 +117,9 @@ def reset_to_defaults(self) -> None:
setattr(self, setting, default_value)


if DEV_MODE:
if TEST_MODE:
dfacto_settings = DfactoSettings("dfacto_test")
elif DEV_MODE:
dfacto_settings = DfactoSettings("dfacto_dev")
else:
dfacto_settings = DfactoSettings("dfacto")
5 changes: 5 additions & 0 deletions tests/frontend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2023 Eric Lemoine
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
116 changes: 116 additions & 0 deletions tests/frontend/test_globals_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Copyright (c) 2022, Eric Lemoine
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from decimal import Decimal

import PyQt6.QtCore as QtCore
import PyQt6.QtWidgets as QtWidgets
import pytest

from dfacto.backend import schemas
from dfacto.frontend.globals_editor import GlobalsEditor

pytestmark = pytest.mark.frontend


@pytest.fixture(scope="session", autouse=True)
def install_tr():
import gettext
from pathlib import Path

from dfacto.settings import dfacto_settings

locales_dir = Path(__file__).resolve().parent.parent.parent / "locales"
translations = gettext.translation(
"dfacto",
locales_dir,
languages=[dfacto_settings.locale],
fallback=True,
)
translations.install()


DUE_DELTA = 15
PENALTY_RATE_D = Decimal("12.0")
PENALTY_RATE_F = 12.0
DISCOUNT_RATE_D = Decimal("2.5")
DISCOUNT_RATE_F = 2.5
FAKE_GLOBALS = schemas.Globals(
id=1,
due_delta=DUE_DELTA,
penalty_rate=PENALTY_RATE_D,
discount_rate=DISCOUNT_RATE_D,
is_current=True,
)


def test_init(qtbot):
editor = GlobalsEditor(FAKE_GLOBALS)
# editor.show()
# qtbot.stop()
qtbot.addWidget(editor)

assert editor.due_spn.value() == DUE_DELTA
assert editor.penalty_spn.value() == PENALTY_RATE_F
assert editor.discount_spn.value() == DISCOUNT_RATE_F
assert not editor.button_box.button(
QtWidgets.QDialogButtonBox.StandardButton.Ok
).isEnabled()
assert not editor.reset_btn.isEnabled()


@pytest.mark.parametrize(
"spn, old, new, attr, res",
(
("due_spn", DUE_DELTA, 45, "due_delta", 45),
("penalty_spn", PENALTY_RATE_F, 20.0, "penalty_rate", Decimal("20.0")),
("discount_spn", DISCOUNT_RATE_F, 3.0, "discount_rate", Decimal("3.0")),
),
)
def test_change(spn, old, new, attr, res, qtbot):
editor = GlobalsEditor(FAKE_GLOBALS)
qtbot.addWidget(editor)

spn_box = getattr(editor, spn)
spn_box.setValue(new)

result = getattr(editor.globals, attr)
assert result == res
assert editor.button_box.button(
QtWidgets.QDialogButtonBox.StandardButton.Ok
).isEnabled()
assert editor.reset_btn.isEnabled()

spn_box.setValue(old)

assert not editor.button_box.button(
QtWidgets.QDialogButtonBox.StandardButton.Ok
).isEnabled()
assert not editor.reset_btn.isEnabled()


@pytest.mark.parametrize("spn", ("due_spn", "penalty_spn", "discount_spn"))
def test_clear(spn, qtbot):
editor = GlobalsEditor(FAKE_GLOBALS)
qtbot.addWidget(editor)

spn_box = getattr(editor, spn)
suffix = spn_box.suffix()
spn_box.selectAll()

def check_suffix(txt: str) -> bool:
return txt == suffix

with qtbot.wait_signal(
spn_box.lineEdit().textEdited, timeout=1000, check_params_cb=check_suffix
):
qtbot.keyClick(spn_box, QtCore.Qt.Key.Key_Delete)

assert spn_box.value() == 0
assert editor.button_box.button(
QtWidgets.QDialogButtonBox.StandardButton.Ok
).isEnabled()
assert editor.reset_btn.isEnabled()
Loading