Files
yiyezhqiu-refactored/tests/ui/test_bom_controller.py
T

155 lines
6.3 KiB
Python

import json
import pytest
from dx_productivity.domain.models import AttributeRecord, RunPaths
from dx_productivity.ui.bom_controller import BomController
from dx_productivity.ui.bom_page import BomPage
def _controller(qtbot, tmp_path, snapshot):
page = BomPage()
qtbot.addWidget(page)
paths = RunPaths(snapshot.run_id, tmp_path, tmp_path / "request.json",
tmp_path / "result.tsv", tmp_path / "locate.tsv", tmp_path / "apply-status.json")
template = tmp_path / "BOM模板.xls"
template.write_bytes(b"template marker; offline tests do not invoke Excel")
controller = BomController(page, run_paths=paths, template_path=template, design_name="old.prj")
controller.load_snapshot(snapshot)
return controller, page, paths
def test_bom_preview_controls_recompute_scopes_description_and_nc_filter(
qtbot, tmp_path, component_factory, snapshot_factory
):
first = component_factory(refdes="R1", attributes=(
AttributeRecord("Value", "10K", True), AttributeRecord("Part Label", "RES", True),
))
other = component_factory(bridge_id="c2", stable_uid="uid-c2", full_path="ROOT\\C2",
logical_group_key="uid-c2", refdes="R2", selected=False, current_sheet=False,
sheet_number=2, attributes=(AttributeRecord("Value", "NC", True),))
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory(components=(first, other)))
assert len(controller._result.rows) == 2
page.selected_button.click()
assert [row.refdes for row in controller._result.rows] == ["R1"]
page.description_combo.setCurrentIndex(1)
assert controller._result.rows[0].description == "RES"
page.current_sheet_button.click()
assert [row.refdes for row in controller._result.rows] == ["R1"]
page.full_button.click()
page.description_combo.setCurrentIndex(0)
page.nc_checkbox.setChecked(True)
assert [row.refdes for row in controller._result.rows] == ["R1"]
assert controller._result.excluded_count == 1
assert page.export_button.isEnabled()
def test_bom_export_clears_old_status_and_disables_duplicate_requests(
qtbot, tmp_path, snapshot_factory
):
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory())
status = tmp_path / "bom-export-status.json"
status.write_text('{"state":"ok","output_path":"old.xls"}', encoding="utf-8")
controller.export_to(str(tmp_path / "new.xls"))
assert not status.exists()
assert controller._export_in_flight
assert not page.export_button.isEnabled()
payload = json.loads((tmp_path / "bom-export.json").read_text(encoding="utf-8"))
assert payload["output_path"] == str(tmp_path / "new.xls")
assert payload["design_name"] == "test.prj"
assert payload["records"] == "S|1\nR|1|R?|PART|10K|"
controller.refresh_preview()
assert not page.export_button.isEnabled()
controller.export_to(str(tmp_path / "ignored.xls"))
assert json.loads((tmp_path / "bom-export.json").read_text(encoding="utf-8"))["output_path"] == str(tmp_path / "new.xls")
controller._status_timer.stop()
def test_bom_export_converts_qt_dialog_path_for_native_excel(
qtbot, tmp_path, snapshot_factory
):
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory())
output = tmp_path / 'BOM导出.xls'
# QFileDialog returns forward slashes on Windows; Excel SaveAs requires
# the platform path separators used by the native COM host.
controller.export_to(output.as_posix())
payload = json.loads((tmp_path / 'bom-export.json').read_text(encoding='utf-8'))
assert payload['output_path'] == str(output)
assert controller._requested_output_path == str(output)
controller._status_timer.stop()
def test_bom_mailbox_write_failure_can_be_retried(
qtbot, tmp_path, snapshot_factory, monkeypatch
):
from dx_productivity.ui import bom_controller as module
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory())
def fail_write(*args):
raise OSError("mailbox is read-only")
monkeypatch.setattr(module, "atomic_write_text", fail_write)
with pytest.raises(OSError, match="read-only"):
controller.export_to("new.xls")
assert not controller._export_in_flight
assert page.export_button.isEnabled()
assert not controller._status_timer.isActive()
@pytest.mark.parametrize("status", ["{not-json", "[]", '{"state":"failed","error":"Excel unavailable"}'])
def test_bom_export_failure_reports_error_and_restores_button(
qtbot, tmp_path, snapshot_factory, status
):
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory())
controller.export_to("new.xls")
(tmp_path / "bom-export-status.json").write_text(status, encoding="utf-8")
with qtbot.waitSignal(controller.export_failed) as signal:
controller._poll_export_status()
assert signal.args[0]
assert not controller._export_in_flight
assert page.export_button.isEnabled()
def test_bom_success_publishes_host_output(qtbot, tmp_path, snapshot_factory):
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory())
controller.export_to("new.xls")
(tmp_path / "bom-export-status.json").write_text('{"state":"ok","output_path":"new.xls"}', encoding="utf-8")
with qtbot.waitSignal(controller.export_completed) as signal:
controller._poll_export_status()
assert signal.args == ["new.xls"]
assert not controller._export_in_flight
assert page.export_button.isEnabled()
def test_bom_success_without_host_path_uses_requested_output(qtbot, tmp_path, snapshot_factory):
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory())
requested = str(tmp_path / "BOM导出.xls")
controller.export_to(requested)
(tmp_path / "bom-export-status.json").write_text('{"state":"ok"}', encoding="utf-8")
with qtbot.waitSignal(controller.export_completed) as signal:
controller._poll_export_status()
assert signal.args == [requested]
def test_bom_records_keep_multiline_attributes_in_one_record(
qtbot, tmp_path, component_factory, snapshot_factory
):
component = component_factory(refdes="R1", attributes=(AttributeRecord("Value", "a\nb\r\nc|d", True),))
controller, page, paths = _controller(qtbot, tmp_path, snapshot_factory(components=(component,)))
records = controller._build_payload("new.xls")["records"]
assert records.splitlines() == ["S|1", "R|1|R1|PART|a b c d|"]