Skip to content

Commit

Permalink
Merge branch 'main' into fix/create_volume_plot
Browse files Browse the repository at this point in the history
  • Loading branch information
gmalinve authored Oct 8, 2024
2 parents 951234a + 48c59c4 commit eb5d293
Show file tree
Hide file tree
Showing 24 changed files with 545 additions and 224 deletions.
4 changes: 0 additions & 4 deletions _unittest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,13 @@
from ansys.aedt.core.generic.filesystem import Scratch
from ansys.aedt.core.generic.general_methods import generate_unique_name
from ansys.aedt.core.generic.general_methods import inside_desktop
from ansys.aedt.core.misc.misc import list_installed_ansysem

local_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(local_path)

# Initialize default desktop configuration
default_version = "2024.2"

if "ANSYSEM_ROOT{}".format(default_version[2:].replace(".", "")) not in list_installed_ansysem():
default_version = list_installed_ansysem()[0][12:].replace(".", "")
default_version = "20{}.{}".format(default_version[:2], default_version[-1])

if inside_desktop and "oDesktop" in dir(sys.modules["__main__"]):
default_version = sys.modules["__main__"].oDesktop.GetVersion()[0:6]
Expand Down
146 changes: 146 additions & 0 deletions _unittest/test_aedt_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2024 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 os
from unittest import mock

from ansys.aedt.core.generic.aedt_versions import AedtVersions
import pytest


@pytest.fixture(scope="module", autouse=True)
def desktop():
"""Override the desktop fixture to DO NOT open the Desktop when running this test class"""
return


@pytest.fixture
def mock_os_environ():
"""Fixture to mock os.environ."""
with mock.patch.dict(
os.environ,
{
"ANSYSEM_ROOT241": r"C:\Program Files\AnsysEM\v241\ANSYS",
"ANSYSEM_ROOT242": r"C:\Program Files\AnsysEM\v242\ANSYS",
"ANSYSEM_ROOT251": r"C:\Program Files\AnsysEM\v251\ANSYS",
"ANSYSEMSV_ROOT241": r"C:\Program Files\AnsysEM\v241SV\ANSYS",
"ANSYSEMSV_ROOT242": r"C:\Program Files\AnsysEM\v242SV\ANSYS",
"ANSYSEMSV_ROOT251": r"C:\Program Files\AnsysEM\v251SV\ANSYS",
"ANSYSEM_PY_CLIENT_ROOT242": r"C:\Program Files\AnsysEM\v242CLIENT\ANSYS",
"ANSYSEM_PY_CLIENT_ROOT251": r"C:\Program Files\AnsysEM\v251CLIENT\ANSYS",
},
clear=True,
):
yield # The mock will be active within the scope of each test using this fixture


@pytest.fixture
def aedt_versions():
"""Fixture to return a new instance of AedtVersions."""
return AedtVersions()


def test_list_installed_ansysem(mock_os_environ, aedt_versions):
"""Test the list_installed_ansysem function."""
result = aedt_versions.list_installed_ansysem
expected = [
"ANSYSEM_ROOT251",
"ANSYSEM_ROOT242",
"ANSYSEM_ROOT241",
"ANSYSEM_PY_CLIENT_ROOT251",
"ANSYSEM_PY_CLIENT_ROOT242",
"ANSYSEMSV_ROOT251",
"ANSYSEMSV_ROOT242",
"ANSYSEMSV_ROOT241",
]
assert result == expected


def test_installed_versions(mock_os_environ, aedt_versions):
"""Test the installed_versions function."""
result = aedt_versions.installed_versions
expected = {
"2025.1": r"C:\Program Files\AnsysEM\v251\ANSYS",
"2024.2": r"C:\Program Files\AnsysEM\v242\ANSYS",
"2024.1": r"C:\Program Files\AnsysEM\v241\ANSYS",
"2025.1CL": r"C:\Program Files\AnsysEM\v251CLIENT\ANSYS",
"2024.2CL": r"C:\Program Files\AnsysEM\v242CLIENT\ANSYS",
"2025.1SV": r"C:\Program Files\AnsysEM\v251SV\ANSYS",
"2024.2SV": r"C:\Program Files\AnsysEM\v242SV\ANSYS",
"2024.1SV": r"C:\Program Files\AnsysEM\v241SV\ANSYS",
}
assert result == expected


@mock.patch("ansys.aedt.core.generic.aedt_versions.CURRENT_STABLE_AEDT_VERSION", 2024.2)
def test_current_version_1(mock_os_environ, aedt_versions):
"""Test the current_version function."""
assert aedt_versions.current_version == "2024.2"


