fix: read effective instance properties in native inventory

This commit is contained in:
阿白叔 committed 2026-10-17 14:01:40 +08:00
1 parent 2ad0a3c6c7
commit ea024da325
4 files changed
+157 -27

No files matched your search

+57 -4
View File
@@ -3,6 +3,50 @@ import pytest
from dx_productivity.trial_task_center import native_inventory_protocol as protocol
def effective_report(tmp_path, *, part='NEW', value='0R/NC', path='', duplicate=False):
output = tmp_path / 'effective.txt'
row = (' Symbol $27I100\tref des R1\tpage \n\tPath ' + path + '\n'
'\tProp Ref Designator R1\n\tProp Part Number ' + part + '\n\tProp Value ' + value + '\n')
output.write_text(' REPORT FORMAT: FREEFORM\n MODE: FLAT BLOCKS\n TEXT LISTS: ON\n'
' SYMBOLS ONLY: OFF\n NETS ONLY: OFF\n USER BLOCKS: NONE\n'
'Config default\tview none\n' + row + (row if duplicate else '') +
' Symbol $28I200\tref des R2\tpage \n\tPath \n'
'\tProp Ref Designator R2\n\tProp Part Number OLD\n\tProp Value 20K\n')
return output
def test_inventory_uses_effective_instance_part_and_value_before_filtering(tmp_path):
result = protocol.read_ascii_discovery(tmp_path / 'board.prj', report(tmp_path), ('NEW',),
effective_report_path=effective_report(tmp_path))
assert [(c.uid, c.old_part_number, c.old_value) for c in result.components] == [('$27I100', 'NEW', '0R/NC')]
assert result.components[0].schematic_name == 'board'
assert result.components[0].sheet_number == 'MAIN'
result = protocol.read_ascii_discovery(tmp_path / 'board.prj', report(tmp_path), ('OLD',),
effective_report_path=effective_report(tmp_path))
assert [c.uid for c in result.components] == ['$28I200']
@pytest.mark.parametrize('change', ['missing', 'extra', 'duplicate', 'nested', 'duplicate_property', 'multiple_configs', 'no_path', 'missing_value'])
def test_effective_inventory_requires_complete_unambiguous_uid_mapping(tmp_path, change):
flat = effective_report(tmp_path, duplicate=change == 'duplicate', path='U1' if change == 'nested' else '')
text = flat.read_text()
if change == 'missing':
text = text[:text.index(' Symbol $28I200')]
elif change == 'extra':
text += ' Symbol $29I300\tref des R3\tpage \n\tPath \n\tProp Ref Designator R3\n\tProp Part Number NEW\n'
elif change == 'duplicate_property':
text = text.replace('\tProp Part Number NEW', '\tProp Part Number NEW\n\tProp Part Number OTHER')
elif change == 'multiple_configs':
text += 'Config alternate\tview none\n'
elif change == 'no_path':
text = text.replace('\tPath \n', '')
elif change == 'missing_value':
text = text.replace('\tProp Value 0R/NC\n', '')
flat.write_text(text)
with pytest.raises(protocol.NativeInventoryUnsupported):
protocol.read_ascii_discovery(tmp_path / 'board.prj', report(tmp_path), ('NEW',), effective_report_path=flat)
def report(tmp_path):
path = tmp_path / 'report.txt'
path.write_text('iCDB Ascii Report Generation Date: test\n'
@@ -22,7 +66,8 @@ def report(tmp_path):
def test_offline_full_project_report_includes_disconnected_schematic_and_exact_uid(tmp_path):
path = report(tmp_path)
project = tmp_path / 'source.prj'
result = protocol.read_ascii_discovery(project, path, ('old',))
result = protocol.read_ascii_discovery(project, path, ('old',),
effective_report_path=effective_report(tmp_path, part='OLD', value='10K/NC'))
assert result.sheets == (('board', 'MAIN'), ('disconnected', 'AUX'))
assert [(t.project_path, t.schematic_name, t.sheet_number, t.uid, t.refdes, t.old_part_number, t.old_value)
for t in result.components] == [
@@ -56,11 +101,13 @@ def test_filtered_or_ambiguous_ascii_report_requires_com_fallback(tmp_path, chan
text = text.replace(old, new)
path.write_text(text)
with pytest.raises(protocol.NativeInventoryUnsupported):
protocol.read_ascii_discovery(tmp_path / 'source.prj', path, ('OLD',))
protocol.read_ascii_discovery(tmp_path / 'source.prj', path, ('OLD',),
effective_report_path=effective_report(tmp_path, part='OLD', value='10K/NC'))
def test_absent_requested_part_returns_empty_inventory_with_available_sheets(tmp_path):
result = protocol.read_ascii_discovery(tmp_path / 'source.prj', report(tmp_path), ('OTHER',))
result = protocol.read_ascii_discovery(tmp_path / 'source.prj', report(tmp_path), ('OTHER',),
effective_report_path=effective_report(tmp_path, part='OLD', value='10K/NC'))
assert not result.components
assert result.matched_old_parts == {('board', 'MAIN'): frozenset(), ('disconnected', 'AUX'): frozenset()}
@@ -76,7 +123,8 @@ def test_component_cache_cannot_substitute_for_complete_page_discovery(tmp_path)
def discover(path, parts, *, collect_components=False, **controls):
calls.append(collect_components)
if collect_components:
return protocol.read_ascii_discovery(project, report(tmp_path), parts)
return protocol.read_ascii_discovery(project, report(tmp_path), parts,
effective_report_path=effective_report(tmp_path, part='OLD', value='10K/NC'))
return ProjectDiscovery((('board', 'MAIN'), ('disconnected', 'AUX'), ('empty', 'BLANK')),
{('board', 'MAIN'): frozenset({'old'}), ('disconnected', 'AUX'): frozenset({'old'}),
('empty', 'BLANK'): frozenset()})
@@ -85,3 +133,8 @@ def test_component_cache_cannot_substitute_for_complete_page_discovery(tmp_path)
full = preparer.discover_project(project, ('OLD',))
assert ('empty', 'BLANK') in full.sheets
assert calls == [True, False]
def test_inventory_requires_effective_values_even_when_no_requested_base_part_matches(tmp_path):
with pytest.raises(protocol.NativeInventoryUnsupported):
protocol.read_ascii_discovery(tmp_path / 'board.prj', report(tmp_path), ('NEW',))
+7 -4
View File
@@ -37,9 +37,11 @@ def reader_fixture(tmp_path, *, fail_cleanup=False, cancel=False, corrupt=False,
assert str(source.parent) not in snapshot.read_text()
Path(options['file']).write_text(
(' REPORT FORMAT: TABULAR\n' if corrupt else ' REPORT FORMAT: FREEFORM\n') +
' MODE: HIER BLOCKS\n USER BLOCKS: NONE\n TEXT LISTS: ON\n'
(' MODE: FLAT BLOCKS\n' if '-f' in command else ' MODE: HIER BLOCKS\n') +
' USER BLOCKS: NONE\n TEXT LISTS: ON\n'
' SYMBOLS ONLY: OFF\n NETS ONLY: OFF\n'
'Block board \tRoot\n Symbol $27I100\tpage MAIN\n'
+ ('Config default\tview none\n Symbol $27I100\tref des R1\tpage \n\tPath \n'
if '-f' in command else 'Block board \tRoot\n Symbol $27I100\tpage MAIN\n') +
'\tProp Ref Designator R1\n\tProp Part Number OLD\n\tProp Value 10K\n')
def poll():
if cancel:
@@ -61,12 +63,13 @@ def test_reader_uses_a_disposable_database_and_returns_certified_native_identity
before = project.read_bytes(), (project.parent / 'database/icdb.dat').read_bytes()
messages = []
result = reader.read(project, ('OLD',), stop_event=stop, on_progress=messages.append)
assert len(calls) == 1 and released == [True]
assert len(calls) == 2 and released == [True]
assert '-h' in calls[0] and '-f' in calls[1]
assert [(c.refdes, c.uid, c.sheet_number) for c in result.components] == [('R1', '$27I100', 'MAIN')]
assert before == (project.read_bytes(), (project.parent / 'database/icdb.dat').read_bytes())
assert not list((tmp_path / 'temporary').iterdir())
record = json.loads(next((tmp_path / 'evidence').rglob('native-inventory.json')).read_text())
assert record['state'] == 'ready' and len(record['tools']) == 1
assert record['state'] == 'ready' and len(record['tools']) == 2
assert messages