d93da0d8b4
CI / Lint, Typecheck & Test (push) Has been cancelled
将 src/pyflowx/cli/_ops/ 整体迁移至 src/pyflowx/ops/, 与 cli/ 平级 (工具函数非 CLI 专属, 可被 YAML 任务编排通用引用). 分类保持不变: dev (git/pip/bump/autofmt), files (date/level/back/zip), media (pdf/screenshot), system (ls/pack/ssh). 同步更新 15 个引用文件 (yaml_loader + 14 个测试) 的 import 路径, README 模块结构表与 test_registry docstring.
111 lines
4.3 KiB
Python
111 lines
4.3 KiB
Python
"""Tests for cli.lscalc module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from pyflowx.conditions import Constants
|
|
from pyflowx.ops import system
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# get_ls_dyna_command
|
|
# ---------------------------------------------------------------------- #
|
|
class TestGetLsDynaCommand:
|
|
"""Test get_ls_dyna_command function."""
|
|
|
|
def test_get_ls_dyna_command_windows(self) -> None:
|
|
"""Should get LS-DYNA command for Windows."""
|
|
with patch.object(Constants, "IS_WINDOWS", True), patch.object(Constants, "IS_MACOS", False):
|
|
cmd = system.get_ls_dyna_command("input.k", 4)
|
|
assert "ls-dyna_mpp" in cmd
|
|
assert "i=input.k" in cmd
|
|
assert "ncpu=4" in cmd
|
|
|
|
def test_get_ls_dyna_command_linux(self) -> None:
|
|
"""Should get LS-DYNA command for Linux."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", False):
|
|
cmd = system.get_ls_dyna_command("input.k", 8)
|
|
assert "ls-dyna_mpp" in cmd
|
|
assert "i=input.k" in cmd
|
|
assert "ncpu=8" in cmd
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# run_ls_dyna
|
|
# ---------------------------------------------------------------------- #
|
|
class TestRunLsDyna:
|
|
"""Test run_ls_dyna function."""
|
|
|
|
def test_run_ls_dyna_success(self, tmp_path: Path) -> None:
|
|
"""Should run LS-DYNA successfully."""
|
|
input_file = tmp_path / "input.k"
|
|
input_file.write_text("LS-DYNA input")
|
|
|
|
with patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
system.run_ls_dyna(str(input_file), ncpu=4)
|
|
assert mock_run.called
|
|
|
|
def test_run_ls_dyna_file_not_found(self, tmp_path: Path) -> None:
|
|
"""Should handle nonexistent input file."""
|
|
input_file = tmp_path / "nonexistent.k"
|
|
|
|
system.run_ls_dyna(str(input_file), ncpu=4)
|
|
# Should print error message
|
|
|
|
def test_run_ls_dyna_command_not_found(self, tmp_path: Path) -> None:
|
|
"""Should handle command not found."""
|
|
input_file = tmp_path / "input.k"
|
|
input_file.write_text("LS-DYNA input")
|
|
|
|
with patch("subprocess.run", side_effect=FileNotFoundError):
|
|
system.run_ls_dyna(str(input_file), ncpu=4)
|
|
# Should print error message
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# run_ls_dyna_mpi
|
|
# ---------------------------------------------------------------------- #
|
|
class TestRunLsDynaMpi:
|
|
"""Test run_ls_dyna_mpi function."""
|
|
|
|
def test_run_ls_dyna_mpi_success(self, tmp_path: Path) -> None:
|
|
"""Should run LS-DYNA MPI successfully."""
|
|
input_file = tmp_path / "input.k"
|
|
input_file.write_text("LS-DYNA input")
|
|
|
|
with patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
system.run_ls_dyna_mpi(str(input_file), ncpu=8)
|
|
assert mock_run.called
|
|
|
|
def test_run_ls_dyna_mpi_file_not_found(self, tmp_path: Path) -> None:
|
|
"""Should handle nonexistent input file."""
|
|
input_file = tmp_path / "nonexistent.k"
|
|
|
|
system.run_ls_dyna_mpi(str(input_file), ncpu=8)
|
|
# Should print error message
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# check_ls_dyna_status
|
|
# ---------------------------------------------------------------------- #
|
|
class TestCheckLsDynaStatus:
|
|
"""Test check_ls_dyna_status function."""
|
|
|
|
def test_check_ls_dyna_status_windows(self) -> None:
|
|
"""Should check LS-DYNA status on Windows."""
|
|
with patch.object(Constants, "IS_WINDOWS", True), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(stdout="ls-dyna_mpp.exe", returncode=0)
|
|
system.check_ls_dyna_status()
|
|
assert mock_run.called
|
|
|
|
def test_check_ls_dyna_status_linux(self) -> None:
|
|
"""Should check LS-DYNA status on Linux."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(stdout="1234", returncode=0)
|
|
system.check_ls_dyna_status()
|
|
assert mock_run.called
|