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.
104 lines
3.6 KiB
Python
104 lines
3.6 KiB
Python
"""Tests for cli.filedate module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from pyflowx.ops import 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
|