Files
periscope/tests/test_inductor_specs.py
T
michele 8040686e8b Allow ferrite beads without impedance_ohm to load.
Missing Z is optional, not invented. graph_build skips bad models
instead of aborting. Other inductors still require value_henries.
2026-09-20 23:08:33 +02:00

108 lines
3.3 KiB
Python

"""Ferrite beads without Z validate; other inductors still need henries."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from pydantic import ValidationError
from backend.periscopex.graph import _load_component_models
from backend.periscopex.models import ComponentModel, InductorSpecs
from backend.periscopex.resolve_passives import simple_to_typed_passive_specs
from backend.periscopex.models import SimpleComponentSpecs
def test_ferrite_bead_without_impedance_ohm_validates():
specs = InductorSpecs(
component_subtype="passive.ferrite_bead",
value_formatted="120 ohm @ 100 MHz",
current_rating_a="3A",
dcr_ohms=0.03,
)
assert specs.impedance_ohm is None
model = ComponentModel(mpn="BLM21PG121SN1D", specs=specs)
assert model.specs.impedance_ohm is None
def test_ferrite_bead_live_payload_without_z_validates():
model = ComponentModel.model_validate({
"mpn": "BLM21PG121SN1D",
"specs": {
"specs_type": "inductor",
"component_subtype": "passive.ferrite_bead",
"value_henries": 120.0,
"value_formatted": "120 ohm @ 100 MHz",
"tolerance": None,
"package": "0805",
"current_rating_a": "3A",
"dcr_ohms": 0.03,
},
})
assert model.specs.impedance_ohm is None
assert model.specs.current_rating_a == "3A"
assert model.specs.dcr_ohms == 0.03
def test_ferrite_bead_with_impedance_ohm_still_ok():
specs = InductorSpecs(
component_subtype="passive.ferrite_bead",
value_formatted="120 ohm @ 100 MHz",
impedance_ohm=120.0,
current_rating_a="3A",
dcr_ohms=0.03,
)
assert specs.impedance_ohm == 120.0
def test_inductor_still_requires_value_henries():
with pytest.raises(ValidationError, match="inductor requires value_henries"):
InductorSpecs(
component_subtype="passive.inductor",
value_formatted="15nH",
)
def test_load_models_bead_without_z_does_not_raise(tmp_path: Path):
payload = {
"mpn": "BLM21PG121SN1D",
"specs": {
"specs_type": "inductor",
"component_subtype": "passive.ferrite_bead",
"value_formatted": "FB",
"current_rating_a": "3A",
"dcr_ohms": 0.03,
},
}
(tmp_path / "BLM21PG121SN1D.json").write_text(json.dumps(payload) + "\n")
loaded = _load_component_models(tmp_path)
assert "BLM21PG121SN1D" in loaded
assert loaded["BLM21PG121SN1D"].impedance_ohm is None
def test_load_models_skips_inductor_missing_henries(tmp_path: Path):
bad = {
"mpn": "LQW-BAD",
"specs": {
"specs_type": "inductor",
"component_subtype": "passive.inductor",
"value_formatted": "x",
},
}
(tmp_path / "LQW-BAD.json").write_text(json.dumps(bad) + "\n")
loaded = _load_component_models(tmp_path)
assert loaded == {}
def test_simple_to_typed_bead_without_z():
simple = SimpleComponentSpecs(
specs_type="passive",
component_subtype="passive.ferrite_bead",
values={"current_rating_a": "3A", "dcr_ohms": 0.03},
)
specs = simple_to_typed_passive_specs(simple)
assert specs.specs_type == "inductor"
assert specs.impedance_ohm is None
assert specs.dcr_ohms == 0.03