Skip to content

Commit da919ad

Browse files
syscordanpyansys-ci-botRobPasMue
authored
test: adding test coverage for designer, sketch, misc (#2070)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com>
1 parent 12242ce commit da919ad

File tree

7 files changed

+100
-4
lines changed

7 files changed

+100
-4
lines changed

doc/changelog.d/2070.test.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adding test coverage for designer, sketch, misc

tests/integration/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,14 @@ def use_service_colors():
204204

205205
# Code here runs after the test, reverting the state
206206
pyansys_geometry.USE_SERVICE_COLORS = False
207+
208+
209+
@pytest.fixture(scope="function")
210+
def use_grpc_client_old_backend(modeler: Modeler):
211+
currentbackend = modeler._grpc_client._backend_version
212+
modeler._grpc_client._backend_version = (24, 2, 0)
213+
214+
yield # This allows the test to run
215+
216+
# Code here runs after the test, reverting the state
217+
modeler._grpc_client._backend_version = currentbackend
Binary file not shown.

tests/integration/test_design.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""Test design interaction."""
2323

2424
import os
25+
from pathlib import Path
2526

2627
import matplotlib.colors as mcolors
2728
import numpy as np
@@ -40,7 +41,7 @@
4041
from ansys.geometry.core.designer.body import CollisionType, FillStyle, MasterBody
4142
from ansys.geometry.core.designer.face import FaceLoopType
4243
from ansys.geometry.core.designer.part import MasterComponent, Part
43-
from ansys.geometry.core.errors import GeometryExitedError
44+
from ansys.geometry.core.errors import GeometryExitedError, GeometryRuntimeError
4445
from ansys.geometry.core.materials import Material, MaterialProperty, MaterialPropertyType
4546
from ansys.geometry.core.math import (
4647
IDENTITY_MATRIX44,
@@ -54,7 +55,7 @@
5455
UnitVector3D,
5556
Vector3D,
5657
)
57-
from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Accuracy, Angle, Distance
58+
from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Accuracy, Angle, Distance, checks
5859
from ansys.geometry.core.misc.auxiliary import DEFAULT_COLOR
5960
from ansys.geometry.core.parameters.parameter import ParameterType, ParameterUpdateStatus
6061
from ansys.geometry.core.shapes import (
@@ -77,6 +78,20 @@
7778
from .conftest import FILES_DIR
7879

7980

81+
def test_design_is_close(modeler: Modeler):
82+
# Testing to see if design is closed and whether more operations can be performed on it
83+
sketch = Sketch()
84+
sketch.box(Point2D([0, 0]), 10, 10)
85+
design = modeler.create_design("Box")
86+
design.extrude_sketch("Box", sketch, 2)
87+
design.close()
88+
with pytest.raises(
89+
GeometryRuntimeError,
90+
match="The design has been closed on the backend. Cannot perform any operations on it.",
91+
):
92+
checks.ensure_design_is_active(design.bodies[0].edges)
93+
94+
8095
def test_design_selection(modeler: Modeler):
8196
"""Test to validate the designer selection for edges and __repr__ method."""
8297
sketch = Sketch()
@@ -490,6 +505,13 @@ def test_named_selections(modeler: Modeler):
490505
assert len(design.named_selections) == 3
491506

492507

508+
def test_old_backend_version(modeler: Modeler, use_grpc_client_old_backend: Modeler):
509+
# Try to vefify name selection using earlier backend version
510+
design = modeler.open_file(Path(FILES_DIR, "25R1BasicBoxNameSelection.scdocx"))
511+
hello = design.named_selections
512+
assert hello[0].faces == []
513+
514+
493515
def test_empty_named_selection(modeler: Modeler):
494516
"""Test for verifying the creation of an empty ``NamedSelection``."""
495517
# Create your design on the server side

tests/test_misc_checks.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from ansys.geometry.core.math import Point3D
3030
from ansys.geometry.core.misc import (
3131
UNITS,
32+
TessellationOptions,
33+
auxiliary,
3234
check_is_float_int,
3335
check_ndarray_is_float_int,
3436
check_ndarray_is_non_zero,
@@ -42,6 +44,44 @@
4244
)
4345

4446

47+
def test_tessellation_options():
48+
# Testing tessellation options
49+
tessellation_options = TessellationOptions(
50+
surface_deviation=0.01,
51+
angle_deviation=0.1,
52+
max_aspect_ratio=2.0,
53+
max_edge_length=5.0,
54+
watertight=True,
55+
)
56+
assert tessellation_options.surface_deviation == 0.01
57+
assert tessellation_options.angle_deviation == 0.1
58+
assert tessellation_options.max_aspect_ratio == 2.0
59+
assert tessellation_options.max_edge_length == 5.0
60+
assert tessellation_options.watertight is True
61+
62+
63+
def test_misc_checks():
64+
# Testing backend_version_decorator for log warning
65+
@min_backend_version(25, 2, 0)
66+
def fake_temp_operation(obj):
67+
print("Performing operation...")
68+
69+
invalid_object = {"key": "value"}
70+
fake_temp_operation(invalid_object)
71+
72+
73+
def test_check_auxiliary():
74+
"""Test the auxiliary functions for checking color conversion."""
75+
with pytest.raises(ValueError, match="RGB values in the 0-1 range must be floats."):
76+
auxiliary.convert_color_to_hex(tuple([0, 1, 0]))
77+
with pytest.raises(ValueError, match="RGB values in the 0-255 range must be integers."):
78+
auxiliary.convert_color_to_hex(tuple([1.1, 1.1, 1.1, 1.1]))
79+
with pytest.raises(ValueError, match="RGB tuple contains mixed ranges or invalid values."):
80+
auxiliary.convert_color_to_hex(tuple([256, 11, 1.1]))
81+
with pytest.raises(ValueError, match="Invalid color value:."):
82+
auxiliary.convert_color_to_hex((125, 128))
83+
84+
4585
def test_check_type():
4686
"""Test that the __eq__ check is working properly.
4787

tests/test_misc_measurements.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
from pint import Quantity
2424
import pytest
2525

26-
from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Angle, Distance
26+
from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Angle, Distance, measurements
27+
28+
29+
def test_repr_():
30+
# Testing the __repr__ method of the Measurement class
31+
mea = measurements.Measurement(5.0, DEFAULT_UNITS.LENGTH, DEFAULT_UNITS.LENGTH)
32+
assert measurements.Measurement.__repr__(mea) == "5.0 meter"
2733

2834

2935
def test_distance():

tests/test_sketch.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
SketchCircle,
4444
SketchEdge,
4545
SketchEllipse,
46+
SketchFace,
4647
SketchSegment,
4748
Slot,
4849
SpurGear,
@@ -122,8 +123,11 @@ def test_sketch_circle_plane_change():
122123
assert circle.dir_z == Vector3D([0, -1, 0])
123124

124125

126+
@pytest.mark.skipif(
127+
not are_graphics_available(), reason="Skipping due to graphics requirements missing"
128+
)
125129
def test_sketch_face():
126-
"""Test the sketch face perimeter and change plane functionality"""
130+
"""Test the sketch face perimeter, change plane, and visualization polydata functionality"""
127131
sketch = Sketch()
128132
per = Triangle(Point2D([10, 10]), Point2D([2, 1]), Point2D([10, -10])).perimeter
129133
assert abs((per - 45.64306508752774 * UNITS.m).m) <= 5e-14
@@ -134,6 +138,18 @@ def test_sketch_face():
134138
)
135139
sketch.faces[0].plane_change(new_plane)
136140

141+
start_point = Point2D([0, 0], unit=UNITS.meter)
142+
end_point = Point2D([5, 5], unit=UNITS.kilometer)
143+
segment1 = SketchSegment(start_point, end_point)
144+
sketch1 = SketchFace()
145+
sketch1._edges = [segment1]
146+
polyda = sketch1.visualization_polydata
147+
assert polyda.center == pytest.approx(
148+
([2500.0, 2500.0, 0.0]),
149+
rel=1e-7,
150+
abs=1e-8,
151+
)
152+
137153

138154
def test_sketch_edge():
139155
"""Test the sketch edge functionality"""

0 commit comments

Comments
 (0)