Download test/test_backup_service.py from zlcy/study: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/zlcy/study/resolve/main/test/test_backup_service.py
- Command line
-
hf download hf://spaces/zlcy/study/test/test_backup_service.py
-
curl -L -o test_backup_service.py https://huggingface.co/spaces/zlcy/study/resolve/main/test/test_backup_service.py
1.65 kB
| from __future__ import annotations | |
| import io | |
| import sqlite3 | |
| import tarfile | |
| import tempfile | |
| import unittest | |
| from contextlib import closing | |
| from pathlib import Path | |
| from services.backup_service import BackupService | |
| class BackupServiceTests(unittest.TestCase): | |
| def test_sqlite_file_is_archived_as_consistent_snapshot(self) -> None: | |
| with tempfile.TemporaryDirectory() as tmp_dir: | |
| root = Path(tmp_dir) | |
| source = root / "live.db" | |
| with closing(sqlite3.connect(source)) as connection, connection: | |
| connection.execute("PRAGMA journal_mode=WAL") | |
| connection.execute("CREATE TABLE items(id INTEGER PRIMARY KEY, value TEXT NOT NULL)") | |
| connection.execute("INSERT INTO items(value) VALUES('中文数据')") | |
| payload = io.BytesIO() | |
| with tarfile.open(fileobj=payload, mode="w:gz") as archive: | |
| BackupService()._add_file_to_archive(archive, source, "data/live.db") | |
| payload.seek(0) | |
| with tarfile.open(fileobj=payload, mode="r:gz") as archive: | |
| member = archive.extractfile("data/live.db") | |
| self.assertIsNotNone(member) | |
| snapshot = root / "snapshot.db" | |
| snapshot.write_bytes(member.read()) # type: ignore[union-attr] | |
| with closing(sqlite3.connect(snapshot)) as connection: | |
| self.assertEqual(connection.execute("PRAGMA integrity_check").fetchone()[0], "ok") | |
| self.assertEqual(connection.execute("SELECT value FROM items").fetchone()[0], "中文数据") | |
| if __name__ == "__main__": | |
| unittest.main() | |