|
@@ -0,0 +1,295 @@
|
|
|
+"""
|
|
|
+测试黄历信息数据模型的五行颜色映射功能
|
|
|
+"""
|
|
|
+
|
|
|
+import sys
|
|
|
+import os
|
|
|
+from datetime import date
|
|
|
+
|
|
|
+# 添加项目路径到sys.path
|
|
|
+sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
+
|
|
|
+from app.core.data_parse.calendar import CalendarInfo, CalendarService
|
|
|
+
|
|
|
+
|
|
|
+def test_wuxing_color_mapping():
|
|
|
+ """测试五行颜色映射功能"""
|
|
|
+ print("=== 测试五行颜色映射功能 ===")
|
|
|
+
|
|
|
+ # 测试数据:包含不同五行元素的wuxing字段
|
|
|
+ test_cases = [
|
|
|
+ {
|
|
|
+ 'name': '金元素测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-20',
|
|
|
+ 'yinli': '腊月二十一',
|
|
|
+ 'wuxing': '井泉水 金 建执位', # 包含"金"
|
|
|
+ },
|
|
|
+ 'expected_color': '白'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': '水元素测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-21',
|
|
|
+ 'yinli': '腊月二十二',
|
|
|
+ 'wuxing': '井泉水 建执位', # 包含"水"
|
|
|
+ },
|
|
|
+ 'expected_color': '黑'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': '木元素测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-22',
|
|
|
+ 'yinli': '腊月二十三',
|
|
|
+ 'wuxing': '大林木 除执位', # 包含"木"
|
|
|
+ },
|
|
|
+ 'expected_color': '绿'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': '火元素测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-23',
|
|
|
+ 'yinli': '腊月二十四',
|
|
|
+ 'wuxing': '山头火 满执位', # 包含"火"
|
|
|
+ },
|
|
|
+ 'expected_color': '红'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': '土元素测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-24',
|
|
|
+ 'yinli': '腊月二十五',
|
|
|
+ 'wuxing': '大驿土 平执位', # 包含"土"
|
|
|
+ },
|
|
|
+ 'expected_color': '黄'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': '无五行元素测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-25',
|
|
|
+ 'yinli': '腊月二十六',
|
|
|
+ 'wuxing': '建执位', # 不包含五行元素
|
|
|
+ },
|
|
|
+ 'expected_color': None
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': '预设颜色测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-26',
|
|
|
+ 'yinli': '腊月二十七',
|
|
|
+ 'wuxing': '井泉水 金 建执位', # 包含"金",但已有预设颜色
|
|
|
+ 'color': '紫' # 预设颜色
|
|
|
+ },
|
|
|
+ 'expected_color': '紫' # 应该保持预设颜色,不被覆盖
|
|
|
+ }
|
|
|
+ ]
|
|
|
+
|
|
|
+ for test_case in test_cases:
|
|
|
+ print(f"\n--- {test_case['name']} ---")
|
|
|
+
|
|
|
+ # 使用from_dict方法创建对象
|
|
|
+ calendar_info = CalendarInfo.from_dict(test_case['data'])
|
|
|
+
|
|
|
+ print(f"输入wuxing: {test_case['data'].get('wuxing', 'None')}")
|
|
|
+ print(f"预设color: {test_case['data'].get('color', 'None')}")
|
|
|
+ print(f"实际color: {calendar_info.color}")
|
|
|
+ print(f"期望color: {test_case['expected_color']}")
|
|
|
+
|
|
|
+ # 验证结果
|
|
|
+ if calendar_info.color == test_case['expected_color']:
|
|
|
+ print("✅ 测试通过")
|
|
|
+ else:
|
|
|
+ print("❌ 测试失败")
|
|
|
+ return False
|
|
|
+
|
|
|
+ print("\n✅ 所有五行颜色映射测试通过!")
|
|
|
+ return True
|
|
|
+
|
|
|
+
|
|
|
+def test_save_calendar_from_api_color_mapping():
|
|
|
+ """测试CalendarService.save_calendar_from_api方法的颜色映射功能"""
|
|
|
+ print("\n=== 测试save_calendar_from_api颜色映射功能 ===")
|
|
|
+
|
|
|
+ # 创建服务实例(不连接数据库)
|
|
|
+ service = CalendarService()
|
|
|
+
|
|
|
+ # 模拟API数据
|
|
|
+ api_test_cases = [
|
|
|
+ {
|
|
|
+ 'name': 'API金元素测试',
|
|
|
+ 'api_data': {
|
|
|
+ 'yangli': '2025-01-20',
|
|
|
+ 'yinli': '腊月二十一',
|
|
|
+ 'wuxing': '白腊金 成执位', # 包含"金"
|
|
|
+ 'chongsha': '冲兔',
|
|
|
+ 'baiji': '甲不开仓',
|
|
|
+ 'jishen': '天德',
|
|
|
+ 'yi': '祭祀',
|
|
|
+ 'xionshen': '天刑',
|
|
|
+ 'ji': '嫁娶'
|
|
|
+ },
|
|
|
+ 'expected_color': '白'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': 'API火元素测试',
|
|
|
+ 'api_data': {
|
|
|
+ 'yangli': '2025-01-21',
|
|
|
+ 'yinli': '腊月二十二',
|
|
|
+ 'wuxing': '山头火 收执位', # 包含"火"
|
|
|
+ 'chongsha': '冲龙',
|
|
|
+ 'baiji': '乙不栽种',
|
|
|
+ 'jishen': '月德',
|
|
|
+ 'yi': '开光',
|
|
|
+ 'xionshen': '天火',
|
|
|
+ 'ji': '安葬'
|
|
|
+ },
|
|
|
+ 'expected_color': '红'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': 'API预设颜色测试',
|
|
|
+ 'api_data': {
|
|
|
+ 'yangli': '2025-01-22',
|
|
|
+ 'yinli': '腊月二十三',
|
|
|
+ 'wuxing': '大林木 开执位', # 包含"木"
|
|
|
+ 'color': '蓝', # API中已有颜色值
|
|
|
+ 'chongsha': '冲蛇',
|
|
|
+ 'baiji': '丙不修灶',
|
|
|
+ 'jishen': '天喜',
|
|
|
+ 'yi': '求嗣',
|
|
|
+ 'xionshen': '月刑',
|
|
|
+ 'ji': '破土'
|
|
|
+ },
|
|
|
+ 'expected_color': '蓝' # 应该保持API中的颜色,不被覆盖
|
|
|
+ }
|
|
|
+ ]
|
|
|
+
|
|
|
+ for test_case in api_test_cases:
|
|
|
+ print(f"\n--- {test_case['name']} ---")
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 模拟save_calendar_from_api中的颜色逻辑
|
|
|
+ api_data = test_case['api_data']
|
|
|
+ wuxing = api_data.get('wuxing', '') or ''
|
|
|
+ color = api_data.get('color') # 先获取API中的color值
|
|
|
+
|
|
|
+ # 如果API中没有color值,则根据wuxing字段判断五行元素设置颜色
|
|
|
+ if not color:
|
|
|
+ if '金' in wuxing:
|
|
|
+ color = '白'
|
|
|
+ elif '水' in wuxing:
|
|
|
+ color = '黑'
|
|
|
+ elif '木' in wuxing:
|
|
|
+ color = '绿'
|
|
|
+ elif '火' in wuxing:
|
|
|
+ color = '红'
|
|
|
+ elif '土' in wuxing:
|
|
|
+ color = '黄'
|
|
|
+
|
|
|
+ print(f"输入wuxing: {wuxing}")
|
|
|
+ print(f"API预设color: {api_data.get('color', 'None')}")
|
|
|
+ print(f"计算得出color: {color}")
|
|
|
+ print(f"期望color: {test_case['expected_color']}")
|
|
|
+
|
|
|
+ # 验证结果
|
|
|
+ if color == test_case['expected_color']:
|
|
|
+ print("✅ 测试通过")
|
|
|
+ else:
|
|
|
+ print("❌ 测试失败")
|
|
|
+ return False
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ print(f"❌ 测试异常: {e}")
|
|
|
+ return False
|
|
|
+
|
|
|
+ print("\n✅ 所有save_calendar_from_api颜色映射测试通过!")
|
|
|
+ return True
|
|
|
+
|
|
|
+
|
|
|
+def test_edge_cases():
|
|
|
+ """测试边界情况"""
|
|
|
+ print("\n=== 测试边界情况 ===")
|
|
|
+
|
|
|
+ test_cases = [
|
|
|
+ {
|
|
|
+ 'name': '空wuxing字段测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-27',
|
|
|
+ 'yinli': '腊月二十八',
|
|
|
+ 'wuxing': '', # 空字符串
|
|
|
+ },
|
|
|
+ 'expected_color': None
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': 'None wuxing字段测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-28',
|
|
|
+ 'yinli': '腊月二十九',
|
|
|
+ # 没有wuxing字段
|
|
|
+ },
|
|
|
+ 'expected_color': None
|
|
|
+ },
|
|
|
+ {
|
|
|
+ 'name': '含有五行字符但非五行元素测试',
|
|
|
+ 'data': {
|
|
|
+ 'yangli': '2025-01-29',
|
|
|
+ 'yinli': '腊月三十',
|
|
|
+ 'wuxing': '建执位 满执位', # 不包含任何五行元素
|
|
|
+ },
|
|
|
+ 'expected_color': None
|
|
|
+ }
|
|
|
+ ]
|
|
|
+
|
|
|
+ for test_case in test_cases:
|
|
|
+ print(f"\n--- {test_case['name']} ---")
|
|
|
+
|
|
|
+ calendar_info = CalendarInfo.from_dict(test_case['data'])
|
|
|
+
|
|
|
+ print(f"输入wuxing: {test_case['data'].get('wuxing', 'None')}")
|
|
|
+ print(f"实际color: {calendar_info.color}")
|
|
|
+ print(f"期望color: {test_case['expected_color']}")
|
|
|
+
|
|
|
+ if calendar_info.color == test_case['expected_color']:
|
|
|
+ print("✅ 测试通过")
|
|
|
+ else:
|
|
|
+ print("❌ 测试失败")
|
|
|
+ return False
|
|
|
+
|
|
|
+ print("\n✅ 所有边界情况测试通过!")
|
|
|
+ return True
|
|
|
+
|
|
|
+
|
|
|
+def main():
|
|
|
+ """主测试函数"""
|
|
|
+ print("开始测试黄历信息五行颜色映射功能...\n")
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 运行所有测试
|
|
|
+ test1_passed = test_wuxing_color_mapping()
|
|
|
+ test2_passed = test_save_calendar_from_api_color_mapping()
|
|
|
+ test3_passed = test_edge_cases()
|
|
|
+
|
|
|
+ if test1_passed and test2_passed and test3_passed:
|
|
|
+ print("\n🎉 所有五行颜色映射测试通过!")
|
|
|
+ print("\n五行颜色映射规则:")
|
|
|
+ print("- 金 → 白")
|
|
|
+ print("- 水 → 黑")
|
|
|
+ print("- 木 → 绿")
|
|
|
+ print("- 火 → 红")
|
|
|
+ print("- 土 → 黄")
|
|
|
+ print("\n功能特性:")
|
|
|
+ print("- ✅ 自动从wuxing字段识别五行元素")
|
|
|
+ print("- ✅ 自动设置对应的颜色值")
|
|
|
+ print("- ✅ 保留API或字典中预设的颜色值")
|
|
|
+ print("- ✅ 处理边界情况(空值、无五行元素等)")
|
|
|
+ print("- ✅ from_dict和save_calendar_from_api都支持此功能")
|
|
|
+ else:
|
|
|
+ print("\n❌ 部分测试失败!")
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ print(f"❌ 测试过程中发生错误: {e}")
|
|
|
+ import traceback
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|