Update tests / helpers to allow adding more tests

This commit is contained in:
Arthur Moore 2024-04-27 01:20:29 -04:00
parent 0649f10802
commit 73b10760f8
2 changed files with 65 additions and 39 deletions

View file

@ -2,6 +2,9 @@
Helpful classes for running OpenScad from Python. Helpful classes for running OpenScad from Python.
@Copyright Arthur Moore 2024 MIT License @Copyright Arthur Moore 2024 MIT License
""" """
import subprocess
from pathlib import Path
from typing import NamedTuple from typing import NamedTuple
class Vec3(NamedTuple): class Vec3(NamedTuple):
@ -30,15 +33,42 @@ def set_variable_argument(var: str, val) -> [str, str]:
""" """
return ['-D', f'{var}={str(val)}'] return ['-D', f'{var}={str(val)}']
openscad_binary_windows = 'C:\Program Files\OpenSCAD\openscad.exe' class OpenScadRunner:
'''Helper to run the openscad binary'''
scad_file_path: Path
openscad_binary_path: Path
image_folder_base: Path
common_arguments = [ WINDOWS_DEFAULT_PATH = 'C:\Program Files\OpenSCAD\openscad.exe'
#'--hardwarnings', // Does not work when setting variables by using functions TOP_ANGLE_CAMERA = CameraArguments(Vec3(0,0,0),Vec3(45,0,45),50)
'--enable=fast-csg',
'--enable=predictible-output',
'--imgsize=1280,720',
'--view=axes',
'--projection=ortho',
] + set_variable_argument('$fa', 8) + set_variable_argument('$fs', 0.25)
top_angle_camera = CameraArguments(Vec3(0,0,0),Vec3(45,0,45),50) common_arguments = [
#'--hardwarnings', // Does not work when setting variables by using functions
'--enable=fast-csg',
'--enable=predictible-output',
'--imgsize=1280,720',
'--view=axes',
'--projection=ortho',
] + \
set_variable_argument('$fa', 8) + set_variable_argument('$fs', 0.25)
def __init__(self, file_path: Path):
self.openscad_binary_path = self.WINDOWS_DEFAULT_PATH
self.scad_file_path = file_path
self.image_folder_base = Path('.')
def create_image(self, camera_args: CameraArguments, args: [str], image_file_name: str):
"""
Run the code, to create an image.
@Important The only verification is that no errors occured.
There is no verification if the image was created, or the image contents.
"""
assert(self.scad_file_path.exists())
assert(self.image_folder_base.exists())
image_path = self.image_folder_base.joinpath(image_file_name)
command_arguments = self.common_arguments + \
[camera_args.as_argument()] + args + \
[f'-o{str(image_path)}', str(self.scad_file_path)]
#print(command_arguments)
return subprocess.run([self.openscad_binary_path]+command_arguments, check=True)

View file

@ -1,36 +1,24 @@
""" """
Functions for testing hole cutouts. Tests for gridfinity-rebuilt-holes.scad
@Copyright Arthur Moore 2024 MIT License @Copyright Arthur Moore 2024 MIT License
""" """
from pathlib import Path from pathlib import Path
from openscad_runner import * from openscad_runner import *
import subprocess
import unittest import unittest
class TestHoles(unittest.TestCase): class TestHoleCutouts(unittest.TestCase):
""" """
Test Hole Cutouts. Test Hole Cutouts. The negatives used with `difference()` to create a hole.
Currently only makes sure code runs, and outputs pictures for manual verification. Currently only makes sure code runs, and outputs pictures for manual verification.
""" """
scad_file_path = Path('../gridfinity-rebuilt-holes.scad')
image_folder_base = Path('../images/hole_cutouts/')
def run_image(self, camera_args: CameraArguments, test_args: [str], image_file_name: str): @classmethod
""" def setUpClass(cls):
Run the code, to create an image. cls.scad_runner = OpenScadRunner(Path('../gridfinity-rebuilt-holes.scad'))
@Important The only verification is that no errors occured. cls.scad_runner.image_folder_base = Path('../images/hole_cutouts/')
There is no verification if the image was created, or the image contents.
"""
assert(self.scad_file_path.exists())
image_path = self.image_folder_base.joinpath(image_file_name)
command_arguments = [openscad_binary_windows] + common_arguments + \
[camera_args.as_argument()] + test_args + \
[f'-o{str(image_path)}', str(self.scad_file_path)]
print(command_arguments)
return subprocess.run(command_arguments, check=True)
def test_refined_hole(self): def test_refined_hole(self):
""" """
@ -39,47 +27,55 @@ class TestHoles(unittest.TestCase):
camera_args = CameraArguments(Vec3(0,0,0),Vec3(225,0,225),50) camera_args = CameraArguments(Vec3(0,0,0),Vec3(225,0,225),50)
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=true, magnet_hole=false, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)') 'bundle_hole_options(refined_hole=true, magnet_hole=false, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(camera_args, test_args, Path('refined_hole.png')) self.scad_runner.create_image(camera_args, test_args, Path('refined_hole.png'))
def test_plain_magnet_hole(self): def test_plain_magnet_hole(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)') 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('magnet_hole.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('magnet_hole.png'))
def test_plain_screw_hole(self): def test_plain_screw_hole(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=false, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)') 'bundle_hole_options(refined_hole=false, magnet_hole=false, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('screw_hole.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('screw_hole.png'))
def test_magnet_and_screw_hole(self): def test_magnet_and_screw_hole(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)') 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('magnet_and_screw_hole.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('magnet_and_screw_hole.png'))
def test_chamfered_magnet_hole(self): def test_chamfered_magnet_hole(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=true, supportless=false)') 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=true, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('chamfered_magnet_hole.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('chamfered_magnet_hole.png'))
def test_magnet_hole_crush_ribs(self): def test_magnet_hole_crush_ribs(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=true, chamfer=false, supportless=false)') 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=true, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('magnet_hole_crush_ribs.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('magnet_hole_crush_ribs.png'))
def test_magnet_hole_supportless(self): def test_magnet_hole_supportless(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=true)') 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=true)')
self.run_image(top_angle_camera, test_args, Path('magnet_hole_supportless.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('magnet_hole_supportless.png'))
def test_magnet_and_screw_hole_supportless(self): def test_magnet_and_screw_hole_supportless(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=true)') 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=true)')
self.run_image(top_angle_camera, test_args, Path('magnet_and_screw_hole_supportless.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('magnet_and_screw_hole_supportless.png'))
def test_all_hole_options(self): def test_all_hole_options(self):
test_args = set_variable_argument('test_options', test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=true, chamfer=true, supportless=true)') 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=true, chamfer=true, supportless=true)')
self.run_image(top_angle_camera, test_args, Path('all_hole_options.png')) self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA,
test_args, Path('all_hole_options.png'))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()