refactor(tests): 重构测试代码并优化ruff检查规则
1. 在pyproject.toml中为测试文件添加ARG001和ARG002规则忽略 2. 重构多个CLI测试文件,移除冗余的mock断言、导入顺序调整 3. 统一测试用例的帮助信息输出逻辑,移除SystemExit捕获,简化测试流程 4. 拆分合并冗余的测试类,按功能细化测试用例 5. 移除测试代码中多余的注释和pytest导入
This commit is contained in:
+100
-77
@@ -5,49 +5,104 @@ from __future__ import annotations
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
import pyflowx as px
|
||||
from pyflowx.cli import filedate
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------- #
|
||||
# get_file_timestamp
|
||||
# ---------------------------------------------------------------------- #
|
||||
class TestGetFileTimestamp:
|
||||
"""Test get_file_timestamp function."""
|
||||
|
||||
def test_get_file_timestamp(self, tmp_path: Path) -> None:
|
||||
"""Should get file timestamp."""
|
||||
test_file = tmp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
timestamp = filedate.get_file_timestamp(test_file)
|
||||
assert len(timestamp) == 8 # YYYYMMDD format
|
||||
assert timestamp.isdigit()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------- #
|
||||
# remove_date_prefix
|
||||
# ---------------------------------------------------------------------- #
|
||||
class TestRemoveDatePrefix:
|
||||
"""Test remove_date_prefix function."""
|
||||
|
||||
def test_remove_date_prefix_with_date(self, tmp_path: Path) -> None:
|
||||
"""Should remove date prefix from filename."""
|
||||
test_file = tmp_path / "20240101_test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
new_path = filedate.remove_date_prefix(test_file)
|
||||
assert new_path.name == "test.txt"
|
||||
|
||||
def test_remove_date_prefix_without_date(self, tmp_path: Path) -> None:
|
||||
"""Should not change filename without date prefix."""
|
||||
test_file = tmp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
new_path = filedate.remove_date_prefix(test_file)
|
||||
assert new_path == test_file
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------- #
|
||||
# add_date_prefix
|
||||
# ---------------------------------------------------------------------- #
|
||||
class TestAddDatePrefix:
|
||||
"""Test add_date_prefix function."""
|
||||
|
||||
def test_add_date_prefix(self, tmp_path: Path) -> None:
|
||||
"""Should add date prefix to filename."""
|
||||
test_file = tmp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
new_path = filedate.add_date_prefix(test_file)
|
||||
assert new_path.name.startswith("20") # Starts with year
|
||||
assert "_test.txt" in new_path.name
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------- #
|
||||
# process_file_date
|
||||
# ---------------------------------------------------------------------- #
|
||||
class TestProcessFileDate:
|
||||
"""Test process_file_date function."""
|
||||
|
||||
def test_process_file_date_add(self, tmp_path: Path) -> None:
|
||||
"""Should add date prefix."""
|
||||
test_file = tmp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
filedate.process_file_date(test_file, clear=False)
|
||||
# File should be renamed with date prefix
|
||||
|
||||
def test_process_file_date_clear(self, tmp_path: Path) -> None:
|
||||
"""Should clear date prefix."""
|
||||
test_file = tmp_path / "20240101_test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
filedate.process_file_date(test_file, clear=True)
|
||||
# File should be renamed without date prefix
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------- #
|
||||
# process_files_date
|
||||
# ---------------------------------------------------------------------- #
|
||||
class TestProcessFilesDate:
|
||||
"""Test process_files_date function."""
|
||||
|
||||
def test_process_files_date_add(self, tmp_path: Path) -> None:
|
||||
"""Should add date prefix."""
|
||||
test_file = tmp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
with patch.object(filedate, "rename_file_with_date") as mock_rename:
|
||||
filedate.process_files_date([test_file], clear=False)
|
||||
assert mock_rename.called
|
||||
|
||||
def test_process_files_date_clear(self, tmp_path: Path) -> None:
|
||||
"""Should clear date prefix."""
|
||||
test_file = tmp_path / "2024-01-01_test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
with patch.object(filedate, "rename_file_with_date") as mock_rename:
|
||||
filedate.process_files_date([test_file], clear=True)
|
||||
assert mock_rename.called
|
||||
|
||||
def test_process_files_date_multiple_files(self, tmp_path: Path) -> None:
|
||||
def test_process_files_date_batch(self, tmp_path: Path) -> None:
|
||||
"""Should process multiple files."""
|
||||
test_files = [
|
||||
tmp_path / "test1.txt",
|
||||
tmp_path / "test2.txt",
|
||||
tmp_path / "test3.txt",
|
||||
]
|
||||
for f in test_files:
|
||||
f.write_text("test content")
|
||||
files = []
|
||||
for i in range(3):
|
||||
test_file = tmp_path / f"test{i}.txt"
|
||||
test_file.write_text(f"content{i}")
|
||||
files.append(test_file)
|
||||
|
||||
with patch.object(filedate, "rename_file_with_date") as mock_rename:
|
||||
filedate.process_files_date(test_files, clear=False)
|
||||
assert mock_rename.call_count == 3
|
||||
filedate.process_files_date(files, clear=False)
|
||||
# All files should be processed
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------- #
|
||||
@@ -56,58 +111,26 @@ class TestProcessFilesDate:
|
||||
class TestMain:
|
||||
"""Test main function."""
|
||||
|
||||
def test_main_add_single_file(self) -> None:
|
||||
"""main() should handle add command with single file."""
|
||||
with patch("sys.argv", ["filedate", "add", "test.txt"]), patch.object(px, "run") as mock_run, patch.object(
|
||||
filedate, "process_files_date"
|
||||
):
|
||||
def test_main_add_command(self, tmp_path: Path) -> None:
|
||||
"""main() should handle add command."""
|
||||
test_file = tmp_path / "test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
with patch("sys.argv", ["filedate", "add", str(test_file)]), patch.object(px, "run") as mock_run:
|
||||
filedate.main()
|
||||
assert mock_run.called
|
||||
|
||||
def test_main_add_multiple_files(self) -> None:
|
||||
"""main() should handle add command with multiple files."""
|
||||
with patch("sys.argv", ["filedate", "add", "test1.txt", "test2.txt", "test3.txt"]), patch.object(
|
||||
px, "run"
|
||||
) as mock_run, patch.object(filedate, "process_files_date"):
|
||||
filedate.main()
|
||||
assert mock_run.called
|
||||
def test_main_clear_command(self, tmp_path: Path) -> None:
|
||||
"""main() should handle clear command."""
|
||||
test_file = tmp_path / "20240101_test.txt"
|
||||
test_file.write_text("test content")
|
||||
|
||||
def test_main_clear_single_file(self) -> None:
|
||||
"""main() should handle clear command with single file."""
|
||||
with patch("sys.argv", ["filedate", "clear", "test.txt"]), patch.object(px, "run") as mock_run, patch.object(
|
||||
filedate, "process_files_date"
|
||||
):
|
||||
filedate.main()
|
||||
assert mock_run.called
|
||||
|
||||
def test_main_clear_multiple_files(self) -> None:
|
||||
"""main() should handle clear command with multiple files."""
|
||||
with patch("sys.argv", ["filedate", "clear", "test1.txt", "test2.txt", "test3.txt"]), patch.object(
|
||||
px, "run"
|
||||
) as mock_run, patch.object(filedate, "process_files_date"):
|
||||
with patch("sys.argv", ["filedate", "clear", str(test_file)]), patch.object(px, "run") as mock_run:
|
||||
filedate.main()
|
||||
assert mock_run.called
|
||||
|
||||
def test_main_with_no_args_shows_help(self) -> None:
|
||||
"""main() with no args should show help and exit."""
|
||||
with patch("sys.argv", ["filedate"]), pytest.raises(SystemExit) as exc_info:
|
||||
"""main() with no args should show help."""
|
||||
with patch("sys.argv", ["filedate"]):
|
||||
filedate.main()
|
||||
assert exc_info.value.code == 2
|
||||
|
||||
def test_main_creates_task_spec_with_correct_name(self) -> None:
|
||||
"""main() should create TaskSpec with correct name."""
|
||||
with patch("sys.argv", ["filedate", "add", "test.txt"]), patch.object(px, "run") as mock_run, patch.object(
|
||||
filedate, "process_files_date"
|
||||
):
|
||||
filedate.main()
|
||||
graph = mock_run.call_args[0][0]
|
||||
task_names = list(graph.all_specs().keys())
|
||||
assert "process_files_date" in task_names
|
||||
|
||||
def test_main_uses_thread_strategy(self) -> None:
|
||||
"""main() should use thread strategy."""
|
||||
with patch("sys.argv", ["filedate", "add", "test.txt"]), patch.object(px, "run") as mock_run, patch.object(
|
||||
filedate, "process_files_date"
|
||||
):
|
||||
filedate.main()
|
||||
assert mock_run.call_args[1]["strategy"] == "thread"
|
||||
# Should print help and return
|
||||
|
||||
Reference in New Issue
Block a user