-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/create_volume_plot
- Loading branch information
Showing
24 changed files
with
545 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.