from pathlib import Path import pytest from openpyxl import Workbook from dx_productivity.trial_task_center.bom_plan import BOM_HEADERS, read_bom_plan, write_bom_template from dx_productivity.trial_task_center.errors import TaskCenterError from dx_productivity.trial_task_center.store import TaskStore from dx_productivity.trial_task_center.target_plan import ComponentTarget def setup_store(tmp_path): store = TaskStore(tmp_path / 'tasks.db') task = store.create_task('board') path = tmp_path / 'board.prj' store.sync_projects(task.task_id, tmp_path, (path,)) targets = tuple(ComponentTarget(path, 'MAIN', '1', uid, ref, 'OLD', value) for uid, ref, value in (('u1', 'R1', '10nF'), ('u2', 'R2', '22nF'), ('u3', 'C1', '1uF'))) store.record_target_inventory(task.task_id, targets) return store, task, targets def test_bom_template_expands_one_to_many_refdes_and_preserves_unlisted_targets(tmp_path): store, task, targets = setup_store(tmp_path) path = tmp_path / 'bom.xlsx' write_bom_template(path, ( {'old_part_number': 'OLD', 'new_part_number': 'NEW-A', 'refdes': 'R1, R2', 'enabled': '是', 'sync_nc': '否', 'custom_value': '10nF'}, )) rows = read_bom_plan(store, task.task_id, path) by_ref = {row.target.refdes: row for row in rows} assert by_ref['R1'].enabled and by_ref['R2'].enabled assert not by_ref['R1'].sync_nc assert not by_ref['C1'].enabled store.close() def test_bom_rejects_unknown_or_ambiguous_refdes_and_formulas(tmp_path): store, task, _ = setup_store(tmp_path) path = tmp_path / 'bom.xlsx' write_bom_template(path, ({'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': 'R9'},)) with pytest.raises(TaskCenterError, match='R9'): read_bom_plan(store, task.task_id, path) workbook = Workbook(); sheet = workbook.active; sheet.append(BOM_HEADERS) sheet.append(['OLD', '=A2', 'R1', '', '', '', True, '', True]) workbook.save(path) with pytest.raises(TaskCenterError, match='公式'): read_bom_plan(store, task.task_id, path) store.close()