231 lines
8.6 KiB
Python
231 lines
8.6 KiB
Python
import hashlib
|
|
import importlib
|
|
import json
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.bridge.protocol import load_apply_request
|
|
from dx_productivity.bridge.run_store import RunStore
|
|
from dx_productivity.domain.models import AttributeRecord, RunPaths
|
|
from dx_productivity.ui.property_change_controller import PropertyChangeController
|
|
from dx_productivity.ui.property_change_page import PropertyChangePage
|
|
from dx_productivity.ui.property_visibility_controller import PropertyVisibilityController
|
|
from dx_productivity.ui.property_visibility_page import PropertyVisibilityPage
|
|
|
|
|
|
def _controller(qtbot, tmp_path, snapshot, kind):
|
|
paths = RunPaths(
|
|
snapshot.run_id,
|
|
tmp_path,
|
|
tmp_path / "request.json",
|
|
tmp_path / "result.tsv",
|
|
tmp_path / "locate.tsv",
|
|
tmp_path / "apply-status.json",
|
|
)
|
|
page = PropertyChangePage() if kind == "change" else PropertyVisibilityPage()
|
|
qtbot.addWidget(page)
|
|
controller_type = PropertyChangeController if kind == "change" else PropertyVisibilityController
|
|
controller = controller_type(page, run_store=RunStore(tmp_path / "store"), run_paths=paths)
|
|
controller.can_apply_changed.connect(page.set_can_apply)
|
|
controller.load_snapshot(snapshot)
|
|
if kind == "change":
|
|
page.model.item(0, 2).setText("47K")
|
|
return controller, page, paths
|
|
|
|
|
|
def test_property_change_load_preserves_current_values_until_edited(
|
|
qtbot, component_factory, snapshot_factory
|
|
):
|
|
page = PropertyChangePage()
|
|
qtbot.addWidget(page)
|
|
controller = PropertyChangeController(page)
|
|
controller.can_apply_changed.connect(page.set_can_apply)
|
|
component = component_factory(attributes=(AttributeRecord("Value", "10K", True),))
|
|
|
|
controller.load_snapshot(snapshot_factory(components=(component,)))
|
|
|
|
assert page.model.item(0, 1).text() == "10K"
|
|
assert page.model.item(0, 2).text() == "10K"
|
|
assert not controller.can_apply
|
|
assert not page.apply_button.isEnabled()
|
|
page.model.item(0, 2).setText("")
|
|
assert controller.can_apply # Clearing requires an explicit edit.
|
|
|
|
|
|
def test_property_switch_loads_new_current_values_without_carrying_old_edits(
|
|
qtbot, component_factory, snapshot_factory
|
|
):
|
|
page = PropertyChangePage()
|
|
qtbot.addWidget(page)
|
|
controller = PropertyChangeController(page)
|
|
component = component_factory(attributes=(
|
|
AttributeRecord("Value", "10K", True),
|
|
AttributeRecord("Value1", "5%", False),
|
|
))
|
|
controller.load_snapshot(snapshot_factory(components=(component,)))
|
|
page.model.item(0, 2).setText("47K")
|
|
|
|
page.property_combo.setCurrentText("Value1")
|
|
|
|
assert page.model.item(0, 1).text() == "5%"
|
|
assert page.model.item(0, 2).text() == "5%"
|
|
assert not controller.can_apply
|
|
page.model.item(0, 2).setText("1%")
|
|
assert page.settings().overrides == (("c0001", "1%"),)
|
|
assert controller.can_apply
|
|
|
|
|
|
def test_custom_property_input_stays_available_after_switching_back(qtbot):
|
|
page = PropertyChangePage()
|
|
qtbot.addWidget(page)
|
|
page.property_combo.setCurrentText("自定义…")
|
|
page.custom_property_edit.setText("Tolerance")
|
|
page.property_combo.setCurrentText("Value")
|
|
|
|
page.property_combo.setCurrentText("自定义…")
|
|
|
|
assert not page.custom_property_edit.isHidden()
|
|
assert page.property_name() == "Tolerance"
|
|
|
|
|
|
@pytest.mark.parametrize("kind,operation,property_name,old,new", [
|
|
("change", "property", "Value", "10K", "47K"),
|
|
("visibility", "visibility", "Part Name", "1", "0"),
|
|
])
|
|
def test_property_apply_publishes_backup_metadata_and_expected_value_schedule(
|
|
qtbot, tmp_path, component_factory, snapshot_factory,
|
|
kind, operation, property_name, old, new
|
|
):
|
|
component = component_factory(attributes=(
|
|
AttributeRecord("Value", "10K", True),
|
|
AttributeRecord("Part Name", "RES", True),
|
|
))
|
|
controller, page, paths = _controller(
|
|
qtbot, tmp_path, snapshot_factory(components=(component,)), kind
|
|
)
|
|
paths.apply_status.write_text('{"stale": true}', encoding="utf-8")
|
|
|
|
result = controller.apply()
|
|
|
|
assert result.read_text(encoding="utf-8") == (
|
|
f"1\t{operation}\tc0001\t{property_name}\t{old}\t{new}\t{new}\n"
|
|
)
|
|
request = load_apply_request(paths.apply_request)
|
|
assert request.schedule_sha256 == hashlib.sha256(result.read_bytes()).hexdigest()
|
|
changes = json.loads(request.backup_path.read_text(encoding="utf-8"))["changes"]
|
|
assert len(changes) == 1
|
|
assert changes[0]["op"] == operation
|
|
assert changes[0]["property"] == property_name
|
|
assert changes[0]["old_value"] == old
|
|
assert changes[0]["new_value"] == new
|
|
assert not paths.apply_status.exists()
|
|
assert not page.apply_button.isEnabled()
|
|
with pytest.raises(RuntimeError, match="already in flight"):
|
|
controller.apply()
|
|
controller._status_timer.stop()
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["change", "visibility"])
|
|
def test_failed_property_transaction_can_be_retried(
|
|
qtbot, tmp_path, component_factory, snapshot_factory, kind
|
|
):
|
|
component = component_factory(attributes=(
|
|
AttributeRecord("Value", "10K", True),
|
|
AttributeRecord("Part Name", "RES", True),
|
|
))
|
|
controller, page, paths = _controller(
|
|
qtbot, tmp_path, snapshot_factory(components=(component,)), kind
|
|
)
|
|
controller.apply()
|
|
controller._apply_started_at = time.monotonic() - 3600
|
|
|
|
with qtbot.waitSignal(controller.apply_failed):
|
|
controller._poll_apply_status()
|
|
|
|
assert not controller._apply_in_flight
|
|
assert controller.can_apply
|
|
assert page.apply_button.isEnabled()
|
|
assert not controller._status_timer.isActive()
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["change", "visibility"])
|
|
def test_property_schedule_write_failure_does_not_lock_future_apply(
|
|
qtbot, tmp_path, component_factory, snapshot_factory, monkeypatch, kind
|
|
):
|
|
component = component_factory(attributes=(
|
|
AttributeRecord("Value", "10K", True),
|
|
AttributeRecord("Part Name", "RES", True),
|
|
))
|
|
controller, page, paths = _controller(
|
|
qtbot, tmp_path, snapshot_factory(components=(component,)), kind
|
|
)
|
|
module_name = "property_change_controller" if kind == "change" else "property_visibility_controller"
|
|
module = importlib.import_module(f"dx_productivity.ui.{module_name}")
|
|
|
|
def fail_write(*args):
|
|
raise OSError("mailbox is read-only")
|
|
|
|
monkeypatch.setattr(module, "write_schedule", fail_write)
|
|
with pytest.raises(OSError, match="read-only"):
|
|
controller.apply()
|
|
|
|
assert not controller._apply_in_flight
|
|
assert controller.can_apply
|
|
assert page.apply_button.isEnabled()
|
|
assert not controller._status_timer.isActive()
|
|
assert not paths.result.exists()
|
|
|
|
|
|
def test_visibility_preview_distinguishes_missing_from_hidden(
|
|
qtbot, component_factory, snapshot_factory
|
|
):
|
|
page = PropertyVisibilityPage()
|
|
qtbot.addWidget(page)
|
|
controller = PropertyVisibilityController(page)
|
|
|
|
controller.load_snapshot(snapshot_factory(components=(component_factory(attributes=()),)))
|
|
|
|
assert page.model.item(0, 2).text() == "属性缺失"
|
|
assert not controller.can_apply
|
|
|
|
|
|
@pytest.mark.parametrize("property_name", ["Value", "Value1", "Part Number", "Tolerance"])
|
|
def test_every_property_selector_uses_its_attribute_value_and_schedule(
|
|
qtbot, tmp_path, component_factory, snapshot_factory, property_name
|
|
):
|
|
component = component_factory(attributes=(AttributeRecord(property_name, "original", True),))
|
|
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory(components=(component,)), "change")
|
|
if property_name == "Tolerance":
|
|
page.property_combo.setCurrentText("自定义…")
|
|
page.custom_property_edit.setText(property_name)
|
|
else:
|
|
page.property_combo.setCurrentText(property_name)
|
|
assert page.model.item(0, 1).text() == "original"
|
|
page.model.item(0, 2).setText("new")
|
|
|
|
controller.apply()
|
|
|
|
assert paths.result.read_text(encoding="utf-8") == (
|
|
f"1\tproperty\tc0001\t{property_name}\toriginal\tnew\tnew\n"
|
|
)
|
|
controller._status_timer.stop()
|
|
|
|
|
|
@pytest.mark.parametrize("property_name", ["Part Name", "Part Label"])
|
|
def test_each_visibility_property_can_be_shown_again(
|
|
qtbot, tmp_path, component_factory, snapshot_factory, property_name
|
|
):
|
|
component = component_factory(attributes=(AttributeRecord(property_name, "RES", False),))
|
|
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory(components=(component,)), "visibility")
|
|
page.property_combo.setCurrentText(property_name)
|
|
page.show_button.click()
|
|
assert controller.can_apply
|
|
|
|
controller.apply()
|
|
|
|
assert paths.result.read_text(encoding="utf-8") == (
|
|
f"1\tvisibility\tc0001\t{property_name}\t0\t1\t1\n"
|
|
)
|
|
controller._status_timer.stop()
|