From 934dc32c9951b1783f1f641e3329337915af996d Mon Sep 17 00:00:00 2001 From: Mainak Kundu <94432368+mkundu1@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:42:24 -0500 Subject: [PATCH] ci: Generate Fluent journals from unittests (#3507) * ci: Add a single pyfluent test to run with Fluent * ci: Add a single pyfluent test to run with Fluent * ci: debug logging * ci: fix * ci: fix * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: Generate Fluent journals * ci: stop containers * ci: fix run * test: fix * test: fix * ci: fix --- .ci/fluent_test_runner.py | 82 +++++--- .github/workflows/test-fluent-journals.yml | 2 +- Makefile | 10 +- pyproject.toml | 1 - tests/conftest.py | 79 ++++++++ tests/fluent/test_assert/test.py | 2 - tests/fluent/test_meshing_workflow/test.py | 21 -- tests/fluent/test_meshing_workflow/test.yaml | 1 - tests/fluent/test_settings_api/test.py | 12 -- tests/fluent/test_version/test.py | 2 - tests/fluent_fixtures.py | 195 +++++++++++++++++++ tests/test_launcher_remote.py | 80 +++++++- tests/test_new_meshing_workflow.py | 5 +- tests/test_session.py | 11 +- tests/test_tests_util.py | 47 ----- tests/util/__init__.py | 38 ---- 16 files changed, 426 insertions(+), 162 deletions(-) delete mode 100644 tests/fluent/test_assert/test.py delete mode 100644 tests/fluent/test_meshing_workflow/test.py delete mode 100644 tests/fluent/test_meshing_workflow/test.yaml delete mode 100644 tests/fluent/test_settings_api/test.py delete mode 100644 tests/fluent/test_version/test.py create mode 100644 tests/fluent_fixtures.py delete mode 100644 tests/test_tests_util.py delete mode 100644 tests/util/__init__.py diff --git a/.ci/fluent_test_runner.py b/.ci/fluent_test_runner.py index d7bcf4a771b..54b35e19caa 100644 --- a/.ci/fluent_test_runner.py +++ b/.ci/fluent_test_runner.py @@ -21,11 +21,16 @@ class FluentRuntimeError(RuntimeError): pass -def run_fluent_test(journal_file: Path, launcher_args: str = "") -> None: +def run_fluent_test( + src_test_dir: Path, journal_file: Path, launcher_args: str = "" +) -> None: """Run Fluent test. Parameters ---------- + src_test_dir : Path + Path to the Fluent test directory in the host. + journal_file : Path Absolute path to the journal file. @@ -41,12 +46,27 @@ def run_fluent_test(journal_file: Path, launcher_args: str = "") -> None: src_pyfluent_dir = str(Path(pyfluent.__file__).parent) verion_for_file_name = FluentVersion.current_dev().number dst_pyfluent_dir = f"/ansys_inc/v{verion_for_file_name}/commonfiles/CPython/3_10/linx64/Release/python/lib/python3.10/site-packages/ansys/fluent/core" - src_test_dir = str(journal_file.parent) + src_gen_dir = ( + Path(pyfluent.__file__).parent / "ansys" / "fluent" / "core" / "generated" + ) + dst_gen_dir = f"/ansys_inc/v{verion_for_file_name}/fluent/fluent{FluentVersion.current_dev()!r}/cortex/pylib/flapi/generated" dst_test_dir = "/testing" + working_dir = Path(dst_test_dir) + parent = journal_file.parent + parents = [] + while parent != src_test_dir: + parents.append(parent.name) + parent = parent.parent + parents.reverse() + for parent in parents: + working_dir /= parent + working_dir = str(working_dir) + src_test_dir = str(src_test_dir) logging.debug(f"src_pyfluent_dir: {src_pyfluent_dir}") logging.debug(f"dst_pyfluent_dir: {dst_pyfluent_dir}") logging.debug(f"src_test_dir: {src_test_dir}") logging.debug(f"dst_test_dir: {dst_test_dir}") + logging.debug(f"working_dir: {working_dir}") docker_client = docker.from_env() version_for_image_tag = FluentVersion.current_dev().docker_image_tag @@ -55,34 +75,39 @@ def run_fluent_test(journal_file: Path, launcher_args: str = "") -> None: image=image_name, volumes=[ f"{src_pyfluent_dir}:{dst_pyfluent_dir}", + f"{src_gen_dir}:{dst_gen_dir}", # Try removing this after pyfluent is updated in commonfiles f"{src_test_dir}:{dst_test_dir}", ], - working_dir=dst_test_dir, + working_dir=working_dir, environment={"ANSYSLMD_LICENSE_FILE": os.environ["ANSYSLMD_LICENSE_FILE"]}, - command=f"3ddp {launcher_args} -gu -py -i {journal_file.name}", + command=f"{launcher_args} -gu -py -i {journal_file.name}", detach=True, stdout=True, stderr=True, + auto_remove=True, ) - while True: - container.reload() - if container.status == "exited": - break - stderr = container.logs(stdout=False, stderr=True) - if stderr: - stderr = stderr.decode() - for line in stderr.split("\n"): - if line.strip().startswith("Error:"): - if "Expected exception" in line: # for check_assert.py - container.stop() - else: - raise FluentRuntimeError(line) - sleep(1) - logging.debug(container.logs(stderr=True).decode()) - container.remove() - - -MAX_TEST_PATH_LENGTH = 40 + try: + while True: + container.reload() + if container.status == "exited": + break + stderr = container.logs(stdout=False, stderr=True) + if stderr: + stderr = stderr.decode() + for line in stderr.split("\n"): + if line.strip().startswith("Error:"): + if "Expected exception" in line: # for check_assert.py + container.stop() + else: + raise FluentRuntimeError(line) + sleep(1) + logging.debug(container.logs(stderr=True).decode()) + container.remove() + except docker.errors.NotFound: + pass + + +MAX_TEST_PATH_LENGTH = 100 if __name__ == "__main__": @@ -93,19 +118,20 @@ def run_fluent_test(journal_file: Path, launcher_args: str = "") -> None: ) args = parser.parse_args() test_dir = Path.cwd() / args.test_dir - with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: - copytree(test_dir, tmpdir, dirs_exist_ok=True) + with TemporaryDirectory(ignore_cleanup_errors=True) as src_test_dir: + copytree(test_dir, src_test_dir, dirs_exist_ok=True) exception_occurred = False - for test_file in Path(tmpdir).rglob("*.py"): + src_test_dir = Path(src_test_dir) + for test_file in (src_test_dir / "fluent").rglob("*.py"): config_file = test_file.with_suffix(".yaml") launcher_args = "" if config_file.exists(): configs = yaml.safe_load(config_file.read_text()) launcher_args = configs.get("launcher_args", "") - test_file_relpath = str(test_file.relative_to(tmpdir)) + test_file_relpath = str(test_file.relative_to(src_test_dir)) print(f"Running {test_file_relpath}", end="", flush=True) try: - run_fluent_test(test_file, launcher_args) + run_fluent_test(src_test_dir, test_file, launcher_args) print( f"{(MAX_TEST_PATH_LENGTH + 10 - len(test_file_relpath)) * 'ยท'}PASSED" ) diff --git a/.github/workflows/test-fluent-journals.yml b/.github/workflows/test-fluent-journals.yml index 05acfd213a3..f73cc2e3eb3 100644 --- a/.github/workflows/test-fluent-journals.yml +++ b/.github/workflows/test-fluent-journals.yml @@ -82,4 +82,4 @@ jobs: - name: Run Fluent tests run: | - python .ci/fluent_test_runner.py tests/fluent + make write-and-run-fluent-tests diff --git a/Makefile b/Makefile index 7c2e9627696..64e4c9d6a6e 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,8 @@ docker-pull: test-import: @python -c "import ansys.fluent.core as pyfluent" -PYTESTEXTRA = --cache-clear --cov=ansys.fluent --cov-report=xml:cov_xml.xml --cov-report=html -PYTESTRERUN = --last-failed --last-failed-no-failures none +PYTESTEXTRA = --cache-clear --cov=ansys.fluent --cov-report=xml:cov_xml.xml --cov-report=html -n 4 +PYTESTRERUN = --last-failed --last-failed-no-failures none -n 4 unittest: unittest-dev-242 @@ -179,3 +179,9 @@ cleanup-previous-docker-containers: docker stop $(docker ps -a -q); \ fi @if [ -n "$(docker ps -a -q)" ]; then docker rm -vf $(docker ps -a -q); fi + +write-and-run-fluent-tests: + @pip install -r requirements/requirements_build.txt + @poetry install --with test -E reader + @poetry run python -m pytest --write-fluent-journals + @python .ci/fluent_test_runner.py tests diff --git a/pyproject.toml b/pyproject.toml index e4cff4f9743..37a883ec907 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -108,7 +108,6 @@ addopts = """ -v --durations=0 --show-capture=all --n 4 """ markers = [ "settings_only: Read and modify the case settings only, without loading the mesh, initializing, or solving the case", diff --git a/tests/conftest.py b/tests/conftest.py index 5133fdf27c4..c6edd7b3835 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,11 @@ from contextlib import nullcontext import functools +import inspect import operator import os +from pathlib import Path +import shutil +import sys from packaging.specifiers import SpecifierSet from packaging.version import Version @@ -26,6 +30,12 @@ def pytest_addoption(parser): parser.addoption( "--solvermode", action="store_true", default=False, help="run solvermode tests" ) + parser.addoption( + "--write-fluent-journals", + action="store_true", + default=False, + help="Write Fluent journals for unittests", + ) def pytest_runtest_setup(item): @@ -65,6 +75,75 @@ def pytest_runtest_setup(item): pytest.skip() +def pytest_collection_finish(session): + if session.config.getoption("--write-fluent-journals"): + import_path = Path(__file__).parent + sys.path.append(str(import_path)) + import fluent_fixtures + + launcher_args_by_fixture = {} + for k, v in fluent_fixtures.__dict__.items(): + if hasattr(v, "fluent_launcher_args"): + launcher_args_by_fixture[k] = v.fluent_launcher_args + fluent_test_root = import_path / "fluent" + shutil.rmtree(fluent_test_root, ignore_errors=True) + for item in session.items: + skip = False + for mark in item.iter_markers(name="skip"): + skip = True + for mark in item.iter_markers(name="fluent_version"): + spec = mark.args[0] + # TODO: Support older versions + if not ( + spec == "latest" + or Version(FluentVersion.current_dev().value) in SpecifierSet(spec) + ): + skip = True + if skip: + continue + fluent_test_dir = fluent_test_root / item.module.__name__ / item.name + fluent_test_config = fluent_test_dir / "test.yaml" + fluent_test_file = fluent_test_dir / "test.py" + launcher_args = "" + parameters = inspect.signature(item.function).parameters + parameter_set = {p for p in parameters} + if not (parameter_set & set(launcher_args_by_fixture.keys())): + # Skipping as unittest doesn't use fluent fixture + continue + for param in parameters: + if param not in dir(fluent_fixtures): + print(f"Skipping {item.nodeid} because of missing fixture {param}") + skip = True + break + if skip: + continue + for param in parameters: + if param in launcher_args_by_fixture: + launcher_args = launcher_args_by_fixture[param] + break + fluent_test_dir.mkdir(parents=True, exist_ok=True) + with open(fluent_test_config, "w") as f: + f.write(f"launcher_args: {launcher_args}\n") + with open(fluent_test_file, "w") as f: + f.write("import sys\n") + f.write('sys.path.append("/testing")\n') + f.write( + f"from {item.module.__name__} import {item.name} # noqa: E402\n" + ) + f.write("from fluent_fixtures import ( # noqa: E402\n") + for param in parameters: + f.write(f" {param},\n") + f.write(")\n") + f.write("\n") + f.write(f"{item.name}(") + f.write(", ".join([f"{p}(globals())" for p in parameters])) + f.write(")\n") + f.write("exit()\n") + print(f"Written {fluent_test_file}") + session.items = [] + session.testscollected = 0 + + @pytest.fixture(autouse=True) def run_before_each_test( monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest diff --git a/tests/fluent/test_assert/test.py b/tests/fluent/test_assert/test.py deleted file mode 100644 index dcdd53f9d8c..00000000000 --- a/tests/fluent/test_assert/test.py +++ /dev/null @@ -1,2 +0,0 @@ -assert False, "Expected exception" -exit() diff --git a/tests/fluent/test_meshing_workflow/test.py b/tests/fluent/test_meshing_workflow/test.py deleted file mode 100644 index efa1e2b321e..00000000000 --- a/tests/fluent/test_meshing_workflow/test.py +++ /dev/null @@ -1,21 +0,0 @@ -from ansys.fluent.core.examples import download_file - -geometry_file = download_file("mixing_elbow.pmdb", "pyfluent/mixing_elbow") -watertight = meshing.watertight() # noqa: F821 -watertight.import_geometry.file_name.set_state(geometry_file) -assert watertight.import_geometry.length_unit() == "mm" -watertight.import_geometry.length_unit = "in" -assert watertight.import_geometry.length_unit() == "in" -assert watertight.import_geometry.cad_import_options.feature_angle() == 40.0 -watertight.import_geometry.cad_import_options.feature_angle.set_state(25.0) -assert watertight.import_geometry.cad_import_options.feature_angle() == 25.0 -assert watertight.import_geometry.cad_import_options.one_zone_per.allowed_values() == [ - "body", - "face", - "object", -] -assert watertight.import_geometry.cad_import_options.one_zone_per() == "body" -watertight.import_geometry.cad_import_options.one_zone_per = "face" -assert watertight.import_geometry.cad_import_options.one_zone_per() == "face" -watertight.import_geometry() -exit() diff --git a/tests/fluent/test_meshing_workflow/test.yaml b/tests/fluent/test_meshing_workflow/test.yaml deleted file mode 100644 index a78241353e1..00000000000 --- a/tests/fluent/test_meshing_workflow/test.yaml +++ /dev/null @@ -1 +0,0 @@ -launcher_args: -meshing diff --git a/tests/fluent/test_settings_api/test.py b/tests/fluent/test_settings_api/test.py deleted file mode 100644 index b695111bdcd..00000000000 --- a/tests/fluent/test_settings_api/test.py +++ /dev/null @@ -1,12 +0,0 @@ -from ansys.fluent.core.examples import download_file - -case_name = download_file("mixing_elbow.cas.h5", "pyfluent/mixing_elbow") -solver.settings.file.read_case(file_name=case_name) # noqa: F821 -viscous_settings = solver.settings.setup.models.viscous # noqa: F821 -assert viscous_settings.model() == "k-omega" -allowed_values = viscous_settings.model.allowed_values() -assert "k-epsilon" in allowed_values -assert len(allowed_values) > 5 -viscous_settings.model = "k-epsilon" -assert viscous_settings.model() == "k-epsilon" -exit() diff --git a/tests/fluent/test_version/test.py b/tests/fluent/test_version/test.py deleted file mode 100644 index f2c0611dff0..00000000000 --- a/tests/fluent/test_version/test.py +++ /dev/null @@ -1,2 +0,0 @@ -assert ansys.fluent.core.__version__ == "0.27.dev0" # noqa: F821 -exit() diff --git a/tests/fluent_fixtures.py b/tests/fluent_fixtures.py new file mode 100644 index 00000000000..27562a3a091 --- /dev/null +++ b/tests/fluent_fixtures.py @@ -0,0 +1,195 @@ +from typing import Callable + +import pytest + +import ansys.fluent.core as pyfluent +from ansys.fluent.core.data_model_cache import DataModelCache +from ansys.fluent.core.examples import download_file + + +def fluent_launcher_args(args: str): + def fluent_launcher_args_inner(f: Callable): + def wrapper(*args, **kwargs): + return f(*args, **kwargs) + + wrapper.fluent_launcher_args = args + return wrapper + + return fluent_launcher_args_inner + + +def mixing_elbow_geometry_filename(globals): + return download_file( + file_name="mixing_elbow.pmdb", directory="pyfluent/mixing_elbow" + ) + + +def exhaust_system_geometry_filename(globals): + return download_file( + file_name="exhaust_system.fmd", directory="pyfluent/exhaust_system" + ) + + +@fluent_launcher_args("3ddp -meshing") +def new_meshing_session(globals): + meshing = globals["meshing"] + return meshing + + +@fluent_launcher_args("3ddp -meshing") +def new_pure_meshing_session(globals): + return new_meshing_session(globals) + + +@fluent_launcher_args("3ddp -meshing") +def watertight_workflow_session(globals): + meshing = new_meshing_session(globals) + meshing.workflow.InitializeWorkflow(WorkflowType="Watertight Geometry") + return meshing + + +@fluent_launcher_args("3ddp -meshing") +def fault_tolerant_workflow_session(globals): + meshing = new_meshing_session(globals) + meshing.workflow.InitializeWorkflow(WorkflowType="Fault-tolerant Meshing") + return meshing + + +@fluent_launcher_args("3ddp -meshing") +def mixing_elbow_watertight_pure_meshing_session(globals): + meshing = new_pure_meshing_session(globals) + geometry_filename = mixing_elbow_geometry_filename(globals) + meshing.workflow.InitializeWorkflow(WorkflowType="Watertight Geometry") + meshing.workflow.TaskObject["Import Geometry"].Arguments = dict( + FileName=geometry_filename, LengthUnit="in" + ) + return meshing + + +@fluent_launcher_args("3ddp") +def new_solver_session(globals): + solver = globals["solver"] + return solver + + +@fluent_launcher_args("3d") +def new_solver_session_sp(globals): + return new_solver_session(globals) + + +@fluent_launcher_args("2ddp") +def new_solver_session_2d(globals): + return new_solver_session(globals) + + +@fluent_launcher_args("3ddp") +def static_mixer_settings_session(globals): + solver = new_solver_session(globals) + case_name = download_file("Static_Mixer_main.cas.h5", "pyfluent/static_mixer") + solver.file.read( + file_type="case", + file_name=case_name, + lightweight_setup=True, + ) + return solver + + +@fluent_launcher_args("3ddp") +def static_mixer_case_session(globals): + solver = new_solver_session(globals) + case_name = download_file("Static_Mixer_main.cas.h5", "pyfluent/static_mixer") + solver.file.read(file_type="case", file_name=case_name) + return solver + + +@fluent_launcher_args("3ddp") +def mixing_elbow_settings_session(globals): + solver = new_solver_session(globals) + case_name = download_file("mixing_elbow.cas.h5", "pyfluent/mixing_elbow") + solver.settings.file.read( + file_type="case", + file_name=case_name, + lightweight_setup=True, + ) + return solver + + +@fluent_launcher_args("3ddp") +def mixing_elbow_case_data_session(globals): + solver = new_solver_session(globals) + case_name = download_file("mixing_elbow.cas.h5", "pyfluent/mixing_elbow") + download_file("mixing_elbow.dat.h5", "pyfluent/mixing_elbow") + solver.settings.file.read(file_type="case-data", file_name=case_name) + return solver + + +@fluent_launcher_args("3ddp") +def mixing_elbow_param_case_data_session(globals): + solver = new_solver_session(globals) + case_name = download_file("elbow_param.cas.h5", "pyfluent/mixing_elbow") + download_file("elbow_param.dat.h5", "pyfluent/mixing_elbow") + solver.settings.file.read(file_type="case-data", file_name=case_name) + return solver + + +@fluent_launcher_args("2ddp") +def disk_settings_session(globals): + solver = new_solver_session_2d(globals) + case_name = download_file("disk.cas.h5", "pyfluent/rotating_disk") + solver.file.read( + file_type="case", + file_name=case_name, + lightweight_setup=True, + ) + return solver + + +@fluent_launcher_args("2ddp") +def disk_case_session(globals): + solver = new_solver_session_2d(globals) + case_name = download_file("disk.cas.h5", "pyfluent/rotating_disk") + solver.file.read(file_type="case", file_name=case_name) + return solver + + +@fluent_launcher_args("3ddp") +def periodic_rot_settings_session(globals): + solver = new_solver_session(globals) + case_name = download_file( + "periodic_rot.cas.h5", + "pyfluent/periodic_rot", + ) + solver.file.read( + file_type="case", + file_name=case_name, + lightweight_setup=True, + ) + return solver + + +monkeypatch = pytest.MonkeyPatch() + + +def disable_datamodel_cache(globals): + monkeypatch.setattr(pyfluent, "DATAMODEL_USE_STATE_CACHE", False) + + +def display_names_as_keys_in_cache(globals): + DataModelCache.use_display_name = True + + +def new_meshing_session2(globals): + session = pyfluent.launch_fluent(mode=pyfluent.LaunchMode.MESHING) + return session + + +def new_solver_session2(globals): + session = pyfluent.launch_fluent() + return session + + +def static_mixer_case_session2(globals): + session = new_solver_session2(globals) + case_name = download_file("Static_Mixer_main.cas.h5", "pyfluent/static_mixer") + session.file.read(file_type="case", file_name=case_name) + return session diff --git a/tests/test_launcher_remote.py b/tests/test_launcher_remote.py index 7815a741bb6..21d9d7a1949 100644 --- a/tests/test_launcher_remote.py +++ b/tests/test_launcher_remote.py @@ -2,6 +2,8 @@ from concurrent import futures import os +from pathlib import Path +import shutil from unittest.mock import create_autospec import uuid @@ -12,7 +14,7 @@ from ansys.api.fluent.v0 import scheme_eval_pb2_grpc import ansys.fluent.core as pyfluent -from ansys.fluent.core import examples +from ansys.fluent.core import EXAMPLES_PATH, examples from ansys.fluent.core.fluent_connection import ( FluentConnection, UnsupportedRemoteFluentInstance, @@ -26,7 +28,6 @@ from ansys.fluent.core.utils.fluent_version import FluentVersion from ansys.fluent.core.utils.networking import get_free_port import ansys.platform.instancemanagement as pypim -from tests.util import rename_downloaded_file def test_launch_remote_instance(monkeypatch, new_solver_session): @@ -143,6 +144,81 @@ def is_configured(self): return True +def rename_downloaded_file(file_path: str, suffix: str) -> str: + """Rename downloaded file by appending a suffix to the file name. + + Parameters + ---------- + file_path : str + Downloaded file path. Can be absolute or relative. + suffix : str + Suffix to append to the file name. + + Returns: + -------- + str + New file path with the suffix appended to the file name. + """ + ext = "".join(Path(file_path).suffixes) + orig_path = Path(file_path) + file_path = file_path.removesuffix(ext) + file_path = Path(file_path) + if file_path.is_absolute(): + new_stem = f"{file_path.stem}{suffix}" + new_path = file_path.with_stem(new_stem) + new_path = new_path.with_suffix(ext) + orig_path.rename(new_path) + return str(new_path) + else: + orig_abs_path = Path(EXAMPLES_PATH) / orig_path + abs_path = Path(EXAMPLES_PATH) / file_path + new_stem = f"{file_path.stem}{suffix}" + new_path = abs_path.with_stem(new_stem) + new_path = new_path.with_suffix(ext) + orig_abs_path.rename(new_path) + return str(file_path.with_stem(new_stem).with_suffix(ext)) + + +@pytest.mark.parametrize( + "ext,a,b,c,d", + [(".cas", "a1", "b1", "c1", "d1"), (".cas.gz", "a2", "b2", "c2", "d2")], +) +def test_rename_downloaded_file(ext, a, b, c, d): + try: + file_path = Path(EXAMPLES_PATH) / f"{a}{ext}" + file_path.touch() + file_path = str(file_path) + new_file_path = rename_downloaded_file(file_path, "_1") + assert new_file_path == str(Path(EXAMPLES_PATH) / f"{a}_1{ext}") + except Exception: + raise + finally: + Path(new_file_path).unlink(missing_ok=True) + + try: + file_path = f"{b}{ext}" + (Path(EXAMPLES_PATH) / file_path).touch() + new_file_path = rename_downloaded_file(file_path, "_1") + assert new_file_path == f"{b}_1{ext}" + except Exception: + raise + finally: + (Path(EXAMPLES_PATH) / new_file_path).unlink(missing_ok=True) + + try: + dir_path = Path(EXAMPLES_PATH) / c + dir_path.mkdir() + file_path = dir_path / f"{d}{ext}" + file_path.touch() + file_path = str(Path(c) / f"{d}{ext}") + new_file_path = rename_downloaded_file(file_path, "_1") + assert new_file_path == str(Path(c) / f"{d}_1{ext}") + except Exception: + raise + finally: + shutil.rmtree(dir_path, ignore_errors=True) + + @pytest.mark.codegen_required @pytest.mark.fluent_version(">=24.2") def test_file_purpose_on_remote_instance( diff --git a/tests/test_new_meshing_workflow.py b/tests/test_new_meshing_workflow.py index 6ae93fc40cb..28688338456 100644 --- a/tests/test_new_meshing_workflow.py +++ b/tests/test_new_meshing_workflow.py @@ -5,7 +5,6 @@ from ansys.fluent.core import FluentVersion, examples from ansys.fluent.core.workflow import camel_to_snake_case -from tests.conftest import new_meshing_session @pytest.mark.nightly @@ -1468,7 +1467,9 @@ def test_created_workflow(new_meshing_session): ) -new_meshing_session2 = new_meshing_session +@pytest.fixture +def new_meshing_session2(new_meshing_session): + return new_meshing_session @pytest.mark.codegen_required diff --git a/tests/test_session.py b/tests/test_session.py index 5e55e72a40c..09aacc13bc1 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -25,7 +25,6 @@ from ansys.fluent.core.utils.fluent_version import FluentVersion from ansys.fluent.core.utils.networking import get_free_port from ansys.fluent.core.warnings import PyFluentDeprecationWarning -from tests.conftest import new_solver_session class MockSettingsServicer(settings_pb2_grpc.SettingsServicer): @@ -391,7 +390,9 @@ def test_help_does_not_throw(new_solver_session): help(new_solver_session.file.read) -new_solver_session2 = new_solver_session +@pytest.fixture +def new_solver_session2(new_solver_session): + return new_solver_session def test_build_from_fluent_connection(new_solver_session, new_solver_session2): @@ -410,7 +411,11 @@ def test_build_from_fluent_connection(new_solver_session, new_solver_session2): ) assert solver1.health_check.is_serving assert solver2.health_check.is_serving - assert not health_check_service1.is_serving + timeout_loop( + not health_check_service1.is_serving, + timeout=60, + idle_period=1, + ) assert solver1._fluent_connection.connection_properties.cortex_pid == cortex_pid2 assert solver2._fluent_connection.connection_properties.cortex_pid == cortex_pid2 diff --git a/tests/test_tests_util.py b/tests/test_tests_util.py deleted file mode 100644 index 64e3a75615e..00000000000 --- a/tests/test_tests_util.py +++ /dev/null @@ -1,47 +0,0 @@ -from pathlib import Path -import shutil - -import pytest - -from ansys.fluent.core import EXAMPLES_PATH -from tests.util import rename_downloaded_file - - -@pytest.mark.parametrize( - "ext,a,b,c,d", - [(".cas", "a1", "b1", "c1", "d1"), (".cas.gz", "a2", "b2", "c2", "d2")], -) -def test_rename_downloaded_file(ext, a, b, c, d): - try: - file_path = Path(EXAMPLES_PATH) / f"{a}{ext}" - file_path.touch() - file_path = str(file_path) - new_file_path = rename_downloaded_file(file_path, "_1") - assert new_file_path == str(Path(EXAMPLES_PATH) / f"{a}_1{ext}") - except Exception: - raise - finally: - Path(new_file_path).unlink(missing_ok=True) - - try: - file_path = f"{b}{ext}" - (Path(EXAMPLES_PATH) / file_path).touch() - new_file_path = rename_downloaded_file(file_path, "_1") - assert new_file_path == f"{b}_1{ext}" - except Exception: - raise - finally: - (Path(EXAMPLES_PATH) / new_file_path).unlink(missing_ok=True) - - try: - dir_path = Path(EXAMPLES_PATH) / c - dir_path.mkdir() - file_path = dir_path / f"{d}{ext}" - file_path.touch() - file_path = str(Path(c) / f"{d}{ext}") - new_file_path = rename_downloaded_file(file_path, "_1") - assert new_file_path == str(Path(c) / f"{d}_1{ext}") - except Exception: - raise - finally: - shutil.rmtree(dir_path, ignore_errors=True) diff --git a/tests/util/__init__.py b/tests/util/__init__.py deleted file mode 100644 index 2a95cf4894f..00000000000 --- a/tests/util/__init__.py +++ /dev/null @@ -1,38 +0,0 @@ -from pathlib import Path - -from ansys.fluent.core import EXAMPLES_PATH - - -def rename_downloaded_file(file_path: str, suffix: str) -> str: - """Rename downloaded file by appending a suffix to the file name. - - Parameters - ---------- - file_path : str - Downloaded file path. Can be absolute or relative. - suffix : str - Suffix to append to the file name. - - Returns: - -------- - str - New file path with the suffix appended to the file name. - """ - ext = "".join(Path(file_path).suffixes) - orig_path = Path(file_path) - file_path = file_path.removesuffix(ext) - file_path = Path(file_path) - if file_path.is_absolute(): - new_stem = f"{file_path.stem}{suffix}" - new_path = file_path.with_stem(new_stem) - new_path = new_path.with_suffix(ext) - orig_path.rename(new_path) - return str(new_path) - else: - orig_abs_path = Path(EXAMPLES_PATH) / orig_path - abs_path = Path(EXAMPLES_PATH) / file_path - new_stem = f"{file_path.stem}{suffix}" - new_path = abs_path.with_stem(new_stem) - new_path = new_path.with_suffix(ext) - orig_abs_path.rename(new_path) - return str(file_path.with_stem(new_stem).with_suffix(ext))