Merge pull request 'fix: BOM 导入提示忽略列及近义表头(#33 D5)' (#38) from codex/issue33-d5 into main
Core regression / Python core, intervention UI and release configuration (push) Successful in 6m4s
Core regression / JavaScript syntax (push) Successful in 7s

This commit was merged in pull request #38.
This commit is contained in:
abaishu0730 committed 2026-11-25 11:00:58 +08:00
commit 22442e03ae
8 files changed
+191 -23

No files matched your search

+99
View File
@@ -0,0 +1,99 @@
import json
import os
from pathlib import Path
import pytest
from openpyxl import load_workbook
from PySide6.QtWidgets import QFileDialog
from test_bom_batch import batch, records_for
from dx_productivity.trial_task_center.bom_plan import write_bom_template
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
@pytest.mark.parametrize('case', ['extra', 'near', 'missing', 'header_formula', 'row_formula', 'original'])
def test_bom_header_feedback_through_workspace(case, batch, tmp_path, qtbot, monkeypatch):
service, _, tasks, targets, inventory_calls = batch
task = tasks[0]
record = records_for(tasks, targets)[0]
source = write_bom_template(tmp_path / f'{case}.xlsx', [record])
workbook = load_workbook(source)
sheet = workbook.active
if case == 'extra':
sheet['L1'], sheet['L2'] = '供应商', 'Example supplier'
sheet['M1'], sheet['M2'] = '备注', 'Example note'
elif case == 'near':
sheet['D1'] = '新料号编码'
elif case == 'missing':
sheet['D1'] = None
elif case == 'header_formula':
sheet['L1'] = '=1+1'
elif case == 'row_formula':
sheet['L1'], sheet['L2'] = '备注', '=1+1'
workbook.save(source)
workbook.close()
assert_workspace_import(case, source, task, service, inventory_calls, qtbot, monkeypatch)
def test_field_extra_column_through_workspace(batch, tmp_path, qtbot, monkeypatch):
field = os.environ.get('DX_ISSUE33_D5_FIELD')
if not field:
pytest.skip('set DX_ISSUE33_D5_FIELD to replay the field attachment')
service, _, tasks, targets, inventory_calls = batch
source = tmp_path / Path(field).name
workbook = load_workbook(field)
sheet = workbook.active
# Rebind to the local fixture project. Keep the original Excel header and BOM cells.
sheet['A2'] = tasks[0].name
sheet['B2'] = str(targets[tasks[0].task_id][0].project_path)
workbook.save(source)
workbook.close()
assert_workspace_import('field', source, tasks[0], service, inventory_calls, qtbot, monkeypatch)
def assert_workspace_import(case, source, task, service, inventory_calls, qtbot, monkeypatch):
window = TrialTaskCenterWindow(service)
window.auto_intervention_check.setChecked(False)
qtbot.addWidget(window)
window.open_task(task.task_id)
panel = window.plan_panel
qtbot.waitUntil(panel.import_bom_button.isEnabled, timeout=5000)
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(source)], 'Excel (*.xlsx)'))
panel.import_bom_button.click()
qtbot.waitUntil(lambda: not window._workers, timeout=8000)
status = window.run_panel.operator_band.text()
print(status)
pending = service.pending_bom(task.task_id)
assert not inventory_calls
if case in {'extra', 'field', 'original', 'near', 'missing', 'header_formula'}:
panel.report_toggle.setChecked(True)
assert panel.report_dialog is not None
details = panel.report_dialog.view.toPlainText()
print(details)
if case in {'extra', 'field', 'original'}:
assert len(pending) == 1
assert panel.last_bom_report.failed_rows == 0 and panel.last_bom_report.deferred
if case == 'extra':
assert '失败 0 行' in status and '警告 2 项' in status
assert all(text in details for text in ('已忽略', '供应商', '备注', source.name))
assert pending[0].record.new == 'NEW-0'
events = (service._logging.root / 'tasks' / task.task_id / 'events.jsonl').read_text(encoding='utf-8')
entries = [json.loads(line) for line in events.splitlines()]
entry = next(event for event in entries if event['operation'] == 'import_bom_drafts')
assert tuple(entry['details']['warnings']) == panel.last_bom_report.warnings
elif case == 'field':
assert '警告 1 项' in status and 'extra_column' in details
else:
assert '警告' not in status and '已忽略' not in details
else:
assert not pending
if case == 'near':
assert '缺少必需列「新料号」' in status
assert '「新料号编码」' in details and '改为「新料号」' in details
elif case == 'missing':
assert '缺少必需列「新料号」' in status and '新料号' in details
elif case == 'header_formula':
assert 'BOM 表头不接受公式' in status and 'BOM 表头不接受公式' in details
else:
assert '第 2 行不接受公式' in status
window.close()
@@ -9,6 +9,8 @@ from test_bom_batch import batch
def test_real_supervised_sources_roundtrip_origin_metadata_and_preview(batch, tmp_path):
from openpyxl import load_workbook
from dx_productivity.trial_task_center.bom_plan import write_bom_template
service, store, tasks, targets, calls = batch
for task in tasks:
@@ -17,9 +19,14 @@ def test_real_supervised_sources_roundtrip_origin_metadata_and_preview(batch, tm
'project_name': task.name, 'project_path': str(targets[task.task_id][0].project_path),
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': 'R1', 'value_mode': 'part_number',
},)) for index, task in enumerate(tasks))
workbook = load_workbook(sources[0])
workbook.active['M1'] = '备注'
workbook.save(sources[0])
workbook.close()
service._supervised = True
report = service.preview_bom_sources(tasks[0].task_id, sources)
assert report.passed_rows == 2 and report.preview is not None
assert report.warnings == ('source-0.xlsx:已忽略未知列「备注」',)
assert tuple(row.source_file for row in report.rows) == tuple(str(path.resolve()) for path in sources)
assert all(row.source_row == 2 for row in report.rows)
assert report.source_map_path.is_file()