@mock.patch("ansys.aedt.core.generic.aedt_versions.CURRENT_STABLE_AEDT_VERSION", 2023.2)
def test_current_version_2(mock_os_environ, aedt_versions):
"""Test the current_version function."""
assert aedt_versions.current_version == ""


@mock.patch("ansys.aedt.core.generic.aedt_versions.CURRENT_STABLE_AEDT_VERSION", 2024.2)
def test_current_student_version_1(mock_os_environ, aedt_versions):
"""Test the current_student_version function."""
assert aedt_versions.current_student_version == "2024.2SV"


@mock.patch("ansys.aedt.core.generic.aedt_versions.CURRENT_STABLE_AEDT_VERSION", 2023.2)
def test_current_student_version_2(mock_os_environ, aedt_versions):
"""Test the current_student_version function."""
assert aedt_versions.current_student_version == ""


@mock.patch("ansys.aedt.core.generic.aedt_versions.CURRENT_STABLE_AEDT_VERSION", 2024.2)
def test_latest_version_1(mock_os_environ, aedt_versions):
"""Test the current_student_version function."""
assert aedt_versions.latest_version == "2025.1"


@mock.patch("ansys.aedt.core.generic.aedt_versions.CURRENT_STABLE_AEDT_VERSION", 2023.2)
def test_latest_version_2(mock_os_environ, aedt_versions):
"""Test the current_student_version function."""
assert aedt_versions.latest_version == "2025.1"


def test_get_version_env_variable(aedt_versions):
# Test case 1: Version < 20, release < 3
version_id = "2018.2"
expected_output = "ANSYSEM_ROOT192"
assert aedt_versions.get_version_env_variable(version_id) == expected_output

# Test case 2: Version < 20, release >= 3
version_id = "2019.3"
expected_output = "ANSYSEM_ROOT195"
assert aedt_versions.get_version_env_variable(version_id) == expected_output

# Test case 3: Version >= 20
version_id = "2023.2"
expected_output = "ANSYSEM_ROOT232"
assert aedt_versions.get_version_env_variable(version_id) == expected_output
75 changes: 75 additions & 0 deletions _unittest/test_desktop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2024 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 socket
from unittest.mock import patch

from ansys.aedt.core.desktop import Desktop
from ansys.aedt.core.desktop import _find_free_port
from ansys.aedt.core.desktop import _is_port_occupied
from ansys.aedt.core.desktop import run_process
import pytest


@pytest.fixture(scope="module", autouse=True)
def desktop():
"""Override the desktop fixture to DO NOT open the Desktop when running this test class"""
return


# Test _is_port_occupied
def test_is_port_occupied_free_port():
with patch("socket.socket") as mock_socket:
mock_socket.return_value.connect.side_effect = socket.error
assert not _is_port_occupied(5000)


def test_is_port_occupied_used_port():
with patch("socket.socket") as mock_socket:
mock_socket.return_value.connect.return_value = None
assert _is_port_occupied(5000)


# Test _find_free_port
@patch("ansys.aedt.core.desktop.active_sessions", return_value={})
@patch("socket.socket")
def test_find_free_port(mock_socket, mock_active_sessions):
mock_socket.return_value.getsockname.return_value = ("127.0.0.1", 12345)
port = _find_free_port()
assert port == 12345


# Test run_process
@patch("subprocess.call")
def test_run_process(mock_call):
command = "dummy_command"
run_process(command)
mock_call.assert_called_once_with(command)


# Test Desktop.get_available_toolkits() static method
def test_get_available_toolkits():
toolkits = Desktop.get_available_toolkits()
result = ["Circuit", "HFSS", "HFSS3DLayout", "Icepak", "Maxwell3D", "Project", "TwinBuilder"]
all(elem in toolkits for elem in result)
54 changes: 54 additions & 0 deletions _unittest/test_generic_filesystem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2024 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.

from unittest import mock

from ansys.aedt.core.generic.filesystem import is_safe_path
import pytest


@pytest.fixture(scope="module", autouse=True)
def desktop():
"""Override the desktop fixture to DO NOT open the Desktop when running this test class"""
return


@pytest.mark.parametrize(
"path, allowed_extensions, expected",
[
("/path/to/file.txt", [".txt", ".pdf"], True),
("/path/to/file.exe", [".txt", ".pdf"], False),
("/path/to/file.txt", None, True),
("/path/to/file.txt", [".pdf"], False),
("/path/;rm -rf /file.txt", [".txt"], False),
],
)
def test_is_safe_path(path, allowed_extensions, expected):
"""Test the is_safe_path function."""
with mock.patch("os.path.exists", return_value=True), mock.patch("os.path.isfile", return_value=True):
assert is_safe_path(path, allowed_extensions) == expected

