test_formatting.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env python3
  2. """
  3. 测试年龄格式化和日期格式化功能
  4. """
  5. import os
  6. import sys
  7. import logging
  8. from pathlib import Path
  9. # 添加项目根目录到Python路径
  10. project_root = Path(__file__).parent
  11. sys.path.insert(0, str(project_root))
  12. # 配置日志
  13. logging.basicConfig(
  14. level=logging.INFO,
  15. format='%(asctime)s - %(levelname)s - %(message)s'
  16. )
  17. def test_formatting_functions():
  18. """测试格式化函数"""
  19. try:
  20. # 导入函数
  21. from app.core.data_parse.parse_resume import format_date_to_yyyy_mm_dd
  22. print("开始测试日期格式化功能...")
  23. # 测试各种日期格式
  24. test_dates = [
  25. "2023.11",
  26. "2023-11",
  27. "2023年11月",
  28. "2023/11",
  29. "202311",
  30. "2023.11.01",
  31. "2023-11-01",
  32. "2023年11月1日",
  33. "2023/11/01",
  34. "2009.08",
  35. "2013.11",
  36. "2017.04",
  37. "2019.12",
  38. "无效日期",
  39. ""
  40. ]
  41. print("\n=== 日期格式化测试 ===")
  42. for test_date in test_dates:
  43. formatted = format_date_to_yyyy_mm_dd(test_date)
  44. print(f"原始: '{test_date}' -> 格式化: '{formatted}'")
  45. return True
  46. except Exception as e:
  47. print(f"测试失败: {str(e)}")
  48. import traceback
  49. traceback.print_exc()
  50. return False
  51. def test_age_formatting():
  52. """测试年龄格式化功能"""
  53. try:
  54. print("\n开始测试年龄格式化功能...")
  55. # 测试年龄字段映射
  56. test_ages = [
  57. "37岁",
  58. "25岁",
  59. "42岁",
  60. "30",
  61. "年龄不详",
  62. ""
  63. ]
  64. print("\n=== 年龄格式化测试 ===")
  65. for test_age in test_ages:
  66. import re
  67. age_match = re.search(r'(\d+)', str(test_age))
  68. if age_match:
  69. formatted_age = age_match.group(1)
  70. print(f"原始: '{test_age}' -> 格式化: '{formatted_age}'")
  71. else:
  72. print(f"原始: '{test_age}' -> 格式化: ''")
  73. return True
  74. except Exception as e:
  75. print(f"测试失败: {str(e)}")
  76. import traceback
  77. traceback.print_exc()
  78. return False
  79. def test_full_resume_parsing():
  80. """测试完整的简历解析功能"""
  81. try:
  82. # 导入函数
  83. from app.core.data_parse.parse_resume import parse_resume_with_qwen
  84. # 测试文件路径
  85. test_file = "方全.pdf"
  86. # 检查文件是否存在
  87. if not os.path.exists(test_file):
  88. print(f"错误: 测试文件 {test_file} 不存在")
  89. return False
  90. print(f"\n开始测试完整简历解析功能...")
  91. print(f"测试文件: {test_file}")
  92. # 调用函数
  93. result = parse_resume_with_qwen(test_file)
  94. # 输出格式化后的结果
  95. print("\n=== 格式化后的解析结果 ===")
  96. print(f"姓名(中文): {result.get('name_zh', 'N/A')}")
  97. print(f"年龄: {result.get('age', 'N/A')}")
  98. # 输出职业轨迹
  99. career_path = result.get('career_path', [])
  100. if career_path:
  101. print(f"\n职业轨迹 (共{len(career_path)}条):")
  102. for i, career in enumerate(career_path, 1):
  103. print(f" {i}. 日期: {career.get('date', 'N/A')}")
  104. print(f" 公司: {career.get('hotel_zh', 'N/A')}")
  105. print(f" 职位: {career.get('title_zh', 'N/A')}")
  106. else:
  107. print("\n职业轨迹: 无")
  108. print("\n=== 完整测试完成 ===")
  109. return True
  110. except Exception as e:
  111. print(f"测试失败: {str(e)}")
  112. import traceback
  113. traceback.print_exc()
  114. return False
  115. if __name__ == "__main__":
  116. print("=" * 50)
  117. print("测试格式化功能")
  118. print("=" * 50)
  119. # 测试日期格式化
  120. success1 = test_formatting_functions()
  121. # 测试年龄格式化
  122. success2 = test_age_formatting()
  123. # 测试完整简历解析
  124. success3 = test_full_resume_parsing()
  125. print("\n" + "=" * 50)
  126. print("测试总结:")
  127. print(f"日期格式化测试: {'✓ 成功' if success1 else '✗ 失败'}")
  128. print(f"年龄格式化测试: {'✓ 成功' if success2 else '✗ 失败'}")
  129. print(f"完整简历解析测试: {'✓ 成功' if success3 else '✗ 失败'}")
  130. print("=" * 50)