perf(native): remove duplicate identity and selection reads

This commit is contained in:
阿白叔 committed 2026-10-19 09:41:43 +08:00
1 parent 0714c7753f
commit 5a0f62f0de
2 files changed
+98 -20

No files matched your search

@@ -0,0 +1,68 @@
from collections import Counter
from types import SimpleNamespace
import pytest
from dx_productivity.trial_task_center import native_helper as native
class Component:
def __init__(self, uid, refdes, **attributes):
self.UID, self.Refdes = uid, refdes
self.attributes, self.reads = attributes, Counter()
def FindAttribute(self, name):
self.reads[name] += 1
value = self.attributes.get(name)
return None if value is None else SimpleNamespace(InstanceValue=value, Value=value)
@pytest.mark.parametrize('target_scope', [True, False])
@pytest.mark.parametrize('changed', [False, True])
def test_native_readback_checks_each_unselected_attribute_once(tmp_path, target_scope, changed):
snapshot = tmp_path / 'snapshot.tsv'
snapshot.write_text('U\tu2\tR2\tOLD\tV0\tNAME\tLABEL\n', encoding='utf8')
target = Component('u1', 'R1', **{'Part Number': 'NEW', 'Part Name': 'TARGET', 'Part Label': 'NEWLABEL'})
other = Component('u2', 'R2', **{'Part Number': 'OLD', 'Value': 'CHANGED' if changed else 'V0',
'Part Name': 'NAME', 'Part Label': 'LABEL'})
metadata = ['M', 'project', 'OLD', 'NEW', 'sheet', 'part_label', '', '1']
if target_scope:
metadata += ['targets', 'u1\x1fR1\x1fV0']
baseline = None if target_scope else {'u2': ('R2', 'OLD', 'V0', 'NAME', 'LABEL')}
def verify():
return native.verify_replacement_readback(snapshot, [target, other], metadata,
[['C', 'u1', 'R1']], {'u1', 'u2'}, baseline, True,
['lib', 'NEW', 'symbol', 'gate', 'TARGET', 'NEWLABEL'], ['u1'])
if changed:
with pytest.raises(ValueError, match='unselected component'):
verify()
else:
assert verify() == ['u1']
assert other.reads == {'Part Number': 1, 'Value': 1, 'Part Name': 1, 'Part Label': 1}
def test_preselection_context_does_not_query_an_unused_selection(monkeypatch):
target = Component('u1', 'R1', **{'Part Number': 'OLD', 'Value': 'V0'})
target._oleobj_ = target
class View:
selection_reads = 0
def Query(self, kind, scope):
if scope == 1:
self.selection_reads += 1
return []
return [target]
view = View()
app = SimpleNamespace(ActiveView=view)
api = SimpleNamespace(IVdComp=lambda item: item)
monkeypatch.setattr(native, 'check_active_context', lambda *_: None)
metadata = ['M', 'project', 'OLD', 'NEW', 'sheet', 'part_label', '', '1', 'targets', 'u1\x1fR1\x1fV0']
records = [['C', 'u1', 'R1']]
assert native.check_context(app, api, metadata, records, require_selection=False) == [target]
assert view.selection_reads == 0
with pytest.raises(ValueError, match='Selected component set'):
native.check_context(app, api, metadata, records)