Files
DDUp/tests/test_backup.py
2026-01-22 12:57:26 +08:00

46 lines
1.2 KiB
Python

"""数据库备份测试"""
from datetime import datetime, timedelta
from src.vitals.core.backup import (
backup_database,
cleanup_old_backups,
list_backups,
restore_database,
)
def test_backup_database(tmp_path):
backup_path = backup_database(tmp_path)
assert backup_path.exists()
assert backup_path.name.startswith("vitals_")
assert backup_path.suffix == ".db"
def test_cleanup_old_backups(tmp_path):
old_date = (datetime.now() - timedelta(days=10)).strftime("%Y%m%d")
new_date = datetime.now().strftime("%Y%m%d")
old_backup = tmp_path / f"vitals_{old_date}_120000.db"
new_backup = tmp_path / f"vitals_{new_date}_120000.db"
old_backup.write_text("old")
new_backup.write_text("new")
deleted = cleanup_old_backups(tmp_path, keep_days=7)
assert deleted == 1
assert not old_backup.exists()
assert new_backup.exists()
def test_list_backups(tmp_path):
backup_path = backup_database(tmp_path)
backups = list_backups(tmp_path)
assert len(backups) > 0
assert backups[0]["path"] == backup_path
def test_restore_database(tmp_path, test_db):
backup_path = backup_database(tmp_path)
result = restore_database(backup_path)
assert result is True