92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
"""
|
|
测试分类配置的加载和保存功能
|
|
"""
|
|
import os
|
|
import sys
|
|
import json
|
|
import logging
|
|
|
|
# 配置日志记录
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 添加项目根目录到Python路径
|
|
project_root = os.path.dirname(os.path.abspath(__file__))
|
|
if project_root not in sys.path:
|
|
sys.path.append(project_root)
|
|
|
|
from llmclipboard.document_processor import DocumentProcessor
|
|
|
|
def test_categories_persistence():
|
|
"""测试分类配置的持久化功能"""
|
|
logger.info("开始测试分类配置的持久化功能")
|
|
|
|
# 确定配置文件路径
|
|
if getattr(sys, 'frozen', False):
|
|
# 打包环境 - 使用可执行文件所在目录
|
|
config_dir = os.path.dirname(sys.executable)
|
|
else:
|
|
# 开发环境 - 使用项目根目录
|
|
config_dir = project_root
|
|
|
|
categories_file = os.path.join(config_dir, 'categories.json')
|
|
logger.info(f"分类配置文件路径: {categories_file}")
|
|
|
|
# 1. 检查配置文件是否存在
|
|
if os.path.exists(categories_file):
|
|
logger.info(f"分类配置文件已存在: {categories_file}")
|
|
with open(categories_file, 'r', encoding='utf-8') as f:
|
|
current_categories = json.load(f)
|
|
logger.info(f"当前分类: {current_categories}")
|
|
else:
|
|
logger.info(f"分类配置文件不存在: {categories_file}")
|
|
current_categories = {}
|
|
|
|
# 2. 创建文档处理器实例
|
|
doc_processor = DocumentProcessor(config_path=categories_file)
|
|
|
|
# 3. 获取当前分类
|
|
logger.info(f"文档处理器加载的分类: {doc_processor.categories}")
|
|
|
|
# 4. 修改分类
|
|
new_categories = {
|
|
"测试": ["测试1", "测试2", "测试3"],
|
|
"工作": ["会议", "项目", "计划", "报告", "任务", "进度"],
|
|
"学习": ["教程", "课程", "学习", "笔记", "知识", "总结"],
|
|
"生活": ["购物", "健康", "旅行", "美食", "娱乐"]
|
|
}
|
|
|
|
# 5. 更新分类
|
|
logger.info(f"更新分类: {new_categories}")
|
|
doc_processor.update_categories(new_categories)
|
|
|
|
# 6. 检查配置文件是否已更新
|
|
if os.path.exists(categories_file):
|
|
with open(categories_file, 'r', encoding='utf-8') as f:
|
|
updated_categories = json.load(f)
|
|
logger.info(f"更新后的分类: {updated_categories}")
|
|
|
|
# 验证更新是否成功
|
|
if updated_categories == new_categories:
|
|
logger.info("分类更新成功!")
|
|
else:
|
|
logger.error("分类更新失败!")
|
|
else:
|
|
logger.error(f"分类配置文件不存在: {categories_file}")
|
|
|
|
# 7. 创建新的文档处理器实例,验证配置是否能被正确加载
|
|
new_doc_processor = DocumentProcessor(config_path=categories_file)
|
|
logger.info(f"新文档处理器加载的分类: {new_doc_processor.categories}")
|
|
|
|
# 验证加载是否成功
|
|
if new_doc_processor.categories == new_categories:
|
|
logger.info("分类加载成功!")
|
|
else:
|
|
logger.error("分类加载失败!")
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
test_categories_persistence()
|