# Test case for an invalid path
with mock.patch("os.path.exists", return_value=False):
assert not is_safe_path("/invalid/path/file.txt", [".txt"])
6 changes: 6 additions & 0 deletions _unittest/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
ERROR_MESSAGE = "Dummy message."


@pytest.fixture(scope="module", autouse=True)
def desktop():
"""Override the desktop fixture to DO NOT open the Desktop when running this test class"""
return


@pyaedt_function_handler(deprecated_arg="trigger_exception")
def foo(trigger_exception=True):
"""Some dummy function used for testing."""
Expand Down
6 changes: 6 additions & 0 deletions _unittest/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
VALID_PYTHON_VERSION = (LATEST_DEPRECATED_PYTHON_VERSION[0], LATEST_DEPRECATED_PYTHON_VERSION[1] + 1)


@pytest.fixture(scope="module", autouse=True)
def desktop():
"""Override the desktop fixture to DO NOT open the Desktop when running this test class"""
return


@patch.object(warnings, "warn")
def test_deprecation_warning_with_deprecated_python_version(mock_warn, monkeypatch):
"""Test that python version warning is triggered."""
Expand Down
5 changes: 1 addition & 4 deletions _unittest_solvers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@
from ansys.aedt.core import Edb
from ansys.aedt.core import Hfss
from ansys.aedt.core.generic.filesystem import Scratch
from ansys.aedt.core.misc.misc import list_installed_ansysem

local_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(local_path)

# Initialize default desktop configuration
default_version = "2024.2"
if "ANSYSEM_ROOT{}".format(default_version[2:].replace(".", "")) not in list_installed_ansysem():
default_version = list_installed_ansysem()[0][12:].replace(".", "")
default_version = "20{}.{}".format(default_version[:2], default_version[-1])

os.environ["ANSYSEM_FEATURE_SS544753_ICEPAK_VIRTUALMESHREGION_PARADIGM_ENABLE"] = "1"
if inside_desktop and "oDesktop" in dir(sys.modules["__main__"]):
default_version = sys.modules["__main__"].oDesktop.GetVersion()[0:6]
Expand Down
9 changes: 3 additions & 6 deletions src/ansys/aedt/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def custom_show_warning(message, category, filename, lineno, file=None, line=Non

warnings.showwarning = custom_show_warning

current_version = sys.version_info[:2]
if current_version <= LATEST_DEPRECATED_PYTHON_VERSION:
current_python_version = sys.version_info[:2]
if current_python_version <= LATEST_DEPRECATED_PYTHON_VERSION:
warnings.warn(PYTHON_VERSION_WARNING, FutureWarning)

# Restore warnings showwarning
Expand All @@ -64,7 +64,7 @@ def custom_show_warning(message, category, filename, lineno, file=None, line=Non
#

pyaedt_path = os.path.dirname(__file__)
__version__ = "0.11.dev0"
__version__ = "0.12.dev0"
version = __version__

# isort: off
Expand Down Expand Up @@ -110,6 +110,3 @@ def custom_show_warning(message, category, filename, lineno, file=None, line=Non
from ansys.aedt.core.generic.general_methods import is_windows
from ansys.aedt.core.generic.general_methods import online_help
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
from ansys.aedt.core.misc import current_student_version
from ansys.aedt.core.misc import current_version
from ansys.aedt.core.misc import installed_versions
4 changes: 2 additions & 2 deletions src/ansys/aedt/core/application/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
from ansys.aedt.core.application.variables import decompose_variable_value
from ansys.aedt.core.desktop import _init_desktop_from_design
from ansys.aedt.core.desktop import exception_to_desktop
from ansys.aedt.core.desktop import get_version_env_variable
from ansys.aedt.core.generic.aedt_versions import aedt_versions
from ansys.aedt.core.generic.constants import AEDT_UNITS
from ansys.aedt.core.generic.constants import unit_system
from ansys.aedt.core.generic.data_handlers import variation_string_to_dict
Expand Down Expand Up @@ -625,7 +625,7 @@ def aedt_version_id(self):
>>> oDesktop.GetVersion()
"""
return get_version_env_variable(self.desktop_class.aedt_version_id)
return aedt_versions.get_version_env_variable(self._aedt_version)

@property
def _aedt_version(self):
Expand Down
Loading

0 comments on commit eb5d293

Please sign in to comment.