Files
pyflowx/tests/cli/test_filedate.py
T
zhou 6da42ec5ff refactor: 移除YAML配置,统一使用@px.tool装饰器实现CLI工具
1.  新增tools.py模块实现@px.tool装饰器与工具注册表
2.  将所有configs下的YAML工具迁移为ops/xxx.py模块形式
3.  重构cli路由逻辑,优先加载Python工具实现回退YAML
4.  删除所有YAML配置文件与旧的yaml_loader相关代码
5.  调整__init__.py导出API,移除YAML相关依赖
2026-07-06 07:53:22 +08:00

104 lines
3.6 KiB
Python

"""Tests for cli.filedate module."""
from __future__ import annotations
from pathlib import Path
from pyflowx.ops import filedate as files
# ---------------------------------------------------------------------- #
# 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 = files.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 = files.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 = files.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 = files.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")
files.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")
files.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_batch(self, tmp_path: Path) -> None:
"""Should process multiple files."""
file_list = []
for i in range(3):
test_file = tmp_path / f"test{i}.txt"
test_file.write_text(f"content{i}")
file_list.append(test_file)
files.process_files_date(file_list, clear=False)
# All files should be processed