383 lines
15 KiB
Python
383 lines
15 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, replace
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.part_replace.models import (
|
|
ComponentSnapshot,
|
|
DesignSnapshot,
|
|
MappingRule,
|
|
PinSnapshot,
|
|
UnitSnapshot,
|
|
)
|
|
from dx_productivity.part_replace.verification import (
|
|
build_impact_matrix,
|
|
eligible_logical_keys,
|
|
verify_replacement,
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ReplacementFixtures:
|
|
before: DesignSnapshot
|
|
compatible: DesignSnapshot
|
|
|
|
|
|
def _component(
|
|
logical_key: str,
|
|
refdes: str,
|
|
part_number: str,
|
|
*,
|
|
units: tuple[UnitSnapshot, ...] | None = None,
|
|
attributes: tuple[tuple[str, str], ...] = (),
|
|
) -> ComponentSnapshot:
|
|
return ComponentSnapshot(
|
|
logical_key=logical_key,
|
|
refdes=refdes,
|
|
part_number=part_number,
|
|
units=units if units is not None else (_unit("A"),),
|
|
attributes=attributes,
|
|
)
|
|
|
|
|
|
def _unit(unit_id: str, *, x: Decimal = Decimal("0"), pins: tuple[PinSnapshot, ...] | None = None) -> UnitSnapshot:
|
|
return UnitSnapshot(
|
|
unit_id=unit_id,
|
|
sheet="/MAIN",
|
|
symbol="SOURCE_SYMBOL",
|
|
origin_x=x - Decimal("0.5"),
|
|
origin_y=Decimal("3.5"),
|
|
x=x,
|
|
y=Decimal("4"),
|
|
rotation=90,
|
|
mirrored=False,
|
|
pins=pins if pins is not None else (
|
|
PinSnapshot("1", "A", Decimal("10"), Decimal("20"), "N$1"),
|
|
PinSnapshot("2", "B", Decimal("11"), Decimal("20"), "N$2"),
|
|
),
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def fixtures() -> ReplacementFixtures:
|
|
replacement = _component(
|
|
"/MAIN|R1", "R1", " old-1 ", attributes=(("tolerance", "1%"),),
|
|
)
|
|
unrelated = _component("/MAIN|C1", "C1", "KEEP-1", attributes=(("value", "1u"),))
|
|
before = DesignSnapshot(2, "project-1", "C:/Projects/board.prj", "before", (replacement, unrelated))
|
|
compatible = replace(
|
|
before,
|
|
fingerprint="after",
|
|
components=(
|
|
replace(
|
|
replacement,
|
|
part_number=" New-1 ",
|
|
units=(replace(replacement.units[0], symbol="LIBRARY_SYMBOL"),),
|
|
attributes=(("tolerance", "5%"), ("library", "central")),
|
|
),
|
|
unrelated,
|
|
),
|
|
)
|
|
return ReplacementFixtures(before, compatible)
|
|
|
|
|
|
def _replacement(after: DesignSnapshot) -> ComponentSnapshot:
|
|
return after.components[0]
|
|
|
|
|
|
def _replace_replacement(after: DesignSnapshot, component: ComponentSnapshot) -> DesignSnapshot:
|
|
return replace(after, components=(component, after.components[1]))
|
|
|
|
|
|
def change_refdes(after: DesignSnapshot) -> DesignSnapshot:
|
|
return _replace_replacement(after, replace(_replacement(after), refdes="R99"))
|
|
|
|
|
|
def move_origin(after: DesignSnapshot) -> DesignSnapshot:
|
|
component = _replacement(after)
|
|
return _replace_replacement(after, replace(component, units=(replace(component.units[0], x=Decimal("0.1")),)))
|
|
|
|
|
|
def change_origin_only(after: DesignSnapshot) -> DesignSnapshot:
|
|
component = _replacement(after)
|
|
unit = component.units[0]
|
|
assert hasattr(unit, "origin_x")
|
|
return _replace_replacement(
|
|
after,
|
|
replace(component, units=(replace(unit, origin_x=Decimal("7.125")),)),
|
|
)
|
|
|
|
|
|
def remove_pin(after: DesignSnapshot) -> DesignSnapshot:
|
|
component = _replacement(after)
|
|
return _replace_replacement(after, replace(component, units=(replace(component.units[0], pins=component.units[0].pins[:1]),)))
|
|
|
|
|
|
def move_pin(after: DesignSnapshot) -> DesignSnapshot:
|
|
component = _replacement(after)
|
|
pin = component.units[0].pins[0]
|
|
return _replace_replacement(
|
|
after,
|
|
replace(component, units=(replace(component.units[0], pins=(replace(pin, x=Decimal("10.1")), component.units[0].pins[1])),)),
|
|
)
|
|
|
|
|
|
def change_net(after: DesignSnapshot) -> DesignSnapshot:
|
|
component = _replacement(after)
|
|
pin = component.units[0].pins[0]
|
|
return _replace_replacement(
|
|
after,
|
|
replace(component, units=(replace(component.units[0], pins=(replace(pin, net="N$2"), component.units[0].pins[1])),)),
|
|
)
|
|
|
|
|
|
def change_unrelated_part(after: DesignSnapshot) -> DesignSnapshot:
|
|
return replace(after, components=(after.components[0], replace(after.components[1], part_number="CHANGED")))
|
|
|
|
|
|
def select_wrong_target(after: DesignSnapshot) -> DesignSnapshot:
|
|
return _replace_replacement(after, replace(_replacement(after), part_number="WRONG-1"))
|
|
|
|
|
|
def test_compatible_replacement_preserves_every_hard_invariant(fixtures: ReplacementFixtures) -> None:
|
|
result = verify_replacement(fixtures.before, fixtures.compatible, MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",))
|
|
|
|
assert result.passed
|
|
assert [(issue.code, issue.severity) for issue in result.issues] == [
|
|
("library_symbol_changed", "info"),
|
|
("library_attribute_changed", "info"),
|
|
]
|
|
assert result.changed_logical_keys == ("/MAIN|R1",)
|
|
|
|
|
|
def test_library_controlled_symbol_pin_names_and_attributes_are_reported_without_blocking(fixtures: ReplacementFixtures) -> None:
|
|
component = _replacement(fixtures.compatible)
|
|
library_wins = _replace_replacement(
|
|
fixtures.compatible,
|
|
replace(
|
|
component,
|
|
attributes=(("library", "central"),),
|
|
units=(
|
|
replace(
|
|
component.units[0],
|
|
symbol="ANOTHER_LIBRARY_SYMBOL",
|
|
pins=(replace(component.units[0].pins[0], name="LIBRARY_A"), component.units[0].pins[1]),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
|
|
result = verify_replacement(fixtures.before, library_wins, MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",))
|
|
|
|
assert result.passed
|
|
assert [(issue.code, issue.severity) for issue in result.issues] == [
|
|
("library_symbol_changed", "info"),
|
|
("library_pin_name_changed", "info"),
|
|
("library_attribute_changed", "info"),
|
|
]
|
|
|
|
|
|
def test_unrelated_symbol_and_attribute_changes_remain_blocking(fixtures: ReplacementFixtures) -> None:
|
|
unrelated = fixtures.compatible.components[1]
|
|
changed_unrelated = replace(
|
|
unrelated,
|
|
attributes=(("value", "47u"),),
|
|
units=(replace(unrelated.units[0], symbol="OTHER_SYMBOL"),),
|
|
)
|
|
after = replace(fixtures.compatible, components=(fixtures.compatible.components[0], changed_unrelated))
|
|
|
|
result = verify_replacement(fixtures.before, after, MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",))
|
|
|
|
assert not result.passed
|
|
assert ("unrelated_component_changed", "error") in [(issue.code, issue.severity) for issue in result.issues]
|
|
|
|
|
|
@pytest.mark.parametrize("mutator,code", [
|
|
(change_refdes, "refdes_changed"),
|
|
(move_origin, "placement_changed"),
|
|
(change_origin_only, "origin_changed"),
|
|
(remove_pin, "pin_set_changed"),
|
|
(move_pin, "pin_position_changed"),
|
|
(change_net, "connectivity_changed"),
|
|
(change_unrelated_part, "unrelated_component_changed"),
|
|
(select_wrong_target, "wrong_target_part_number"),
|
|
])
|
|
def test_verification_blocks_unsafe_change(fixtures: ReplacementFixtures, mutator, code: str) -> None:
|
|
result = verify_replacement(fixtures.before, mutator(fixtures.compatible), MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",))
|
|
|
|
assert not result.passed
|
|
assert code in {issue.code for issue in result.issues}
|
|
|
|
|
|
def test_impact_matrix_uses_trimmed_casefold_matching_and_snapshot_rule_order(fixtures: ReplacementFixtures) -> None:
|
|
second = replace(fixtures.before, project_id="project-2", components=(
|
|
_component("/MAIN|R2", "R2", "old-1", units=(_unit("A"), _unit("B"))),
|
|
))
|
|
rules = (MappingRule(" keep-1 ", "KEEP-2"), MappingRule("OLD-1", "NEW-1"))
|
|
|
|
matrix = build_impact_matrix((fixtures.before, second), rules)
|
|
|
|
assert [(cell.project_id, cell.rule.old_part_number, cell.logical_count, cell.unit_count) for cell in matrix] == [
|
|
("project-1", " keep-1 ", 1, 1),
|
|
("project-1", "OLD-1", 1, 1),
|
|
("project-2", " keep-1 ", 0, 0),
|
|
("project-2", "OLD-1", 1, 2),
|
|
]
|
|
assert eligible_logical_keys(fixtures.before, MappingRule(" OLD-1", "NEW-1")) == ("/MAIN|R1",)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("mutator", "expected_code"),
|
|
(
|
|
(lambda component: replace(component, units=()), "missing_units"),
|
|
(
|
|
lambda component: replace(
|
|
component,
|
|
units=(replace(component.units[0], pins=()),),
|
|
),
|
|
"missing_pins",
|
|
),
|
|
(
|
|
lambda component: replace(
|
|
component,
|
|
units=(
|
|
replace(
|
|
component.units[0],
|
|
pins=(replace(component.units[0].pins[0], net=None), component.units[0].pins[1]),
|
|
),
|
|
),
|
|
),
|
|
"missing_network_identity",
|
|
),
|
|
),
|
|
)
|
|
def test_matched_component_missing_evidence_blocks_impact_and_hard_verification(
|
|
fixtures: ReplacementFixtures,
|
|
mutator,
|
|
expected_code: str,
|
|
) -> None:
|
|
# Catches equality-only checks that accept equally incomplete before/after snapshots.
|
|
before_component = mutator(fixtures.before.components[0])
|
|
after_component = mutator(fixtures.compatible.components[0])
|
|
before = replace(fixtures.before, components=(before_component, fixtures.before.components[1]))
|
|
after = replace(fixtures.compatible, components=(after_component, fixtures.compatible.components[1]))
|
|
rule = MappingRule("OLD-1", "NEW-1")
|
|
|
|
impact = build_impact_matrix((before,), (rule,))[0]
|
|
result = verify_replacement(before, after, rule, ("/MAIN|R1",))
|
|
|
|
assert impact.logical_count == 1
|
|
assert expected_code in {issue.code for issue in impact.issues}
|
|
assert not result.passed
|
|
assert expected_code in {issue.code for issue in result.issues}
|
|
|
|
|
|
def test_coordinate_tolerance_allows_exact_boundary_but_blocks_larger_difference(fixtures: ReplacementFixtures) -> None:
|
|
component = _replacement(fixtures.compatible)
|
|
pin = component.units[0].pins[0]
|
|
at_boundary = _replace_replacement(
|
|
fixtures.compatible,
|
|
replace(component, units=(replace(component.units[0], pins=(replace(pin, x=Decimal("10.000001")), component.units[0].pins[1])),)),
|
|
)
|
|
beyond_boundary = _replace_replacement(
|
|
fixtures.compatible,
|
|
replace(component, units=(replace(component.units[0], pins=(replace(pin, x=Decimal("10.0000011")), component.units[0].pins[1])),)),
|
|
)
|
|
|
|
assert verify_replacement(fixtures.before, at_boundary, MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",)).passed
|
|
result = verify_replacement(fixtures.before, beyond_boundary, MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",))
|
|
assert "pin_position_changed" in {issue.code for issue in result.issues}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("mutator", "code"),
|
|
[
|
|
(lambda before, after: (replace(after, project_id="other"), "project_id_changed"), "project_id_changed"),
|
|
(lambda before, after: (replace(after, project_path="C:/Other/board.prj"), "project_path_changed"), "project_path_changed"),
|
|
(lambda before, after: (replace(after, components=after.components[:1]), "component_removed"), "component_removed"),
|
|
(lambda before, after: (replace(after, components=after.components + (_component("/MAIN|L1", "L1", "KEEP"),)), "component_added"), "component_added"),
|
|
(lambda before, after: (after, "expected_logical_key_missing"), "expected_logical_key_missing"),
|
|
],
|
|
)
|
|
def test_verification_blocks_project_and_component_identity_changes(fixtures: ReplacementFixtures, mutator, code: str) -> None:
|
|
after, expected_code = mutator(fixtures.before, fixtures.compatible)
|
|
expected_keys = ("/MAIN|R1",) if expected_code != "expected_logical_key_missing" else ("/MAIN|MISSING",)
|
|
|
|
result = verify_replacement(fixtures.before, after, MappingRule("OLD-1", "NEW-1"), expected_keys)
|
|
|
|
assert code in {issue.code for issue in result.issues}
|
|
assert not result.passed
|
|
|
|
|
|
def test_multi_unit_replacement_requires_the_same_gate_slot_set(fixtures: ReplacementFixtures) -> None:
|
|
units = (_unit("A"), _unit("B", x=Decimal("30")))
|
|
before_component = _component("/MAIN|U1", "U1", "OLD-1", units=units)
|
|
before = replace(fixtures.before, components=(before_component, fixtures.before.components[1]))
|
|
after = replace(before, components=(replace(before_component, part_number="NEW-1"), before.components[1]))
|
|
incompatible = replace(after, components=(replace(after.components[0], units=(after.components[0].units[0], _unit("C", x=Decimal("30")))), after.components[1]))
|
|
|
|
assert verify_replacement(before, after, MappingRule("OLD-1", "NEW-1"), ("/MAIN|U1",)).passed
|
|
result = verify_replacement(before, incompatible, MappingRule("OLD-1", "NEW-1"), ("/MAIN|U1",))
|
|
assert "unit_set_changed" in {issue.code for issue in result.issues}
|
|
|
|
|
|
def test_expected_key_must_have_the_rule_source_part_number(fixtures: ReplacementFixtures) -> None:
|
|
wrong_before = _replace_replacement(fixtures.before, replace(fixtures.before.components[0], part_number="OTHER"))
|
|
result = verify_replacement(wrong_before, fixtures.compatible, MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",))
|
|
|
|
assert "source_part_number_mismatch" in {issue.code for issue in result.issues}
|
|
assert not result.passed
|
|
|
|
|
|
def test_verification_fails_closed_for_duplicate_direct_snapshot_identities(fixtures: ReplacementFixtures) -> None:
|
|
duplicate_component = replace(fixtures.before.components[0], refdes="R2")
|
|
duplicate_unit = replace(fixtures.before.components[0].units[0], unit_id="A")
|
|
duplicate_pin = replace(fixtures.before.components[0].units[0].pins[0], name="Alternative")
|
|
malformed = replace(
|
|
fixtures.compatible,
|
|
components=(
|
|
replace(fixtures.compatible.components[0], units=(duplicate_unit, duplicate_unit)),
|
|
fixtures.compatible.components[1],
|
|
duplicate_component,
|
|
),
|
|
)
|
|
malformed = replace(
|
|
malformed,
|
|
components=(
|
|
replace(
|
|
malformed.components[0],
|
|
units=(
|
|
replace(duplicate_unit, pins=(duplicate_pin, duplicate_pin)),
|
|
duplicate_unit,
|
|
),
|
|
),
|
|
malformed.components[1],
|
|
malformed.components[2],
|
|
),
|
|
)
|
|
|
|
result = verify_replacement(fixtures.before, malformed, MappingRule("OLD-1", "NEW-1"), ("/MAIN|R1",))
|
|
|
|
assert {"duplicate_logical_key", "duplicate_unit_id", "duplicate_pin_number"} <= {issue.code for issue in result.issues}
|
|
assert not result.passed
|
|
|
|
|
|
def test_duplicate_normalized_refdes_is_ineligible_and_blocks_verification(fixtures: ReplacementFixtures) -> None:
|
|
duplicate_refdes_before = replace(fixtures.before.components[1], refdes=" r1 ", part_number="OLD-1")
|
|
before = replace(fixtures.before, components=(fixtures.before.components[0], duplicate_refdes_before))
|
|
after = replace(
|
|
fixtures.compatible,
|
|
components=(
|
|
fixtures.compatible.components[0],
|
|
replace(duplicate_refdes_before, part_number="NEW-1"),
|
|
),
|
|
)
|
|
rule = MappingRule("OLD-1", "NEW-1")
|
|
|
|
assert eligible_logical_keys(before, rule) == ()
|
|
result = verify_replacement(before, after, rule, ("/MAIN|R1", "/MAIN|C1"))
|
|
assert not result.passed
|
|
assert ("duplicate_refdes", "error") in [(issue.code, issue.severity) for issue in result.issues]
|