Files
dxdesigner-part-replacement/tests/v1_1/test_target_execution.py
T

147 lines
6.4 KiB
Python

from datetime import datetime, timezone
from pathlib import Path
import pytest
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.models import ProjectRecord, ProjectStatus
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer, _queue_matches_inputs, _read_commands
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow, compile_target_rules
from dx_productivity.trial_task_center.native_helper import check_context, verify_unselected_snapshot
from dx_productivity.trial_task_center.recovery import _recovery_command
def setup_queue(tmp_path):
script = tmp_path / 'QuickPartReplace.vbs'
script.write_text("' test script\n", encoding='ascii')
project_path = tmp_path / 'board.prj'
project_path.touch()
project = ProjectRecord('p', 't', 'board', project_path, tmp_path, True, True,
ProjectStatus.PENDING, 0)
rows = (
TargetPlanRow(ComponentTarget(project_path, 'MAIN', '1', 'u1', 'R1', 'OLD', 'A'), True, 'NEW-A'),
TargetPlanRow(ComponentTarget(project_path, 'MAIN', '1', 'u2', 'R2', 'OLD', 'B')),
TargetPlanRow(ComponentTarget(project_path, 'MAIN', '1', 'u3', 'R3', 'OLD', 'C'), True, 'NEW-B'),
)
rules = compile_target_rules(rows)
preparer = QuickRunPreparer(tmp_path / 'data', script_source=script,
run_token_factory=lambda: 'run_abc')
return preparer, project, rules
def test_target_queue_contains_only_selected_uid_refdes_pairs(tmp_path):
preparer, project, rules = setup_queue(tmp_path)
handoff = preparer.prepare('t', (project,), rules,
sheet_targets={str(project.path).casefold(): (('MAIN', '1'),)})
lines = handoff.command_path.read_text(encoding='utf-8').splitlines()
header = lines[0].split('\t')
values = [dict(zip(header, line.split('\t'))) for line in lines[1:]]
assert len(values) == 2
assert [value['target_scope'] for value in values] == ['targets', 'targets']
assert [value['target_selectors'] for value in values] == ['u1%001FR1%001FA', 'u3%001FR3%001FC']
assert _queue_matches_inputs(handoff.command_path, _read_commands(handoff.command_path),
(project,), rules)
changed = compile_target_rules((TargetPlanRow(rules[0].targets[0], True, 'OTHER'),))
assert not _queue_matches_inputs(handoff.command_path, _read_commands(handoff.command_path),
(project,), changed)
def test_target_queue_rejects_missing_scanned_sheet(tmp_path):
preparer, project, rules = setup_queue(tmp_path)
with pytest.raises(TaskCenterError):
preparer.prepare('t', (project,), rules,
sheet_targets={str(project.path).casefold(): (('OTHER', '1'),)})
def test_recovery_selects_saved_unverified_job_before_next_pending_job():
commands = (('t', 'a', 'board.prj', 'OLD', 'NEW'),
('t', 'b', 'board.prj', 'OTHER', 'NEXT'))
statuses = (('a', 'batch_saved_unverified', '', 'readback failed'),)
assert _recovery_command(commands, statuses) == commands[0]
def test_recovered_unverified_job_no_longer_blocks_queue(tmp_path):
script = tmp_path / 'QuickPartReplace.vbs'
script.write_text("' fixture\n", encoding='ascii')
preparer = QuickRunPreparer(tmp_path / 'data', script_source=script)
root = tmp_path / 'data' / 'quick-run'
root.mkdir(parents=True)
(root / 'command.tsv').write_text(
'task_id\tjob_id\tproject_path\told_part\tnew_part\n'
't\ta\tboard.prj\tOLD\tNEW\n', encoding='utf-8')
(root / 'status.tsv').write_text(
'job_id\tstate\tcode\tmessage\n'
'a\tbatch_saved_unverified\t\treadback failed\n'
'a\trestored_ok\t\trecovered\n', encoding='utf-8')
assert preparer.has_interrupted('t') is False
def test_native_context_rejects_missing_or_changed_target_without_no_match():
class Component:
def __init__(self, uid, refdes, part, value):
self.UID, self.Refdes = uid, refdes
self.attributes = {'Part Number': part, 'Value': value}
self.Selected = False
self._oleobj_ = self
def FindAttribute(self, name):
value = self.attributes.get(name)
return None if value is None else type('Attr', (), {'InstanceValue': value, 'Value': value})()
class View:
def __init__(self, components):
self.components = components
self.Block = type('Block', (), {'SheetNum': '1'})()
def GetName(self, index):
return 'MAIN'
def Query(self, kind, selected):
return [item for item in self.components if not selected or item.Selected]
class App:
def __init__(self, components):
self.ActiveView = View(components)
def GetProjectData(self):
return type('Project', (), {'GetProjectFilePath': lambda self: 'C:/board.prj'})()
class Api:
IVdComp = staticmethod(lambda value: value)
metadata = ['M', 'C:/board.prj', 'OLD', 'NEW', 'MAIN:1', 'preserve', '', '1',
'targets', 'u1\x1fR1\x1fA']
record = ['C', 'u1', 'R1', '0', '0', '0', '0', '']
components = [Component('u1', 'R1', 'OLD', 'A'), Component('u2', 'R2', 'OLD', 'B')]
components[0].Selected = True
assert len(check_context(App(components), Api, metadata, [record])) == 2
components[0].attributes['Value'] = 'changed'
with pytest.raises(ValueError):
check_context(App(components), Api, metadata, [record])
components[0].attributes['Value'] = 'A'
components.pop(0)
with pytest.raises(ValueError):
check_context(App(components), Api, metadata, [record])
def test_native_rejects_changed_unselected_component_since_snapshot(tmp_path):
class Component:
UID = 'u2'
Refdes = 'R2'
def FindAttribute(self, name):
values = {'Part Number': 'OLD', 'Value': 'CHANGED', 'Part Name': '', 'Part Label': ''}
value = values[name]
return type('Attr', (), {'InstanceValue': value, 'Value': value})()
snapshot = tmp_path / 'snapshot.tsv'
snapshot.write_text('M\tC:/board.prj\tOLD\tNEW\tMAIN:1\tpreserve\t\t1\ttargets\tu1%001FR1%001FA\n'
'C\tu1\tR1\t0\t0\t0\t0\t\n'
'U\tu2\tR2\tOLD\tORIGINAL\t\t\n', encoding='utf-8')
with pytest.raises(ValueError):
verify_unselected_snapshot(snapshot, [Component()], {'u1'})