Add unit tests and GitHub Action

This commit is contained in:
Richard Ward
2025-07-17 00:14:08 +01:00
parent f1ef73134e
commit 69e6114e84
4 changed files with 71 additions and 0 deletions

20
.github/workflows/python-tests.yml vendored Normal file
View File

@@ -0,0 +1,20 @@
name: Python unit tests
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
pip install pytest trimesh shapely
- name: Run tests
env:
PYTHONPATH: ${{ github.workspace }}
run: pytest -vv

13
tests/test_export.py Normal file
View File

@@ -0,0 +1,13 @@
import os
import pytest
import trimesh
from parametric_cad.primitives.box import Box
from parametric_cad.export.stl import STLExporter
def test_stl_exporter(tmp_path):
exporter = STLExporter(output_dir=tmp_path)
box = Box(1.0, 1.0, 1.0)
path = exporter.export_mesh(box, "test_box", preview=False)
assert os.path.isfile(path)
assert path == str(tmp_path / "test_box.stl")

15
tests/test_mechanisms.py Normal file
View File

@@ -0,0 +1,15 @@
import numpy as np
import pytest
import trimesh
from parametric_cad.mechanisms.butthinge import ButtHinge
def test_butthinge_mesh_and_translation():
hinge = ButtHinge()
mesh = hinge.mesh()
assert isinstance(mesh, trimesh.Trimesh)
original_centroid = mesh.centroid.copy()
hinge.at(1.0, 2.0, 3.0)
translated_centroid = hinge.mesh().centroid
assert np.allclose(translated_centroid, original_centroid + [1.0, 2.0, 3.0])

23
tests/test_primitives.py Normal file
View File

@@ -0,0 +1,23 @@
import pytest
import numpy as np
import trimesh
from math import cos, pi
from parametric_cad.primitives.box import Box
from parametric_cad.primitives.gear import SpurGear
def test_box_mesh_extents_and_position():
box = Box(1.0, 2.0, 3.0).at(1.0, 1.0, 1.0)
mesh = box.mesh()
assert np.allclose(mesh.centroid, [1.0, 1.0, 1.0])
assert mesh.extents[0] == pytest.approx(1.0)
assert mesh.extents[1] == pytest.approx(2.0)
assert mesh.extents[2] == pytest.approx(3.0)
def test_spur_gear_diameters():
gear = SpurGear(module=2.0, teeth=10)
assert gear.pitch_diameter == pytest.approx(20.0)
expected_base = gear.pitch_diameter * cos(20 * pi / 180)
assert gear.base_diameter == pytest.approx(expected_base)