test_parse_pic_fix.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. """
  3. 测试parse_pic.py的修复
  4. 验证batch_process_images函数能正确处理字典格式的输入
  5. """
  6. import sys
  7. import os
  8. import logging
  9. # 添加项目路径
  10. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  11. # 设置日志
  12. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(funcName)s - %(lineno)d - %(message)s')
  13. def test_batch_process_images_fix():
  14. """测试batch_process_images函数的修复"""
  15. print("测试batch_process_images函数的修复...")
  16. try:
  17. from app.core.data_parse.parse_pic import batch_process_images
  18. # 测试数据 - 模拟错误日志中的输入格式
  19. test_input = [
  20. {
  21. 'minio_path': 'http://192.168.3.143:9000/dataops-bucket/misc_files/misc_file_20250801_212256_b61ddd94.png',
  22. 'original_filename': '杂项表格样例.png',
  23. 'status': '正常'
  24. }
  25. ]
  26. print(f"测试输入: {test_input}")
  27. # 调用函数(这里只是测试参数处理,不会真正处理图片)
  28. # 由于没有真实的图片文件,我们期望函数能正确处理参数格式而不抛出类型错误
  29. result = batch_process_images(test_input, process_type='table')
  30. print(f"函数执行结果: {result}")
  31. print("✓ batch_process_images函数修复验证通过")
  32. except TypeError as e:
  33. if "expected str, bytes or os.PathLike object, not dict" in str(e):
  34. print(f"❌ 仍然存在类型错误: {e}")
  35. return False
  36. else:
  37. print(f"其他类型错误: {e}")
  38. return False
  39. except Exception as e:
  40. print(f"其他错误: {e}")
  41. return False
  42. return True
  43. def test_invalid_input_handling():
  44. """测试无效输入的处理"""
  45. print("\n测试无效输入处理...")
  46. try:
  47. from app.core.data_parse.parse_pic import batch_process_images
  48. # 测试各种无效输入
  49. invalid_inputs = [
  50. [{'minio_path': None}], # minio_path为None
  51. [{'minio_path': {}}], # minio_path为字典
  52. [{'minio_path': 123}], # minio_path为数字
  53. [{'wrong_field': 'value'}], # 缺少minio_path字段
  54. [{'minio_path': ''}], # minio_path为空字符串
  55. ]
  56. for i, invalid_input in enumerate(invalid_inputs):
  57. print(f"测试无效输入 {i+1}: {invalid_input}")
  58. try:
  59. result = batch_process_images(invalid_input, process_type='table')
  60. print(f" 处理结果: {result.get('success', False)}")
  61. # 期望处理失败但不抛出类型错误
  62. if not result.get('success', False):
  63. print(f" ✓ 正确处理了无效输入")
  64. else:
  65. print(f" ⚠ 意外成功处理了无效输入")
  66. except TypeError as e:
  67. if "expected str, bytes or os.PathLike object, not dict" in str(e):
  68. print(f" ❌ 仍然存在类型错误: {e}")
  69. return False
  70. else:
  71. print(f" ✓ 正确处理了类型错误: {e}")
  72. except Exception as e:
  73. print(f" ✓ 正确处理了其他错误: {e}")
  74. print("✓ 无效输入处理验证通过")
  75. return True
  76. except Exception as e:
  77. print(f"❌ 测试失败: {e}")
  78. return False
  79. if __name__ == "__main__":
  80. print("开始测试parse_pic.py的修复...")
  81. success = True
  82. # 测试基本功能
  83. if not test_batch_process_images_fix():
  84. success = False
  85. # 测试无效输入处理
  86. if not test_invalid_input_handling():
  87. success = False
  88. if success:
  89. print("\n🎉 所有测试通过!parse_pic.py修复成功。")
  90. else:
  91. print("\n❌ 部分测试失败,需要进一步修复。")
  92. sys.exit(1)