diff --git a/doc/changelog.d/2070.test.md b/doc/changelog.d/2070.test.md new file mode 100644 index 0000000000..9667bec3a9 --- /dev/null +++ b/doc/changelog.d/2070.test.md @@ -0,0 +1 @@ +Adding test coverage for designer, sketch, misc \ No newline at end of file diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 9e24cd9e53..c6cad77fa4 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -204,3 +204,14 @@ def use_service_colors(): # Code here runs after the test, reverting the state pyansys_geometry.USE_SERVICE_COLORS = False + + +@pytest.fixture(scope="function") +def use_grpc_client_old_backend(modeler: Modeler): + currentbackend = modeler._grpc_client._backend_version + modeler._grpc_client._backend_version = (24, 2, 0) + + yield # This allows the test to run + + # Code here runs after the test, reverting the state + modeler._grpc_client._backend_version = currentbackend diff --git a/tests/integration/files/25R1BasicBoxNameSelection.scdocx b/tests/integration/files/25R1BasicBoxNameSelection.scdocx new file mode 100644 index 0000000000..77e3f68fd8 Binary files /dev/null and b/tests/integration/files/25R1BasicBoxNameSelection.scdocx differ diff --git a/tests/integration/test_design.py b/tests/integration/test_design.py index 1e7220a0e5..80a901cf42 100644 --- a/tests/integration/test_design.py +++ b/tests/integration/test_design.py @@ -22,6 +22,7 @@ """Test design interaction.""" import os +from pathlib import Path import matplotlib.colors as mcolors import numpy as np @@ -40,7 +41,7 @@ from ansys.geometry.core.designer.body import CollisionType, FillStyle, MasterBody from ansys.geometry.core.designer.face import FaceLoopType from ansys.geometry.core.designer.part import MasterComponent, Part -from ansys.geometry.core.errors import GeometryExitedError +from ansys.geometry.core.errors import GeometryExitedError, GeometryRuntimeError from ansys.geometry.core.materials import Material, MaterialProperty, MaterialPropertyType from ansys.geometry.core.math import ( IDENTITY_MATRIX44, @@ -54,7 +55,7 @@ UnitVector3D, Vector3D, ) -from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Accuracy, Angle, Distance +from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Accuracy, Angle, Distance, checks from ansys.geometry.core.misc.auxiliary import DEFAULT_COLOR from ansys.geometry.core.parameters.parameter import ParameterType, ParameterUpdateStatus from ansys.geometry.core.shapes import ( @@ -77,6 +78,20 @@ from .conftest import FILES_DIR +def test_design_is_close(modeler: Modeler): + # Testing to see if design is closed and whether more operations can be performed on it + sketch = Sketch() + sketch.box(Point2D([0, 0]), 10, 10) + design = modeler.create_design("Box") + design.extrude_sketch("Box", sketch, 2) + design.close() + with pytest.raises( + GeometryRuntimeError, + match="The design has been closed on the backend. Cannot perform any operations on it.", + ): + checks.ensure_design_is_active(design.bodies[0].edges) + + def test_design_selection(modeler: Modeler): """Test to validate the designer selection for edges and __repr__ method.""" sketch = Sketch() @@ -490,6 +505,13 @@ def test_named_selections(modeler: Modeler): assert len(design.named_selections) == 3 +def test_old_backend_version(modeler: Modeler, use_grpc_client_old_backend: Modeler): + # Try to vefify name selection using earlier backend version + design = modeler.open_file(Path(FILES_DIR, "25R1BasicBoxNameSelection.scdocx")) + hello = design.named_selections + assert hello[0].faces == [] + + def test_empty_named_selection(modeler: Modeler): """Test for verifying the creation of an empty ``NamedSelection``.""" # Create your design on the server side diff --git a/tests/test_misc_checks.py b/tests/test_misc_checks.py index 0f690418f1..6715aca587 100644 --- a/tests/test_misc_checks.py +++ b/tests/test_misc_checks.py @@ -29,6 +29,8 @@ from ansys.geometry.core.math import Point3D from ansys.geometry.core.misc import ( UNITS, + TessellationOptions, + auxiliary, check_is_float_int, check_ndarray_is_float_int, check_ndarray_is_non_zero, @@ -42,6 +44,44 @@ ) +def test_tessellation_options(): + # Testing tessellation options + tessellation_options = TessellationOptions( + surface_deviation=0.01, + angle_deviation=0.1, + max_aspect_ratio=2.0, + max_edge_length=5.0, + watertight=True, + ) + assert tessellation_options.surface_deviation == 0.01 + assert tessellation_options.angle_deviation == 0.1 + assert tessellation_options.max_aspect_ratio == 2.0 + assert tessellation_options.max_edge_length == 5.0 + assert tessellation_options.watertight is True + + +def test_misc_checks(): + # Testing backend_version_decorator for log warning + @min_backend_version(25, 2, 0) + def fake_temp_operation(obj): + print("Performing operation...") + + invalid_object = {"key": "value"} + fake_temp_operation(invalid_object) + + +def test_check_auxiliary(): + """Test the auxiliary functions for checking color conversion.""" + with pytest.raises(ValueError, match="RGB values in the 0-1 range must be floats."): + auxiliary.convert_color_to_hex(tuple([0, 1, 0])) + with pytest.raises(ValueError, match="RGB values in the 0-255 range must be integers."): + auxiliary.convert_color_to_hex(tuple([1.1, 1.1, 1.1, 1.1])) + with pytest.raises(ValueError, match="RGB tuple contains mixed ranges or invalid values."): + auxiliary.convert_color_to_hex(tuple([256, 11, 1.1])) + with pytest.raises(ValueError, match="Invalid color value:."): + auxiliary.convert_color_to_hex((125, 128)) + + def test_check_type(): """Test that the __eq__ check is working properly. diff --git a/tests/test_misc_measurements.py b/tests/test_misc_measurements.py index fb398da11d..c96478e2ca 100644 --- a/tests/test_misc_measurements.py +++ b/tests/test_misc_measurements.py @@ -23,7 +23,13 @@ from pint import Quantity import pytest -from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Angle, Distance +from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Angle, Distance, measurements + + +def test_repr_(): + # Testing the __repr__ method of the Measurement class + mea = measurements.Measurement(5.0, DEFAULT_UNITS.LENGTH, DEFAULT_UNITS.LENGTH) + assert measurements.Measurement.__repr__(mea) == "5.0 meter" def test_distance(): diff --git a/tests/test_sketch.py b/tests/test_sketch.py index 2b1d7f1313..ae3c33c9af 100644 --- a/tests/test_sketch.py +++ b/tests/test_sketch.py @@ -43,6 +43,7 @@ SketchCircle, SketchEdge, SketchEllipse, + SketchFace, SketchSegment, Slot, SpurGear, @@ -122,8 +123,11 @@ def test_sketch_circle_plane_change(): assert circle.dir_z == Vector3D([0, -1, 0]) +@pytest.mark.skipif( + not are_graphics_available(), reason="Skipping due to graphics requirements missing" +) def test_sketch_face(): - """Test the sketch face perimeter and change plane functionality""" + """Test the sketch face perimeter, change plane, and visualization polydata functionality""" sketch = Sketch() per = Triangle(Point2D([10, 10]), Point2D([2, 1]), Point2D([10, -10])).perimeter assert abs((per - 45.64306508752774 * UNITS.m).m) <= 5e-14 @@ -134,6 +138,18 @@ def test_sketch_face(): ) sketch.faces[0].plane_change(new_plane) + start_point = Point2D([0, 0], unit=UNITS.meter) + end_point = Point2D([5, 5], unit=UNITS.kilometer) + segment1 = SketchSegment(start_point, end_point) + sketch1 = SketchFace() + sketch1._edges = [segment1] + polyda = sketch1.visualization_polydata + assert polyda.center == pytest.approx( + ([2500.0, 2500.0, 0.0]), + rel=1e-7, + abs=1e-8, + ) + def test_sketch_edge(): """Test the sketch edge functionality"""