from decimal import Decimal from dx_productivity.domain.models import AttributeRecord from dx_productivity.features.bom import BOMSettings, build_bom def _component(component_factory, bridge_id, refdes, value, pn="P1", **kw): return component_factory( bridge_id=bridge_id, stable_uid=f"uid-{bridge_id}", full_path=f"ROOT\{bridge_id}", logical_group_key=f"uid-{bridge_id}", refdes=refdes, symbol_refdes_default=refdes, attributes=( AttributeRecord(name="VALUE", value=value, visible=True), AttributeRecord(name="Part Number", value=pn, visible=True), ), **kw, ) def test_same_page_same_part_merges(component_factory, snapshot_factory): components = tuple( _component(component_factory, f"c{i}", f"R{i}", "10K") for i in range(1, 4) ) snapshot = snapshot_factory(components=components) result = build_bom(snapshot, BOMSettings()) assert len(result.rows) == 1 assert result.rows[0].qty == 3 assert result.rows[0].refdes == "R1,R2,R3" assert result.rows[0].part_number == "P1" def test_nc_filter_excludes_rows(component_factory, snapshot_factory): nc = _component(component_factory, "c1", "R1", "NC CAP") normal = _component( component_factory, "c2", "R2", "10K", pn="P2" ) snapshot = snapshot_factory(components=(nc, normal)) filtered = build_bom(snapshot, BOMSettings(exclude_nc=True)) unfiltered = build_bom(snapshot, BOMSettings(exclude_nc=False)) assert filtered.excluded_count == 1 assert len(filtered.rows) == 1 assert len(unfiltered.rows) == 2 def test_mark_rows_are_removed(component_factory, snapshot_factory): marker = _component( component_factory, "c1", "TP1", "TEST MARK", pn="MARK-1" ) normal = _component(component_factory, "c2", "R2", "10K", pn="P2") snapshot = snapshot_factory(components=(marker, normal)) result = build_bom(snapshot, BOMSettings()) assert [row.part_number for row in result.rows] == ["P2"] def test_sections_are_grouped_by_page(component_factory, snapshot_factory): from decimal import Decimal as D p1 = _component( component_factory, "c1", "R1", "10K", sheet_number=1, y=D(10) ) p2 = _component( component_factory, "c2", "R2", "10K", sheet_number=2, y=D(10) ) snapshot = snapshot_factory(components=(p1, p2)) result = build_bom(snapshot, BOMSettings()) assert [page for page, _rows in result.sections] == [1, 2] def test_duplicate_refdes_flagged(component_factory, snapshot_factory): a = _component(component_factory, "c1", "R1", "10K", pn="P1") b = _component(component_factory, "c2", "R1", "22K", pn="P2") snapshot = snapshot_factory(components=(a, b)) result = build_bom(snapshot, BOMSettings()) flagged = [row for row in result.rows if row.duplicate_refdes] assert len(flagged) == 2 def test_selected_scope_only_exports_selection( component_factory, snapshot_factory ): selected = _component(component_factory, "c1", "R1", "10K") other = _component( component_factory, "c2", "R2", "10K", selected=False ) snapshot = snapshot_factory(components=(selected, other)) result = build_bom(snapshot, BOMSettings(scope="selected")) assert len(result.rows) == 1 assert result.rows[0].refdes == "R1" def test_part_label_description_source(component_factory, snapshot_factory): component = component_factory( bridge_id="c1", attributes=( AttributeRecord(name="VALUE", value="10K", visible=True), AttributeRecord(name="Part Label", value="10K 1% RES", visible=True), AttributeRecord(name="Part Number", value="410000010A20", visible=True), ), ) snapshot = snapshot_factory(components=(component,)) result = build_bom( snapshot, BOMSettings(description_source="part-label") ) assert result.rows[0].description == "10K 1% RES" def test_pvcheck_mismatch_flagged(component_factory, snapshot_factory): component = component_factory( bridge_id="c1", attributes=( AttributeRecord(name="VALUE", value="4.7R", visible=True), AttributeRecord( name="Part Number", value="410000010A20", visible=True ), ), ) snapshot = snapshot_factory(components=(component,)) result = build_bom(snapshot, BOMSettings()) assert result.rows[0].part_number_mismatch is True assert "1.02R" in result.rows[0].mismatch_reason