Skip to content

test: expand coverage and add bug fix test #2103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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/2103.test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expand coverage and add bug fix test
Binary file not shown.
840 changes: 840 additions & 0 deletions tests/integration/files/rci_std.x_t

Large diffs are not rendered by default.

Binary file added tests/integration/files/reactorWNS.scdocx
Binary file not shown.
66 changes: 64 additions & 2 deletions tests/integration/test_design_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@
# SOFTWARE.
"""Test design export functionality."""

from pathlib import Path

import numpy as np
import pytest

from ansys.geometry.core import Modeler
from ansys.geometry.core.connection.backend import BackendType
from ansys.geometry.core.designer import Component, Design
from ansys.geometry.core.designer import Component, Design, DesignFileFormat
from ansys.geometry.core.math import Plane, Point2D, Point3D, UnitVector3D, Vector3D
from ansys.geometry.core.sketch import Sketch

from .conftest import skip_if_core_service, skip_if_spaceclaim, skip_if_windows
from .conftest import (
FILES_DIR,
skip_if_core_service,
skip_if_spaceclaim,
skip_if_windows,
)


def _create_demo_design(modeler: Modeler) -> Design:
Expand Down Expand Up @@ -346,3 +353,58 @@ def test_export_to_pmdb(modeler: Modeler, tmp_path_factory: pytest.TempPathFacto

# TODO: Check the exported file content
# https://github.com/ansys/pyansys-geometry/issues/1146


def test_import_export_reimport_design_scdocx(
modeler: Modeler, tmp_path_factory: pytest.TempPathFactory
):
"""Test importing, exporting, and re-importing a design file."""
# Define the working directory and file paths
working_directory = tmp_path_factory.mktemp("test_import_export_reimport")
original_file = Path(FILES_DIR, "reactorWNS.scdocx")
reexported_file = Path(working_directory, "reexported.scdocx")

# Create a new design
design = modeler.create_design("Assembly")

# Import the original file
design.insert_file(original_file)

# Export the design to a new file
design.download(reexported_file, format=DesignFileFormat.SCDOCX)

# Re-import the exported file
design.insert_file(reexported_file)

# Assertions to check the number of components and bodies
assert len(design.components) == 2
assert len(design.components[0].components[0].bodies) == 3


def test_import_export_reimport_design_x_t(
modeler: Modeler, tmp_path_factory: pytest.TempPathFactory
):
"""Test importing, exporting, and re-importing a design file in Parasolid text format."""
# Define the working directory and file paths
working_directory = tmp_path_factory.mktemp("test_import_export_reimport")
original_file = Path(FILES_DIR, "rci_std.x_t")
reexported_file = Path(working_directory, "reexported.x_t")

# Create a new design
design = modeler.create_design("Assembly")

# Import the original file
design.insert_file(original_file)

# Export the design to a new file
design.download(reexported_file, format=DesignFileFormat.PARASOLID_TEXT)

# Ensure the re-exported file exists
assert reexported_file.exists(), f"Re-exported file {reexported_file} does not exist."

# Re-import the exported file
design.insert_file(reexported_file)

# Assertions to check the number of components and bodies
assert len(design.components[0].bodies) == 1
assert len(design.components[1].components[0].components[0].bodies) == 1
23 changes: 21 additions & 2 deletions tests/integration/test_design_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from ansys.geometry.core.connection.backend import BackendType
from ansys.geometry.core.designer import Component, Design
from ansys.geometry.core.designer.design import DesignFileFormat
from ansys.geometry.core.math import Plane, Point2D, Point3D, UnitVector3D, Vector3D
from ansys.geometry.core.misc import UNITS
from ansys.geometry.core.math import UNITVECTOR3D_Z, Plane, Point2D, Point3D, UnitVector3D, Vector3D
from ansys.geometry.core.misc import UNITS, Distance
from ansys.geometry.core.sketch import Sketch
from ansys.geometry.core.tools.unsupported import PersistentIdType

Expand Down Expand Up @@ -516,3 +516,22 @@ def test_design_import_stride_with_named_selections(modeler: Modeler):
assert len(named_selection.vertices) == expected_properties["vertices"], (
f"Mismatch in vertices for {named_selection.name}"
)


def test_design_insert_id_bug(modeler: Modeler):
"""Test inserting a file into the design with ID bug fix."""
# This fix is available in version 261 and later
design1 = modeler.create_design("Test")

design1.add_component("test")

design1.insert_file(Path(FILES_DIR, "bottom_mounted_imp.dsco"))
design1.components[1].bodies[0].copy(design1.components[0], "test")

ns = design1.create_named_selection("ComponentNS", components=[design1.components[0]])
modeler.geometry_commands.move_translate(
ns, direction=UNITVECTOR3D_Z, distance=Distance(1, UNITS.m)
)

assert len(design1.components[0].bodies) == 1
assert len(design1.components[1].bodies) == 1
133 changes: 133 additions & 0 deletions tests/test_laucher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import pytest

from ansys.geometry.core.connection.launcher import (
_launch_with_automatic_detection,
_launch_with_launchmode,
)


def dummy_launcher(**kwargs):
"""Dummy launcher function to simulate a launch without actually doing anything."""
return f"Dummy launcher called with args: {kwargs}"


@pytest.mark.parametrize(
"mode,expected_result",
[
("pypim", "Dummy launcher called with args: {}"),
("docker", "Dummy launcher called with args: {}"),
("core_service", "Dummy launcher called with args: {}"),
("geometry_service", "Dummy launcher called with args: {}"),
("spaceclaim", "Dummy launcher called with args: {}"),
("discovery", "Dummy launcher called with args: {}"),
],
)
def test_launch_with_launchmode_no_launch(monkeypatch, mode, expected_result):
"""Test _launch_with_launchmode without actually launching anything."""

# Override the launch functions with the dummy_launcher
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_remote_modeler", dummy_launcher
)
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_docker_modeler", dummy_launcher
)
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_modeler_with_core_service", dummy_launcher
)
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_modeler_with_geometry_service",
dummy_launcher,
)
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_modeler_with_spaceclaim", dummy_launcher
)
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_modeler_with_discovery", dummy_launcher
)

