mirror of
https://github.com/CodeSeeed/sbackup.git
synced 2026-10-05 02:42:58 +08:00
feat: 添加基础单元测试
This commit is contained in:
1 parent
2d528ac3da
commit
fa343ea8e9
13 files changed
+506
-80
No files matched your search
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
单元测试 for sbackup.auto_save 模块
|
||||
"""
|
||||
import unittest
|
||||
import os
|
||||
import json
|
||||
import tempfile
|
||||
import shutil
|
||||
from sbackup.auto_save import add_folder, rm_folder
|
||||
|
||||
|
||||
class TestAutoSave(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# 创建临时目录
|
||||
self.test_dir = tempfile.mkdtemp()
|
||||
self.data_file = os.path.join(self.test_dir, "sbackup.json")
|
||||
|
||||
# 设置环境变量
|
||||
os.environ["SBACKUP_DATA_FILE"] = self.data_file
|
||||
|
||||
# 创建测试文件夹
|
||||
self.source_folder = os.path.join(self.test_dir, "source")
|
||||
self.target_folder = os.path.join(self.test_dir, "target")
|
||||
os.makedirs(self.source_folder)
|
||||
os.makedirs(self.target_folder)
|
||||
|
||||
def tearDown(self):
|
||||
# 清理临时目录
|
||||
shutil.rmtree(self.test_dir)
|
||||
|
||||
def test_add_folder(self):
|
||||
"""测试添加备份策略"""
|
||||
# 添加策略
|
||||
add_folder(self.source_folder, self.target_folder, ".git")
|
||||
|
||||
# 验证数据文件
|
||||
import time
|
||||
time.sleep(0.5) # 确保文件写入完成
|
||||
|
||||
# 检查文件是否存在
|
||||
self.assertTrue(os.path.exists(self.data_file), f"数据文件 {self.data_file} 不存在")
|
||||
|
||||
with open(self.data_file, "r") as f:
|
||||
data = json.load(f)
|
||||
self.assertIn(os.path.abspath(self.source_folder), data, f"数据文件内容: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,46 @@
|
||||
"""
|
||||
单元测试 for sbackup._compression 模块
|
||||
"""
|
||||
import unittest
|
||||
import os
|
||||
import shutil
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
from sbackup._compression import Config, ZipfileCompression
|
||||
|
||||
|
||||
class TestCompression(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# 创建测试文件夹
|
||||
self.test_dir = "test_data"
|
||||
os.makedirs(self.test_dir, exist_ok=True)
|
||||
(Path(self.test_dir) / "file1.txt").write_text("test content 1")
|
||||
(Path(self.test_dir) / "subdir").mkdir()
|
||||
(Path(self.test_dir) / "subdir" / "file2.txt").write_text("test content 2")
|
||||
|
||||
self.zip_path = "test.zip"
|
||||
if os.path.exists(self.zip_path):
|
||||
os.remove(self.zip_path)
|
||||
|
||||
def tearDown(self):
|
||||
# 清理测试文件夹
|
||||
if os.path.exists(self.test_dir):
|
||||
shutil.rmtree(self.test_dir)
|
||||
if os.path.exists(self.zip_path):
|
||||
os.remove(self.zip_path)
|
||||
|
||||
def test_zip_folder_basic(self):
|
||||
"""测试基本压缩功能"""
|
||||
config = Config(
|
||||
folder_path=self.test_dir,
|
||||
zipfile_path=self.zip_path,
|
||||
)
|
||||
compressor = ZipfileCompression(config)
|
||||
compressor.zip_folder()
|
||||
|
||||
self.assertTrue(os.path.exists(self.zip_path))
|
||||
self.assertGreater(os.path.getsize(self.zip_path), 0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,19 @@
|
||||
"""
|
||||
单元测试 for sbackup.__init__ 模块
|
||||
"""
|
||||
import unittest
|
||||
import sys
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
class TestMain(unittest.TestCase):
|
||||
@patch("builtins.print")
|
||||
def test_version_command(self, mock_print):
|
||||
"""测试 version 命令"""
|
||||
sys.argv = ["sbackup", "version"]
|
||||
from sbackup import run
|
||||
run()
|
||||
mock_print.assert_called()
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user