otko/tests/gui/test_update_element_fields.py
smillmorel ba783718d4 chore: adopt remaining local development state
Catch-all for the intermixed residue of the unpushed otko-development
work ported into this tree: combinations/console-dock/quick-guide wiring
across commands, core, services, views and tests; repo-wide ruff-format
normalization; README/CONTRIBUTING updates; and the toolbar default
(both toolbars now open in the top area, quick guide text updated).

Splitting this further would require hunk-level surgery with low
confidence; the preceding commits in this branch isolate the
self-contained features.
2026-09-16 12:03:22 -04:00

99 lines
2.6 KiB
Python

"""Tests for UpdateElementFieldsCommand — inline scalar edits."""
from __future__ import annotations
import pytest
pytest.importorskip("PySide6")
from otko.commands import ( # noqa: E402
AddElementsCommand,
AddMaterialsCommand,
AddNodesCommand,
UpdateElementFieldsCommand,
)
from otko.core import ( # noqa: E402
ElasticUniaxial,
Node,
TrussElement,
)
from otko.viewmodels import ProjectViewModel # noqa: E402
def _vm_with_truss() -> ProjectViewModel:
vm = ProjectViewModel()
vm.new_project()
vm.apply_command(
AddNodesCommand(
vm,
[
Node(id=1, coords=(0, 0, 0)),
Node(id=2, coords=(3, 0, 0)),
],
)
)
vm.apply_command(
AddMaterialsCommand(
vm,
[
ElasticUniaxial(id=1, name="Steel", E=200e9),
],
)
)
vm.apply_command(
AddElementsCommand(
vm,
[
TrussElement(id=1, nodes=(1, 2), area=0.001, material_id=1),
],
)
)
return vm
@pytest.mark.gui
def test_update_area_on_truss_element(qtbot) -> None: # type: ignore[no-untyped-def]
vm = _vm_with_truss()
vm.apply_command(UpdateElementFieldsCommand(vm, 1, {"area": 0.00645}))
assert vm.project.element(1).area == pytest.approx(0.00645)
@pytest.mark.gui
def test_update_area_is_undoable(qtbot) -> None: # type: ignore[no-untyped-def]
vm = _vm_with_truss()
vm.apply_command(UpdateElementFieldsCommand(vm, 1, {"area": 0.005}))
vm.undo_stack.undo()
assert vm.project.element(1).area == pytest.approx(0.001)
vm.undo_stack.redo()
assert vm.project.element(1).area == pytest.approx(0.005)
@pytest.mark.gui
def test_unknown_field_is_silently_ignored(qtbot) -> None: # type: ignore[no-untyped-def]
"""Fields not in the element's model are filtered out — no exception."""
vm = _vm_with_truss()
vm.apply_command(
UpdateElementFieldsCommand(
vm,
1,
{"section_id": 42}, # TrussElement has no section_id
)
)
# The element is unchanged.
assert vm.project.element(1).area == pytest.approx(0.001)
assert vm.project.element(1).material_id == 1
@pytest.mark.gui
def test_update_multiple_fields_at_once(qtbot) -> None: # type: ignore[no-untyped-def]
vm = _vm_with_truss()
vm.apply_command(
UpdateElementFieldsCommand(
vm,
1,
{"area": 0.003, "rho": 7850.0},
)
)
el = vm.project.element(1)
assert el.area == pytest.approx(0.003)
assert el.rho == pytest.approx(7850.0)