Files
main_plugin_farm/tests/test_config_migration.py
T
Diuse 38f4b84cf0 add # 新增种子商店界面
fixed # 修复数据库目录错误的BUG
2026-10-20 13:15:01 +08:00

48 lines
1.9 KiB
Python

import importlib.util
from pathlib import Path
from types import SimpleNamespace
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location(
"farm_config_migration",
ROOT / "integrations/zhenxun/config_migration.py",
)
migration = importlib.util.module_from_spec(spec)
spec.loader.exec_module(migration)
class ConfigMigrationTests(unittest.TestCase):
def test_cleanup_backups_and_repeat_are_scoped(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
group = SimpleNamespace(configs={
"绘制农场清晰度": "low", "CUSTOM": "keep",
})
saves = []
manager = SimpleNamespace(
file=root / "plugins2config.yaml",
_simple_file=root / "config.yaml",
_simple_data={
"farm": {"绘制农场清晰度": "low", "CUSTOM": "keep"},
"other": {"服务地址": "keep"},
},
get=lambda module: group,
save=lambda **kwargs: saves.append(kwargs),
)
for path in (manager.file, manager._simple_file):
path.write_text("original", encoding="utf-8")
migration.migrate_config(manager, "farm")
self.assertEqual(group.configs, {"CUSTOM": "keep"})
self.assertEqual(manager._simple_data["farm"], {"CUSTOM": "keep"})
self.assertEqual(manager._simple_data["other"], {"服务地址": "keep"})
self.assertEqual(saves, [{"save_simple_data": True}])
for path in (manager.file, manager._simple_file):
self.assertEqual(path.with_name(
path.name + ".farm-v1.bak",
).read_text(encoding="utf-8"), "original")
migration.migrate_config(manager, "farm")
self.assertEqual(len(saves), 1)