Files
make-cad/test/test_freecad_single_read.py
T
NiJingzhe 06bb58b2bb fix(translator): stop reading the FreeCAD package twice
translate_product_package called read_product_package_translation_units
and then passed the package into _translate_product_package_to_script,
which called the same reader again - for bytes/path inputs the ZIP
parse, full validation, occurrence verification, and feature-graph
deserialization all ran twice on the slowest export path. Pass the
already-read units into the script builder instead. Contract test
wraps the reader with a spy: one translation call, exactly one read,
and byte-identical script output.
2026-08-21 16:24:28 +08:00

49 lines
1.6 KiB
Python

"""H2 contract test: FreeCAD package translation reads the package once."""
import tempfile
import unittest
from pathlib import Path
from unittest import mock
import simplecadapi as scad
def _build_package(root: Path) -> scad.ProductPackage:
cache = scad.CachePolicy(root=root / "cache")
@scad.part(id="single_box", cache=cache, project_root=Path(__file__).parent)
def build_box() -> scad.Part:
return scad.make_part_rpart(
"single_box", scad.make_box_rsolid(width=4.0, height=3.0, depth=2.0)
)
return scad.build_product_package(build_box())
class TestFreecadSingleRead(unittest.TestCase):
def test_translate_reads_package_units_once(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
package = _build_package(root)
payload = scad.encode_product_package(package)
from simplecadapi.translator import package_units as pu
from simplecadapi.translator.freecad_translator import (
translate_product_package_to_freecad_script,
)
reference = translate_product_package_to_freecad_script(payload)
with mock.patch.object(
pu,
"read_product_package_translation_units",
wraps=pu.read_product_package_translation_units,
) as spy:
script = translate_product_package_to_freecad_script(payload)
self.assertEqual(spy.call_count, 1)
self.assertEqual(script, reference)
if __name__ == "__main__":
unittest.main()