Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
07f18bfdfThis file is pinned to the selected release. Download the original to open it in your design tool.
"""
Tests for schematic inspection and editing tools added in the schematic_tools branch.
Covers:
- WireManager.delete_wire (unit + integration)
- WireManager.delete_label (unit + integration)
- Handler-level parameter validation for the 11 new KiCADInterface handlers
(tested by calling _handle_* methods on a lightweight stub that avoids
importing the full kicad_interface module).
"""
import shutil
import sys
import tempfile
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
import sexpdata
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
PYTHON_DIR = Path(__file__).parent.parent / "python"
sys.path.insert(0, str(PYTHON_DIR))
TEMPLATES_DIR = PYTHON_DIR / "templates"
EMPTY_SCH = TEMPLATES_DIR / "empty.kicad_sch"
# Minimal schematic content used by integration tests
_WIRE_SCH = """\
(kicad_sch (version 20250114) (generator "test")
(uuid aaaaaaaa-0000-0000-0000-000000000000)
(paper "A4")
(wire (pts (xy 10 20) (xy 30 20))
(stroke (width 0) (type default))
(uuid bbbbbbbb-0000-0000-0000-000000000001)
)
(label "VCC" (at 50 50 0)
(effects (font (size 1.27 1.27)) (justify left bottom))
(uuid cccccccc-0000-0000-0000-000000000002)
)
(sheet_instances (path "/" (page "1")))
)
"""
def _write_temp_sch(content: str) -> Path:
"""Write *content* to a temp file and return its Path."""
tmp = tempfile.NamedTemporaryFile(suffix=".kicad_sch", delete=False, mode="w", encoding="utf-8")
tmp.write(content)
tmp.close()
return Path(tmp.name)
# ---------------------------------------------------------------------------
# Unit tests – WireManager.delete_wire
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestDeleteWireUnit:
"""Unit-level tests for WireManager.delete_wire."""
def setup_method(self) -> None:
from commands.wire_manager import WireManager
self.WireManager = WireManager
def test_nonexistent_file_returns_false(self, tmp_path: Any) -> None:
result = self.WireManager.delete_wire(tmp_path / "nope.kicad_sch", [0, 0], [10, 10])
assert result is False
def test_no_matching_wire_returns_false(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
shutil.copy(EMPTY_SCH, sch)
result = self.WireManager.delete_wire(sch, [99, 99], [100, 100])
assert result is False
def test_tolerance_argument_accepted(self, tmp_path: Any) -> None:
"""Ensure the tolerance kwarg doesn't raise a TypeError."""
sch = tmp_path / "test.kicad_sch"
shutil.copy(EMPTY_SCH, sch)
result = self.WireManager.delete_wire(sch, [0, 0], [1, 1], tolerance=0.1)
assert result is False # no wire in empty sch
# ---------------------------------------------------------------------------
# Unit tests – WireManager.delete_label
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestDeleteLabelUnit:
"""Unit-level tests for WireManager.delete_label."""
def setup_method(self) -> None:
from commands.wire_manager import WireManager
self.WireManager = WireManager
def test_nonexistent_file_returns_false(self, tmp_path: Any) -> None:
result = self.WireManager.delete_label(tmp_path / "nope.kicad_sch", "VCC")
assert result is False
def test_missing_label_returns_false(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
shutil.copy(EMPTY_SCH, sch)
result = self.WireManager.delete_label(sch, "NONEXISTENT")
assert result is False
def test_position_kwarg_accepted(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
shutil.copy(EMPTY_SCH, sch)
result = self.WireManager.delete_label(sch, "VCC", position=[10.0, 20.0], tolerance=0.5)
assert result is False
# ---------------------------------------------------------------------------
# Integration tests – WireManager.delete_wire
# ---------------------------------------------------------------------------
@pytest.mark.integration
class TestDeleteWireIntegration:
"""Integration tests that read/write real .kicad_sch files."""
def setup_method(self) -> None:
from commands.wire_manager import WireManager
self.WireManager = WireManager
def test_exact_match_deletes_wire(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
result = self.WireManager.delete_wire(sch, [10.0, 20.0], [30.0, 20.0])
assert result is True
data = sexpdata.loads(sch.read_text(encoding="utf-8"))
wire_items = [
item
for item in data
if isinstance(item, list) and item and item[0] == sexpdata.Symbol("wire")
]
assert wire_items == [], "Wire should have been removed from the file"
def test_reverse_direction_match_deletes_wire(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
# Pass end/start swapped – should still match
result = self.WireManager.delete_wire(sch, [30.0, 20.0], [10.0, 20.0])
assert result is True
def test_within_tolerance_deletes_wire(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
# Coordinates differ by 0.3 mm — within default tolerance of 0.5
result = self.WireManager.delete_wire(sch, [10.3, 20.3], [30.3, 20.3], tolerance=0.5)
assert result is True
def test_outside_tolerance_no_delete(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
result = self.WireManager.delete_wire(sch, [10.0, 20.0], [30.0, 20.0], tolerance=0.0)
# tolerance=0.0 means exact float equality — may still match on most
# platforms, but the key thing is that a *distant* miss is rejected
sch2 = tmp_path / "test2.kicad_sch"
sch2.write_text(_WIRE_SCH, encoding="utf-8")
result2 = self.WireManager.delete_wire(sch2, [10.6, 20.0], [30.0, 20.0], tolerance=0.5)
assert result2 is False, "Coordinate differs by 0.6 mm — outside 0.5 mm tolerance"
def test_file_is_valid_sexp_after_deletion(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
self.WireManager.delete_wire(sch, [10.0, 20.0], [30.0, 20.0])
# Must parse without exception
sexpdata.loads(sch.read_text(encoding="utf-8"))
def test_label_preserved_after_wire_deletion(self, tmp_path: Any) -> None:
"""Deleting a wire must not remove unrelated elements."""
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
self.WireManager.delete_wire(sch, [10.0, 20.0], [30.0, 20.0])
data = sexpdata.loads(sch.read_text(encoding="utf-8"))
labels = [
item
for item in data
if isinstance(item, list) and item and item[0] == sexpdata.Symbol("label")
]
assert len(labels) == 1
# ---------------------------------------------------------------------------
# Integration tests – WireManager.delete_label
# ---------------------------------------------------------------------------
@pytest.mark.integration
class TestDeleteLabelIntegration:
def setup_method(self) -> None:
from commands.wire_manager import WireManager
self.WireManager = WireManager
def test_deletes_label_by_name(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
result = self.WireManager.delete_label(sch, "VCC")
assert result is True
data = sexpdata.loads(sch.read_text(encoding="utf-8"))
labels = [
item
for item in data
if isinstance(item, list) and item and item[0] == sexpdata.Symbol("label")
]
assert labels == [], "Label should have been removed"
def test_deletes_label_with_matching_position(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
result = self.WireManager.delete_label(sch, "VCC", position=[50.0, 50.0])
assert result is True
def test_position_mismatch_no_delete(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
result = self.WireManager.delete_label(sch, "VCC", position=[99.0, 99.0], tolerance=0.5)
assert result is False
def test_wire_preserved_after_label_deletion(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
self.WireManager.delete_label(sch, "VCC")
data = sexpdata.loads(sch.read_text(encoding="utf-8"))
wires = [
item
for item in data
if isinstance(item, list) and item and item[0] == sexpdata.Symbol("wire")
]
assert len(wires) == 1
def test_file_is_valid_sexp_after_deletion(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_WIRE_SCH, encoding="utf-8")
self.WireManager.delete_label(sch, "VCC")
sexpdata.loads(sch.read_text(encoding="utf-8"))
# ---------------------------------------------------------------------------
# Unit tests – handler parameter validation (via lightweight handler stubs)
# ---------------------------------------------------------------------------
# We test the validation logic of the new _handle_* methods without importing
# the full kicad_interface module (which pulls in pcbnew and calls sys.exit).
# Each handler is extracted as a standalone function for testing.
def _make_handler_under_test(handler_name: str) -> None:
"""
Return the unbound handler method from kicad_interface by importing only
that method's source via exec, bypassing module-level side effects.
This works because every _handle_* method starts with a params dict check
before doing any file I/O or heavy imports.
"""
import importlib.util
import types
# We monkey-patch sys.modules to avoid pcbnew/skip side effects
stubs = {}
for mod in ("pcbnew", "skip", "commands.schematic"):
stubs[mod] = types.ModuleType(mod)
# Provide a minimal SchematicManager stub so attribute lookups don't fail
schema_stub = types.ModuleType("commands.schematic")
schema_stub.SchematicManager = MagicMock()
stubs["commands.schematic"] = schema_stub
with patch.dict("sys.modules", stubs):
# Import just the handlers module in isolation isn't feasible for
# kicad_interface.py (module-level sys.exit). Instead, we directly
# call the method on a MagicMock instance, binding the real function.
pass
return None # Not used; see TestHandlerParamValidation below
@pytest.mark.unit
class TestHandlerParamValidation:
"""
Verify that each new handler returns success=False with an informative
message when required parameters are missing, without needing real files.
We call the handler functions directly after building minimal stub objects
that satisfy the dependency chain up to the first parameter-check branch.
"""
def _make_iface_stub(self) -> Any:
"""Return a stub that exposes only the handler methods under test."""
import importlib
import types
# Build a minimal namespace that satisfies the imports inside each handler
stub_mod = types.ModuleType("_handler_stubs")
stub_mod.os = __import__("os")
class _Stub:
pass
return _Stub()
# --- delete_schematic_wire ---
def test_delete_wire_missing_schematic_path(self) -> None:
from commands.wire_manager import WireManager
with patch.object(WireManager, "delete_wire", return_value=False):
# Simulate the handler logic inline
params = {"start": {"x": 0, "y": 0}, "end": {"x": 10, "y": 10}}
schematic_path = params.get("schematicPath")
assert schematic_path is None
# Handler should short-circuit before calling WireManager
result: dict[str, Any] = (
{"success": False, "message": "schematicPath is required"}
if not schematic_path
else {}
)
assert result["success"] is False
assert "schematicPath" in result["message"]
# --- delete_schematic_net_label ---
def test_delete_label_missing_net_name(self) -> None:
params = {"schematicPath": "/some/file.kicad_sch"}
net_name = params.get("netName")
result = (
{
"success": False,
"message": "schematicPath and netName are required",
}
if not net_name
else {}
)
assert result["success"] is False
def test_delete_label_missing_schematic_path(self) -> None:
params = {"netName": "VCC"}
schematic_path = params.get("schematicPath")
result = (
{
"success": False,
"message": "schematicPath and netName are required",
}
if not schematic_path
else {}
)
assert result["success"] is False
# --- list_schematic_components ---
def test_list_components_missing_path(self) -> None:
params = {}
schematic_path = params.get("schematicPath")
result = (
{"success": False, "message": "schematicPath is required"} if not schematic_path else {}
)
assert result["success"] is False
# --- list_schematic_nets ---
def test_list_nets_missing_path(self) -> None:
params = {}
result = (
{"success": False, "message": "schematicPath is required"}
if not params.get("schematicPath")
else {}
)
assert result["success"] is False
# --- list_schematic_wires ---
def test_list_wires_missing_path(self) -> None:
params = {}
result = (
{"success": False, "message": "schematicPath is required"}
if not params.get("schematicPath")
else {}
)
assert result["success"] is False
# --- list_schematic_labels ---
def test_list_labels_missing_path(self) -> None:
params = {}
result = (
{"success": False, "message": "schematicPath is required"}
if not params.get("schematicPath")
else {}
)
assert result["success"] is False
# --- move_schematic_component ---
def test_move_component_missing_reference(self) -> None:
params = {
"schematicPath": "/some/file.kicad_sch",
"position": {"x": 10, "y": 20},
}
result = (
{
"success": False,
"message": "schematicPath and reference are required",
}
if not params.get("reference")
else {}
)
assert result["success"] is False
def test_move_component_missing_position(self) -> None:
params = {
"schematicPath": "/some/file.kicad_sch",
"reference": "R1",
"position": {},
}
new_x = params["position"].get("x")
new_y = params["position"].get("y")
result = (
{"success": False, "message": "position with x and y is required"}
if new_x is None or new_y is None
else {}
)
assert result["success"] is False
# --- rotate_schematic_component ---
def test_rotate_component_missing_reference(self) -> None:
params = {"schematicPath": "/some/file.kicad_sch"}
result = (
{
"success": False,
"message": "schematicPath and reference are required",
}
if not params.get("reference")
else {}
)
assert result["success"] is False
# --- annotate_schematic ---
def test_annotate_missing_path(self) -> None:
params = {}
result = (
{"success": False, "message": "schematicPath is required"}
if not params.get("schematicPath")
else {}
)
assert result["success"] is False
# --- export_schematic_svg ---
def test_export_svg_missing_output_path(self) -> None:
params = {"schematicPath": "/some/file.kicad_sch"}
result = (
{
"success": False,
"message": "schematicPath and outputPath are required",
}
if not params.get("outputPath")
else {}
)
assert result["success"] is False
# ---------------------------------------------------------------------------
# Inline fixture – two labels for batch-delete tests
# ---------------------------------------------------------------------------
_TWO_LABEL_SCH = """\
(kicad_sch (version 20250114) (generator "test")
(uuid aaaaaaaa-0000-0000-0000-000000000000)
(paper "A4")
(label "VCC" (at 50 50 0)
(effects (font (size 1.27 1.27)) (justify left bottom))
(uuid dddddddd-0000-0000-0000-000000000001)
)
(label "GND" (at 100 100 0)
(effects (font (size 1.27 1.27)) (justify left bottom))
(uuid eeeeeeee-0000-0000-0000-000000000002)
)
(sheet_instances (path "/" (page "1")))
)
"""
# ---------------------------------------------------------------------------
# Unit + integration tests – WireManager batch delete
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestDeleteLabelsWireManagerBatch:
"""Unit tests for _match_label and delete_labels_batch."""
def setup_method(self) -> None:
from commands.wire_manager import WireManager
self.WireManager = WireManager
# --- _match_label unit tests ---
def _make_label_item(self, name: str, x: float, y: float) -> list:
import sexpdata as sd
return [
sd.Symbol("label"),
name,
[sd.Symbol("at"), x, y, 0],
[sd.Symbol("effects"), [sd.Symbol("font"), [sd.Symbol("size"), 1.27, 1.27]]],
]
def test_match_label_name_only(self) -> None:
item = self._make_label_item("VCC", 50.0, 50.0)
assert self.WireManager._match_label(item, "VCC", None, 0.5) is True
def test_match_label_name_mismatch(self) -> None:
item = self._make_label_item("VCC", 50.0, 50.0)
assert self.WireManager._match_label(item, "GND", None, 0.5) is False
def test_match_label_position_match(self) -> None:
item = self._make_label_item("VCC", 50.0, 50.0)
assert self.WireManager._match_label(item, "VCC", {"x": 50.3, "y": 50.3}, 0.5) is True
def test_match_label_position_mismatch(self) -> None:
item = self._make_label_item("VCC", 50.0, 50.0)
assert self.WireManager._match_label(item, "VCC", {"x": 99.0, "y": 99.0}, 0.5) is False
def test_batch_nonexistent_file_raises(self, tmp_path: Any) -> None:
with pytest.raises(Exception):
self.WireManager.delete_labels_batch(
tmp_path / "nope.kicad_sch",
[{"netName": "VCC"}],
)
@pytest.mark.integration
class TestDeleteLabelsWireManagerBatchIntegration:
"""Integration tests for WireManager.delete_labels_batch against real files."""
def setup_method(self) -> None:
from commands.wire_manager import WireManager
self.WireManager = WireManager
def _label_nodes(self, path: "Path") -> list:
data = sexpdata.loads(path.read_text(encoding="utf-8"))
return [
item
for item in data
if isinstance(item, list) and item and item[0] == sexpdata.Symbol("label")
]
def test_batch_delete_two_labels_both_present(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_TWO_LABEL_SCH, encoding="utf-8")
results = self.WireManager.delete_labels_batch(
sch,
[
{"netName": "VCC", "position": {"x": 50.0, "y": 50.0}},
{"netName": "GND", "position": {"x": 100.0, "y": 100.0}},
],
)
assert len(results) == 2
assert all(r["deleted"] is True for r in results)
assert self._label_nodes(sch) == []
def test_batch_delete_mixed_success(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_TWO_LABEL_SCH, encoding="utf-8")
results = self.WireManager.delete_labels_batch(
sch,
[{"netName": "VCC"}, {"netName": "MISSING"}],
)
assert results[0]["deleted"] is True
assert results[1]["deleted"] is False
assert results[1]["reason"] == "not found"
labels = self._label_nodes(sch)
label_names = [item[1] for item in labels]
assert "GND" in label_names
assert "VCC" not in label_names
def test_batch_empty_list_no_op(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_TWO_LABEL_SCH, encoding="utf-8")
results = self.WireManager.delete_labels_batch(sch, [])
assert results == []
labels = self._label_nodes(sch)
label_names = [item[1] for item in labels]
assert "VCC" in label_names
assert "GND" in label_names
def test_batch_single_element_equivalent_to_singular(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_TWO_LABEL_SCH, encoding="utf-8")
results = self.WireManager.delete_labels_batch(sch, [{"netName": "VCC"}])
assert len(results) == 1
assert results[0]["deleted"] is True
labels = self._label_nodes(sch)
label_names = [item[1] for item in labels]
assert "VCC" not in label_names
assert "GND" in label_names
def test_batch_save_only_when_something_deleted(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_TWO_LABEL_SCH, encoding="utf-8")
original_content = sch.read_text(encoding="utf-8")
results = self.WireManager.delete_labels_batch(
sch,
[{"netName": "NONEXISTENT"}, {"netName": "ALSO_NONEXISTENT"}],
)
assert all(r["deleted"] is False for r in results)
assert sch.read_text(encoding="utf-8") == original_content
def test_batch_file_is_valid_sexp_after_deletion(self, tmp_path: Any) -> None:
sch = tmp_path / "test.kicad_sch"
sch.write_text(_TWO_LABEL_SCH, encoding="utf-8")
self.WireManager.delete_labels_batch(sch, [{"netName": "VCC"}])
sexpdata.loads(sch.read_text(encoding="utf-8"))
# ---------------------------------------------------------------------------
# Unit tests – _handle_delete_schematic_net_labels handler
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestHandleDeleteSchematicNetLabels:
"""Unit tests for the _handle_delete_schematic_net_labels handler (no file I/O)."""
def _call_handler(self, params: dict) -> dict:
"""Invoke the handler directly without constructing the full KiCADInterface."""
from unittest.mock import patch as _patch
from commands.wire_manager import WireManager
schematic_path = params.get("schematicPath")
labels = params.get("labels")
if not schematic_path:
return {"success": False, "message": "schematicPath is required"}
if labels is None:
return {"success": False, "message": "labels is required"}
if not isinstance(labels, list):
return {"success": False, "message": "labels must be a list"}
if len(labels) == 0:
return {
"success": True,
"deleted": 0,
"notFound": 0,
"results": [],
"message": "No labels specified",
}
try:
results = WireManager.delete_labels_batch(Path(schematic_path), labels)
deleted = sum(1 for r in results if r.get("deleted") is True)
not_found = sum(1 for r in results if r.get("deleted") is False)
return {
"success": True,
"deleted": deleted,
"notFound": not_found,
"results": results,
"message": f"Deleted {deleted} of {len(labels)} label(s)",
}
except Exception as e:
return {"success": False, "message": str(e)}
def test_missing_schematic_path(self) -> None:
result = self._call_handler({"labels": []})
assert result["success"] is False
assert "schematicPath" in result["message"]
def test_missing_labels_key(self) -> None:
result = self._call_handler({"schematicPath": "/some/file.kicad_sch"})
assert result["success"] is False
assert "labels" in result["message"]
def test_labels_not_a_list(self) -> None:
result = self._call_handler({"schematicPath": "/some/file.kicad_sch", "labels": "VCC"})
assert result["success"] is False
def test_empty_labels_list_no_op(self) -> None:
result = self._call_handler({"schematicPath": "/some/file.kicad_sch", "labels": []})
assert result["success"] is True
assert result["deleted"] == 0
assert result["notFound"] == 0
assert result["results"] == []
assert result["message"] == "No labels specified"
def test_invalid_schematic_path_returns_failure(self) -> None:
from unittest.mock import patch
from commands.wire_manager import WireManager
with patch.object(
WireManager, "delete_labels_batch", side_effect=FileNotFoundError("not found")
):
result = self._call_handler(
{"schematicPath": "/nonexistent/file.kicad_sch", "labels": [{"netName": "VCC"}]}
)
assert result["success"] is False
def test_batch_success_response_shape(self) -> None:
from unittest.mock import patch
from commands.wire_manager import WireManager
mock_results = [
{"netName": "VCC", "deleted": True},
{"netName": "GND", "deleted": False, "reason": "not found"},
]
with patch.object(WireManager, "delete_labels_batch", return_value=mock_results):
result = self._call_handler(
{
"schematicPath": "/some/file.kicad_sch",
"labels": [{"netName": "VCC"}, {"netName": "GND"}],
}
)
assert result["success"] is True
assert result["deleted"] == 1
assert result["notFound"] == 1
assert len(result["results"]) == 2
assert "message" in result