Files
boltgen/tests/test_boltgen.py
OpenClaw Coder 95cfecd965
CI / test (3.10) (push) Canceled after 0s
CI / test (3.11) (push) Canceled after 0s
CI / test (3.12) (push) Canceled after 0s
CI / build-check (push) Canceled after 0s
boltgen: GOST 1759.0-87 designation (strength class + coating Zn/Od/An/Ni); collapse duplicate designation helpers; add strength_class/coating to specs
2026-11-08 12:56:04 +10:00

167 lines
4.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Тесты для boltgen — генератор крепёжа с STEP-библиотекой и каталогом."""
from __future__ import annotations
import os
import sys
import tempfile
from contextlib import contextmanager
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
import pytest
from boltgen import PortableCatalog, get_bolt, get_nut, get_washer
from boltgen.standard import (
BOLT_ALLOWED_LENGTHS,
BOLT_SIZES,
NUT_SIZES,
WASHER_SIZES,
)
# =====Стандарты ======
def test_bolt_sizes_present():
assert "M6" in BOLT_SIZES and "M24" in BOLT_SIZES
assert BOLT_SIZES["M10"]["d"] == 10.0
def test_nut_washer_sizes():
assert len(NUT_SIZES) == 9
assert len(WASHER_SIZES) == 9
def test_invalid_bolt_size():
with pytest.raises(ValueError):
get_bolt("M99", 40)
def test_invalid_length():
with pytest.raises(ValueError):
get_bolt("M10", 33) # 33不在 BOLT_ALLOWED_LENGTHS
def test_valid_lengths():
assert all(l in BOLT_ALLOWED_LENGTHS for l in (20, 25, 120))
# =====Генерация STEP ======
@pytest.mark.parametrize("size,length", [("M6", 20), ("M10", 40), ("M16", 80), ("M24", 120)])
def test_generate_bolt_step(size, length, tmp_path):
spec = get_bolt(size, length)
out = str(tmp_path / f"bolt_{size}_{length}.step")
rec = PortableCatalog(str(tmp_path / "cat.json"), models_dir=str(tmp_path)).add_bolt(spec)
assert os.path.exists(out)
assert os.path.getsize(out) > 1000 # не пустой STEP
@pytest.mark.parametrize("size", list(NUT_SIZES))
def test_generate_nut_step(size, tmp_path):
spec = get_nut(size)
out = str(tmp_path / f"nut_{size}.step")
PortableCatalog(str(tmp_path / "cat.json"), models_dir=str(tmp_path)).add_nut(spec)
assert os.path.exists(out)
assert os.path.getsize(out) > 1000
@pytest.mark.parametrize("size", list(WASHER_SIZES))
def test_generate_washer_step(size, tmp_path):
spec = get_washer(size)
out = str(tmp_path / f"washer_{size}.step")
PortableCatalog(str(tmp_path / "cat.json"), models_dir=str(tmp_path)).add_washer(spec)
assert os.path.exists(out)
assert os.path.getsize(out) > 1000
# =====Каталог ======
def test_catalog_roundtrip(tmp_path):
cat_path = str(tmp_path / "catalog.json")
models_dir = str(tmp_path / "out")
cat = PortableCatalog(cat_path, models_dir=models_dir)
# добавляем
b = cat.add_bolt(get_bolt("M12", 45))
n = cat.add_nut(get_nut("M12"))
w = cat.add_washer(get_washer("M12"))
cat.save()
# размер каталога
assert len(cat) == 3
assert "bolt_M12_45" in cat
assert cat["bolt_M12_45"].designation == "Болт М12×45 8.8 ГОСТ 1759.0-87 ГОСТ 7805-70 Zn (225) ГОСТ 1759.2-76"
assert b.model_file.endswith(".step")
assert b.attributes["диаметр"] == "12"
# загрузка заново
cat2 = PortableCatalog(cat_path, models_dir=models_dir)
assert len(cat2) == 3
assert cat2["nut_M12"].type == "nut"
assert cat2["washer_M12"].standard == "ГОСТ 11371-78"
def test_gen_all(tmp_path):
cat_path = str(tmp_path / "catalog.json")
models_dir = str(tmp_path / "out")
cat = PortableCatalog(cat_path, models_dir=models_dir)
recs = cat.add_all()
# 167 болтов + 9 гаек + 9 шайб = 185
assert len(cat) == 167 + 9 + 9
assert len(recs) == 185
assert cat_path.endswith("catalog.json")
# все модели существуют
for r in cat:
assert os.path.exists(os.path.join(models_dir, r.model_file))
# =====CLI ======
def test_cli_bolt_gen(tmp_path):
from boltgen.cli import main
with chdir(tmp_path):
assert main(["bolt-gen", "M10", "40", "--dir", "out"]) == 0
assert os.path.exists(str(tmp_path / "out" / "bolt_M10_40.step"))
assert os.path.exists(str(tmp_path / "catalog.json"))
def test_cli_gen_all(tmp_path):
from boltgen.cli import main
with chdir(tmp_path):
assert main(["gen-all", "--dir", "out"]) == 0
cat_path = tmp_path / "catalog.json"
assert cat_path.exists()
def test_cli_show_and_list(tmp_path):
from boltgen.cli import main
with chdir(tmp_path):
main(["gen-all", "--dir", "out"])
assert main(["list"]) == 0
assert main(["show", "bolt_M10_40"]) == 0
def test_cli_error(tmp_path):
from boltgen.cli import main
with chdir(tmp_path):
assert main(["bolt-gen", "M99", "40"]) == 2 # invalid size
@contextmanager
def chdir(d):
cwd = os.getcwd()
os.chdir(d)
try:
yield
finally:
os.chdir(cwd)