Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/2108.test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding test coverage for modeler, plotting, logger, and parameters
11 changes: 11 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,14 @@ def use_grpc_client_old_backend(modeler: Modeler):

# Code here runs after the test, reverting the state
modeler._grpc_client._backend_version = currentbackend


@pytest.fixture(scope="function")
def disable_active_design_check_true():
import ansys.geometry.core as pyansys_geometry

pyansys_geometry.DISABLE_ACTIVE_DESIGN_CHECK = True

yield

pyansys_geometry.DISABLE_ACTIVE_DESIGN_CHECK = False
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
57 changes: 56 additions & 1 deletion tests/integration/test_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from ansys.geometry.core import Modeler
from ansys.geometry.core.connection import BackendType
import ansys.geometry.core.connection.defaults as pygeom_defaults
from ansys.geometry.core.designer import (
CurveType,
DesignFileFormat,
Expand Down Expand Up @@ -75,7 +76,55 @@
from ansys.geometry.core.sketch import Sketch

from ..conftest import are_graphics_available
from .conftest import FILES_DIR
from .conftest import FILES_DIR, IMPORT_FILES_DIR


def test_error_opening_file(modeler: Modeler, tmp_path_factory: pytest.TempPathFactory):
"""Validating error messages when opening up files"""
fake_file_path = Path("C:\\Users\\FakeUser\\Documents\\FakeProject\\FakeFile.scdocx")
with pytest.raises(ValueError, match="Could not find file:"):
modeler._upload_file(fake_file_path)
file = tmp_path_factory.mktemp("test_design")
with pytest.raises(ValueError, match="File path must lead to a file, not a directory"):
modeler._upload_file(file)
fake_file_path = Path("C:\\Users\\FakeUser\\Documents\\FakeProject\\FakeFile.scdocx")
with pytest.raises(ValueError, match="Could not find file:"):
modeler._upload_file_stream(fake_file_path)
file = tmp_path_factory.mktemp("test_design")
with pytest.raises(ValueError, match="File path must lead to a file, not a directory"):
modeler._upload_file_stream(file)


def test_modeler_open_files(modeler: Modeler):
"""Test the modeler open files for assembly files and for too small of files."""
catiafile = Path(IMPORT_FILES_DIR, "CAT5/Top_Down_Assembly_SC_CATv5.CATProduct")
design = modeler.open_file(catiafile)
design.close()
old_value = pygeom_defaults.MAX_MESSAGE_LENGTH
try:
pygeom_defaults.MAX_MESSAGE_LENGTH = 1024 # 0.5 MB
with pytest.raises(
GeometryExitedError,
match="Geometry service connection terminated: Exception iterating requests!",
):
design = modeler.open_file(catiafile)
pygeom_defaults.MAX_MESSAGE_LENGTH = 10 # 0.5 MB
with pytest.raises(
GeometryExitedError,
match="Geometry service connection terminated: Exception iterating requests!",
):
design = modeler.open_file(Path(FILES_DIR, "InspectAndRepair01.scdocx"))
finally:
pygeom_defaults.MAX_MESSAGE_LENGTH = old_value


def test_disable_active_design_check(modeler: Modeler, disable_active_design_check_true: None):
"""Validating test for disabling active design check."""
sketch = Sketch()
sketch.box(Point2D([0, 0]), 10, 10)
design = modeler.create_design("Box")
design.extrude_sketch("Box", sketch, 2)
checks.ensure_design_is_active(design.bodies[0].edges)


def test_design_is_close(modeler: Modeler):
Expand Down Expand Up @@ -3107,6 +3156,12 @@ def test_design_parameters(modeler: Modeler):
status = design.set_parameter(test_parameters[0])
assert status == ParameterUpdateStatus.CONSTRAINED_PARAMETERS

test_parameters[0].name = "NewName"
assert test_parameters[0].name == "NewName"

test_parameters[0].dimension_type = ParameterType.DIMENSIONTYPE_AREA
assert test_parameters[0].dimension_type == ParameterType.DIMENSIONTYPE_AREA


def test_cached_bodies(modeler: Modeler):
"""Test that bodies are cached correctly.
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
Trapezoid,
Triangle,
)
from ansys.tools.visualization_interface import MeshObjectPlot

skip_no_xserver = pytest.mark.skipif(
not system_supports_plotting(), reason="Requires active X Server"
Expand All @@ -68,6 +69,44 @@
IMAGE_RESULTS_DIR = Path(Path(__file__).parent, "image_cache", "results")


@skip_no_xserver
def test_adding_to_plotter(modeler: Modeler, verify_image_cache):
"""Testing out clipping plane, adding body edges and face,
and if body is suppressed to an existing plotter"""
plotter = GeometryPlotter(allow_picking=True)
plane = Plane(origin=[0, 0, 0], direction_x=[1, 0, 0], direction_y=[0, 1, 0])
box_plane = Sketch(plane=plane)
plotting_options = {"clipping_plane": True}
plotter.add_sketch(box_plane, show_plane=True, show_frame=True, **plotting_options)
design = modeler.create_design("Box")
box_plane.box(Point2D([0.0, 0.0]), width=1, height=1)
box = design.extrude_sketch("Box", box_plane, 1)
box_plot = MeshObjectPlot(box, mesh=None)
plotter.add_body_edges(box_plot)
box.set_suppressed(True)
plotter.add_body(box)
box.set_suppressed(False)
plotter.add_body(box)
plotter.add_face(box.faces[0])
plotter.show(screenshot=Path(IMAGE_RESULTS_DIR, "adding_to_plotter.png"))


@skip_no_xserver
def test_different_color_than_default(modeler: Modeler, verify_image_cache):
"""Testing out adding a face to the plotter with a different color than the default."""
design = modeler.create_design("Box")
plane = Plane(origin=[0, 0, 0], direction_x=[1, 0, 0], direction_y=[0, 1, 0])
box_plane = Sketch(plane=plane)
box_plane.box(Point2D([0.0, 0.0]), width=1, height=1)
box = design.extrude_sketch("Box", box_plane, 1)
plotter2 = GeometryPlotter(allow_picking=True, use_service_colors=True)
box.faces[0].color = "blue"
plotter2.add_face(box.faces[0])
plotter2.show(
plotting_object=box, screenshot=Path(IMAGE_RESULTS_DIR, "different_color_than_default.png")
)


@skip_no_xserver
def test_plot_body(modeler: Modeler, verify_image_cache):
"""Test plotting of the body."""
Expand Down Expand Up @@ -953,6 +992,9 @@ def test_export_glb(modeler: Modeler):
output_glb_path = Path(IMAGE_RESULTS_DIR, "plot_box_glb")
pl.export_glb(filename=output_glb_path)

tempglb = pl.export_glb(filename=None)
assert tempglb.exists()

# Add suffix to the output path
output_glb_path = output_glb_path.with_suffix(".glb")
assert output_glb_path.exists(), "GLB file was not created successfully."
Expand Down
57 changes: 57 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import pytest

from ansys.geometry.core import LOG # Global logger
from ansys.geometry.core.connection import GrpcClient
import ansys.geometry.core.logger as logger

## Notes
Expand All @@ -38,6 +39,62 @@
LOG_LEVELS = {"CRITICAL": 50, "ERROR": 40, "WARNING": 30, "INFO": 20, "DEBUG": 10}


def test_add_instance():
"""Testing adding an instance logger while checking if log has certain key"""
base_name = "root"
instance_logger_1 = LOG.add_instance_logger(
name=base_name, client_instance=GrpcClient(), level=10
)
instance_logger_1.info("This is a message from the first instance logger.")
with pytest.raises(KeyError, match="There is no instances with name root_4."):
LOG.__getitem__("root_4")


def test_custom_and_child_log(tmp_path_factory: pytest.TempPathFactory):
"""Testing out writing a child log and adding std handler to it"""
custom_filename = tmp_path_factory.mktemp("custom_geometry") / "custom_geometry.log"
logger1 = logger.Logger(level="DEBUG", to_file=True, filename=custom_filename)
child_log = logger1._make_child_logger(suffix="ChildLogger", level="INFO")
child_log.info("This is a test to child logger")
child_log1 = logger1._make_child_logger(suffix="ChildLogger1", level=None)
child_log1.info("This is a test to child logger")
logger1.add_child_logger(suffix="ChildLogger", level="INFO")
logger.add_stdout_handler(child_log, level=logger.logging.INFO, write_headers=True)
for handler in logger1.logger.handlers:
if isinstance(handler, logger.logging.FileHandler):
handler.close()


def test_stdout_defined():
"""Testing if stdout is defined already and giving a custom format"""
logger_adapter = logger.PyGeometryCustomAdapter(LOG)
with pytest.raises(
Exception,
match="Stdout logger is already defined.",
):
logger_adapter.log_to_stdout(level=logger.LOG_LEVEL)
formatter = logger.PyGeometryFormatter(fmt="%(levelname)s - %(instance_name)s - %(message)s")
stream_handler = logger.logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger_adapter.logger.handlers = [stream_handler]
logger_adapter.setLevel("ERROR")
defaults = {
"instance_name": "DefaultInstance",
"module": "DefaultModule",
"funcName": "DefaultFunction",
}
formatter = logger.PyGeometryFormatter(
fmt="%(levelname)s - %(instance_name)s - %(module)s - %(funcName)s - %(message)s",
defaults=defaults,
)
logger1 = logger.logging.getLogger("CustomLogger")
logger1.setLevel(logger.logging.DEBUG)
stream_handler = logger.logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger1.addHandler(stream_handler)
logger1.info("This is a test message without extra fields.")


def test_stdout_reading(capfd: pytest.CaptureFixture):
"""Test for checking simple standard output reading by pytest.

Expand Down
Loading