# Call the function and verify the result
result = _launch_with_launchmode(mode)
assert result == expected_result


@pytest.mark.parametrize(
"invalid_mode,expected_exception",
[
("invalid_mode", ValueError),
("", ValueError),
(None, TypeError), # Expect TypeError for None
],
)
def test_launch_with_launchmode_invalid_mode(monkeypatch, invalid_mode, expected_exception):
"""Test _launch_with_launchmode with invalid modes."""
with pytest.raises(
expected_exception, match="Invalid launch mode|The launch mode must be a string"
):
_launch_with_launchmode(invalid_mode)


def dummy_launch_docker_modeler(**kwargs):
"""Dummy function to simulate launching the Docker modeler."""
return "Dummy Docker Modeler Launched"


def test_launch_with_docker_detection(monkeypatch):
"""Test Docker detection and fallback logic without launching anything."""

# Simulate Docker being available
monkeypatch.setattr("ansys.geometry.core.connection.docker_instance._HAS_DOCKER", True)
monkeypatch.setattr(
"ansys.geometry.core.connection.docker_instance.LocalDockerInstance.is_docker_installed",
lambda: True,
)

# Replace the launch_docker_modeler function with a dummy function
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_docker_modeler",
dummy_launch_docker_modeler,
)

# Call the function and verify the result
result = _launch_with_automatic_detection()
assert result == "Dummy Docker Modeler Launched"


def test_docker_not_available(monkeypatch):
"""Test fallback logic when Docker is not available."""

# Simulate Docker not being available
monkeypatch.setattr("ansys.geometry.core.connection.docker_instance._HAS_DOCKER", False)

# Replace the launch_docker_modeler function with a dummy function
monkeypatch.setattr(
"ansys.geometry.core.connection.launcher.launch_docker_modeler",
dummy_launch_docker_modeler,
)

# Call the function and verify that it raises NotImplementedError
with pytest.raises(NotImplementedError, match="Geometry service cannot be initialized."):
_launch_with_automatic_detection()
Loading