Skip to content

Commit f82160d

Browse files
RyanJWardpyansys-ci-botpre-commit-ci[bot]
authored
test: expand coverage and add bug fix test (#2103)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent bfa184d commit f82160d

File tree

7 files changed

+1059
-4
lines changed

7 files changed

+1059
-4
lines changed

doc/changelog.d/2103.test.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expand coverage and add bug fix test
Binary file not shown.

tests/integration/files/rci_std.x_t

Lines changed: 840 additions & 0 deletions
Large diffs are not rendered by default.
106 KB
Binary file not shown.

tests/integration/test_design_export.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@
2121
# SOFTWARE.
2222
"""Test design export functionality."""
2323

24+
from pathlib import Path
25+
2426
import numpy as np
2527
import pytest
2628

2729
from ansys.geometry.core import Modeler
2830
from ansys.geometry.core.connection.backend import BackendType
29-
from ansys.geometry.core.designer import Component, Design
31+
from ansys.geometry.core.designer import Component, Design, DesignFileFormat
3032
from ansys.geometry.core.math import Plane, Point2D, Point3D, UnitVector3D, Vector3D
3133
from ansys.geometry.core.sketch import Sketch
3234

33-
from .conftest import skip_if_core_service, skip_if_spaceclaim, skip_if_windows
35+
from .conftest import (
36+
FILES_DIR,
37+
skip_if_core_service,
38+
skip_if_spaceclaim,
39+
skip_if_windows,
40+
)
3441

3542

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

347354
# TODO: Check the exported file content
348355
# https://github.com/ansys/pyansys-geometry/issues/1146
356+
357+
358+
def test_import_export_reimport_design_scdocx(
359+
modeler: Modeler, tmp_path_factory: pytest.TempPathFactory
360+
):
361+
"""Test importing, exporting, and re-importing a design file."""
362+
# Define the working directory and file paths
363+
working_directory = tmp_path_factory.mktemp("test_import_export_reimport")
364+
original_file = Path(FILES_DIR, "reactorWNS.scdocx")
365+
reexported_file = Path(working_directory, "reexported.scdocx")
366+
367+
# Create a new design
368+
design = modeler.create_design("Assembly")
369+
370+
# Import the original file
371+
design.insert_file(original_file)
372+
373+
# Export the design to a new file
374+
design.download(reexported_file, format=DesignFileFormat.SCDOCX)
375+
376+
# Re-import the exported file
377+
design.insert_file(reexported_file)
378+
379+
# Assertions to check the number of components and bodies
380+
assert len(design.components) == 2
381+
assert len(design.components[0].components[0].bodies) == 3
382+
383+
384+
def test_import_export_reimport_design_x_t(
385+
modeler: Modeler, tmp_path_factory: pytest.TempPathFactory
386+
):
387+
"""Test importing, exporting, and re-importing a design file in Parasolid text format."""
388+
# Define the working directory and file paths
389+
working_directory = tmp_path_factory.mktemp("test_import_export_reimport")
390+
original_file = Path(FILES_DIR, "rci_std.x_t")
391+
reexported_file = Path(working_directory, "reexported.x_t")
392+
393+
# Create a new design
394+
design = modeler.create_design("Assembly")
395+
396+
# Import the original file
397+
design.insert_file(original_file)
398+
399+
# Export the design to a new file
400+
design.download(reexported_file, format=DesignFileFormat.PARASOLID_TEXT)
401+
402+
# Ensure the re-exported file exists
403+
assert reexported_file.exists(), f"Re-exported file {reexported_file} does not exist."
404+
405+
# Re-import the exported file
406+
design.insert_file(reexported_file)
407+
408+
# Assertions to check the number of components and bodies
409+
assert len(design.components[0].bodies) == 1
410+
assert len(design.components[1].components[0].components[0].bodies) == 1

tests/integration/test_design_import.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
from ansys.geometry.core.connection.backend import BackendType
3232
from ansys.geometry.core.designer import Component, Design
3333
from ansys.geometry.core.designer.design import DesignFileFormat
34-
from ansys.geometry.core.math import Plane, Point2D, Point3D, UnitVector3D, Vector3D
35-
from ansys.geometry.core.misc import UNITS
34+
from ansys.geometry.core.math import UNITVECTOR3D_Z, Plane, Point2D, Point3D, UnitVector3D, Vector3D
35+
from ansys.geometry.core.misc import UNITS, Distance
3636
from ansys.geometry.core.sketch import Sketch
3737
from ansys.geometry.core.tools.unsupported import PersistentIdType
3838

@@ -516,3 +516,22 @@ def test_design_import_stride_with_named_selections(modeler: Modeler):
516516
assert len(named_selection.vertices) == expected_properties["vertices"], (
517517
f"Mismatch in vertices for {named_selection.name}"
518518
)
519+
520+
521+
def test_design_insert_id_bug(modeler: Modeler):
522+
"""Test inserting a file into the design with ID bug fix."""
523+
# This fix is available in version 261 and later
524+
design1 = modeler.create_design("Test")
525+
526+
design1.add_component("test")
527+
528+
design1.insert_file(Path(FILES_DIR, "bottom_mounted_imp.dsco"))
529+
design1.components[1].bodies[0].copy(design1.components[0], "test")
530+
531+
ns = design1.create_named_selection("ComponentNS", components=[design1.components[0]])
532+
modeler.geometry_commands.move_translate(
533+
ns, direction=UNITVECTOR3D_Z, distance=Distance(1, UNITS.m)
534+
)
535+
536+
assert len(design1.components[0].bodies) == 1
537+
assert len(design1.components[1].bodies) == 1

tests/test_laucher.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import pytest
25+
26+
from ansys.geometry.core.connection.launcher import (
27+
_launch_with_automatic_detection,
28+
_launch_with_launchmode,
29+
)
30+
31+
32+
def dummy_launcher(**kwargs):
33+
"""Dummy launcher function to simulate a launch without actually doing anything."""
34+
return f"Dummy launcher called with args: {kwargs}"
35+
36+
37+
@pytest.mark.parametrize(
38+
"mode,expected_result",
39+
[
40+
("pypim", "Dummy launcher called with args: {}"),
41+
("docker", "Dummy launcher called with args: {}"),
42+
("core_service", "Dummy launcher called with args: {}"),
43+
("geometry_service", "Dummy launcher called with args: {}"),
44+
("spaceclaim", "Dummy launcher called with args: {}"),
45+
("discovery", "Dummy launcher called with args: {}"),
46+
],
47+
)
48+
def test_launch_with_launchmode_no_launch(monkeypatch, mode, expected_result):
49+
"""Test _launch_with_launchmode without actually launching anything."""
50+
51+
# Override the launch functions with the dummy_launcher
52+
monkeypatch.setattr(
53+
"ansys.geometry.core.connection.launcher.launch_remote_modeler", dummy_launcher
54+
)
55+
monkeypatch.setattr(
56+
"ansys.geometry.core.connection.launcher.launch_docker_modeler", dummy_launcher
57+
)
58+
monkeypatch.setattr(
59+
"ansys.geometry.core.connection.launcher.launch_modeler_with_core_service", dummy_launcher
60+
)
61+
monkeypatch.setattr(
62+
"ansys.geometry.core.connection.launcher.launch_modeler_with_geometry_service",
63+
dummy_launcher,
64+
)
65+
monkeypatch.setattr(
66+
"ansys.geometry.core.connection.launcher.launch_modeler_with_spaceclaim", dummy_launcher
67+
)
68+
monkeypatch.setattr(
69+
"ansys.geometry.core.connection.launcher.launch_modeler_with_discovery", dummy_launcher
70+
)
71+
72+
# Call the function and verify the result
73+
result = _launch_with_launchmode(mode)
74+
assert result == expected_result
75+
76+
77+
@pytest.mark.parametrize(
78+
"invalid_mode,expected_exception",
79+
[
80+
("invalid_mode", ValueError),
81+
("", ValueError),
82+
(None, TypeError), # Expect TypeError for None
83+
],
84+
)
85+
def test_launch_with_launchmode_invalid_mode(monkeypatch, invalid_mode, expected_exception):
86+
"""Test _launch_with_launchmode with invalid modes."""
87+
with pytest.raises(
88+
expected_exception, match="Invalid launch mode|The launch mode must be a string"
89+
):
90+
_launch_with_launchmode(invalid_mode)
91+
92+
93+
def dummy_launch_docker_modeler(**kwargs):
94+
"""Dummy function to simulate launching the Docker modeler."""
95+
return "Dummy Docker Modeler Launched"
96+
97+
98+
def test_launch_with_docker_detection(monkeypatch):
99+
"""Test Docker detection and fallback logic without launching anything."""
100+
101+
# Simulate Docker being available
102+
monkeypatch.setattr("ansys.geometry.core.connection.docker_instance._HAS_DOCKER", True)
103+
monkeypatch.setattr(
104+
"ansys.geometry.core.connection.docker_instance.LocalDockerInstance.is_docker_installed",
105+
lambda: True,
106+
)
107+
108+
# Replace the launch_docker_modeler function with a dummy function
109+
monkeypatch.setattr(
110+
"ansys.geometry.core.connection.launcher.launch_docker_modeler",
111+
dummy_launch_docker_modeler,
112+
)
113+
114+
# Call the function and verify the result
115+
result = _launch_with_automatic_detection()
116+
assert result == "Dummy Docker Modeler Launched"
117+
118+
119+
def test_docker_not_available(monkeypatch):
120+
"""Test fallback logic when Docker is not available."""
121+
122+
# Simulate Docker not being available
123+
monkeypatch.setattr("ansys.geometry.core.connection.docker_instance._HAS_DOCKER", False)
124+
125+
# Replace the launch_docker_modeler function with a dummy function
126+
monkeypatch.setattr(
127+
"ansys.geometry.core.connection.launcher.launch_docker_modeler",
128+
dummy_launch_docker_modeler,
129+
)
130+
131+
# Call the function and verify that it raises NotImplementedError
132+
with pytest.raises(NotImplementedError, match="Geometry service cannot be initialized."):
133+
_launch_with_automatic_detection()

0 commit comments

Comments
 (0)