123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- """
- 测试日历API集成功能
- """
- 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 get_calendar_by_date, CalendarService
- def test_calendar_api_integration():
- """测试日历API集成功能"""
- print("=== 测试日历API集成功能 ===")
-
- # 测试用例1: 测试一个可能不在数据库中的日期
- print("\n1. 测试API集成功能:")
- test_date = "2025-08-24" # 这个日期可能不在数据库中
- print(f"查询日期: {test_date}")
-
- try:
- result = get_calendar_by_date(test_date)
- print(f"查询结果: {result}")
-
- if result.get('return_code') == 200:
- print("✅ 查询成功!")
- if result.get('result'):
- print(f" - ID: {result['result'].get('id')}")
- print(f" - 阳历: {result['result'].get('yangli')}")
- print(f" - 阴历: {result['result'].get('yinli')}")
- print(f" - 五行: {result['result'].get('wuxing')}")
- else:
- print(f"❌ 查询失败: {result.get('error')}")
-
- except Exception as e:
- print(f"❌ 测试过程中发生异常: {e}")
- import traceback
- traceback.print_exc()
-
- print("\n✓ API集成测试完成!")
- def test_calendar_service_methods():
- """测试CalendarService的新方法"""
- print("\n=== 测试CalendarService的新方法 ===")
-
- try:
- # 创建服务实例
- service = CalendarService()
- print("✅ CalendarService实例创建成功")
-
- # 测试fetch_calendar_from_api方法
- test_date = date(2025, 8, 24)
- print(f"\n测试fetch_calendar_from_api方法,日期: {test_date}")
-
- if hasattr(service, 'fetch_calendar_from_api'):
- print("✅ fetch_calendar_from_api方法存在")
- else:
- print("❌ fetch_calendar_from_api方法不存在")
-
- if hasattr(service, 'save_calendar_from_api'):
- print("✅ save_calendar_from_api方法存在")
- else:
- print("❌ save_calendar_from_api方法不存在")
-
- except Exception as e:
- print(f"❌ 测试过程中发生异常: {e}")
- def test_config_import():
- """测试配置文件导入"""
- print("\n=== 测试配置文件导入 ===")
-
- try:
- from app.core.data_parse.calendar_config import CALENDAR_API_CONFIG
- print("✅ 配置文件导入成功")
- print(f"API配置: {CALENDAR_API_CONFIG}")
-
- except ImportError as e:
- print(f"❌ 配置文件导入失败: {e}")
- except Exception as e:
- print(f"❌ 其他错误: {e}")
- def main():
- """主测试函数"""
- print("开始测试日历API集成功能...\n")
-
- try:
- test_config_import()
- test_calendar_service_methods()
- test_calendar_api_integration()
-
- print("\n🎉 所有测试完成!")
- print("\n新增功能特性:")
- print("- ✅ 当数据库中没有数据时自动调用外部API")
- print("- ✅ API数据自动保存到数据库")
- print("- ✅ 配置文件管理API设置")
- print("- ✅ 完整的错误处理和日志记录")
- print("- ✅ 支持API数据格式转换")
-
- except Exception as e:
- print(f"❌ 测试失败: {e}")
- import traceback
- traceback.print_exc()
- if __name__ == "__main__":
- main()
|