feat: add database backup and restore in system settings
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.db.backup import (
|
||||
backup_db,
|
||||
is_valid_backup_name,
|
||||
list_backups,
|
||||
resolve_backup_file,
|
||||
restore_db,
|
||||
)
|
||||
|
||||
|
||||
def test_backup_roundtrip(tmp_path: Path):
|
||||
db = tmp_path / "live.db"
|
||||
backup_dir = tmp_path / "backups"
|
||||
conn = __import__("sqlite3").connect(str(db))
|
||||
conn.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT)")
|
||||
conn.execute("INSERT INTO t(v) VALUES ('hello')")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
dest = backup_db(db, backup_dir, tz="UTC")
|
||||
assert dest.is_file()
|
||||
assert is_valid_backup_name(dest.name)
|
||||
|
||||
conn = __import__("sqlite3").connect(str(db))
|
||||
conn.execute("DELETE FROM t")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
restore_db(dest, db)
|
||||
conn = __import__("sqlite3").connect(str(db))
|
||||
row = conn.execute("SELECT v FROM t").fetchone()
|
||||
conn.close()
|
||||
assert row[0] == "hello"
|
||||
|
||||
items = list_backups(backup_dir)
|
||||
assert len(items) == 1
|
||||
assert items[0]["name"] == dest.name
|
||||
assert resolve_backup_file(backup_dir, dest.name) == dest.resolve()
|
||||
|
||||
|
||||
def test_invalid_backup_name(tmp_path: Path):
|
||||
with pytest.raises(ValueError):
|
||||
resolve_backup_file(tmp_path, "../etc/passwd")
|
||||
Reference in New Issue
Block a user