80 lines
3.9 KiB
Python
80 lines
3.9 KiB
Python
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_unique_scoped_reference_ignores_stale_bom_uid(tmp_path):
|
|
from dx_productivity.trial_task_center.bom_plan import BomMatcher, BomRecord
|
|
from dx_productivity.trial_task_center.target_plan import TargetPlanRow
|
|
target = ComponentTarget(tmp_path / 'board.prj', 'MAIN', '1', 'current', 'R1', 'OLD', '10nF')
|
|
matcher = BomMatcher((TargetPlanRow(target),))
|
|
record = BomRecord('', 'OLD', 'NEW', ('R1',), 'MAIN', '1', 'stale', True, '', True)
|
|
matched = matcher.match_record(record)
|
|
assert len(matched) == 1 and next(iter(matched.values())).target is target
|
|
|
|
|
|
def test_uid_can_only_disambiguate_multiple_reference_candidates(tmp_path):
|
|
from dx_productivity.trial_task_center.bom_plan import BomMatcher, BomRecord
|
|
from dx_productivity.trial_task_center.target_plan import TargetPlanRow
|
|
first = ComponentTarget(tmp_path / 'board.prj', 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF')
|
|
second = ComponentTarget(tmp_path / 'board.prj', 'MAIN', '2', 'u2', 'R1', 'OLD', '10nF')
|
|
matcher = BomMatcher((TargetPlanRow(first), TargetPlanRow(second)))
|
|
record = BomRecord('', 'OLD', 'NEW', ('R1',), '', '', 'u2', True, '', True)
|
|
assert next(iter(matcher.match_record(record).values())).target is second
|
|
|
|
|
|
def test_already_target_does_not_accept_duplicate_nc_from_leading_marker(tmp_path):
|
|
from dx_productivity.trial_task_center.bom_plan import BomMatcher, BomRecord
|
|
from dx_productivity.trial_task_center.target_plan import TargetPlanRow
|
|
target = ComponentTarget(tmp_path / 'board.prj', 'MAIN', '1', 'u1', 'R1', 'NEW', 'NC/10nF/NC')
|
|
matcher = BomMatcher((TargetPlanRow(target, False, 'NEW'),))
|
|
record = BomRecord('', 'OLD', 'NEW', ('R1',), '', '', '', True, 'NC/10nF', True, 'custom')
|
|
with pytest.raises(TaskCenterError, match='Value'):
|
|
matcher.match_record(record)
|
|
|
|
|
|
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()
|