- ruff profile in pyproject (E/W/F/I/B/RUF); clean src/ + tests/ - remove dead code: threaded_len/s/d/coat/first_rec_for_size, self.cat in gui - 59 public re-exports added to __all__ (sorted) - renamed l->ln (E741), zip(strict=True), split long lines, [*list, x] (RUF005) - CI: quality job (ruff), test runs tests/ -k 'not gen_all' w/ coverage - README rewritten (commands, API modules, tests/lint/coverage, CI) - docs/tasks.md: P4 recorded - 136 passed, 3 deselected (regen); ruff: all checks passed
84 lines
2.6 KiB
Python
84 lines
2.6 KiB
Python
"""Tests for KOMPAS (.con) converter: product params + OKPД2 + document XML.
|
|
|
|
No em-dash in code: Python 3.10 source lacks UTF-8 mode (no PEP 540).
|
|
Cyrillic only in string values.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
|
|
|
from fastengen.catalog import ProductRecord
|
|
from fastengen.kompas import build_document, save_document
|
|
|
|
|
|
def _rec(size="M12", ptype="bolt", length="40"):
|
|
return ProductRecord(
|
|
id=f"bolt_{size}_{length}",
|
|
designation=f"Болт {size}x{length} 8.8 ГОСТ 7802-70",
|
|
type=ptype,
|
|
category="bolt",
|
|
standard="ГОСТ 7802-70",
|
|
material="Сталь 8.8",
|
|
model_file=f"bolt_{size}_{length}.step",
|
|
attributes={"d": size, "l": length},
|
|
created_at="2026-01-01",
|
|
)
|
|
|
|
|
|
def test_okpd2_by_type():
|
|
assert '28.93.21.110' in build_document(_rec(ptype="bolt"))
|
|
assert "28.93.21.210" in build_document(_rec(ptype="nut"))
|
|
assert "28.93.21.310" in build_document(_rec(ptype="washer"))
|
|
assert "28.93.21.410" in build_document(_rec(ptype="stud"))
|
|
assert "28.93.21.510" in build_document(_rec(ptype="self_tacker"))
|
|
|
|
|
|
def test_document_is_valid_xml_and_contains_params():
|
|
xml = build_document(_rec())
|
|
from xml.etree.ElementTree import fromstring
|
|
root = fromstring(xml)
|
|
assert root.tag == "KOMPAS_Document"
|
|
assert "ГОСТ 7802-70" in xml
|
|
assert "bolt_M12_40.step" in xml
|
|
|
|
|
|
def test_save_document_writes_file():
|
|
with tempfile.TemporaryDirectory() as d:
|
|
out = os.path.join(d, "bolt_M12_40.con")
|
|
save_document(_rec(), out)
|
|
assert os.path.exists(out)
|
|
assert os.path.getsize(out) > 100
|
|
with open(out, encoding="utf-8") as f:
|
|
assert "designator" in f.read()
|
|
|
|
|
|
def test_build_document_okpd2_default():
|
|
# unknown type -> default OKPД2
|
|
xml = build_document(_rec(ptype="widget"))
|
|
assert "28.93.29.000" in xml
|
|
|
|
|
|
# =====CLI: fastengen kompas ======
|
|
|
|
|
|
def test_cli_kompas_exports_documents(tmp_path):
|
|
from fastengen.cli import main
|
|
|
|
cat = str(tmp_path / "cat.json")
|
|
models = str(tmp_path / "out")
|
|
out = str(tmp_path / "con")
|
|
main(["bolt-gen", "M12", "40", "--catalog", cat, "--dir", models])
|
|
main(["nut-gen", "M12", "--catalog", cat, "--dir", models])
|
|
main(["kompas", "--catalog", cat, "--dir", models, "--output", out])
|
|
|
|
con_files = sorted(os.listdir(out))
|
|
assert "bolt_M12_40.con" in con_files
|
|
assert "nut_M12.con" in con_files
|
|
with open(os.path.join(out, "bolt_M12_40.con"), encoding="utf-8") as f:
|
|
assert "28.93.21.110" in f.read()
|