-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_parser.py
More file actions
71 lines (55 loc) · 2.6 KB
/
Copy pathtest_parser.py
File metadata and controls
71 lines (55 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from parse_tsv import parse_tsv_file, get_product_sample
def main():
"""测试数据解析模块"""
print("=== 开始测试数据解析模块 ===")
# 测试文件路径
test_file = os.path.join('data', 'test_sample.tsv')
# 确认文件存在
if not os.path.exists(test_file):
print(f"错误: 测试文件不存在 - {test_file}")
return False
try:
# 解析测试数据
print(f"正在解析文件: {test_file}")
products = parse_tsv_file(test_file)
# 验证结果
if not products:
print("错误: 解析结果为空")
return False
print(f"成功解析 {len(products)} 个产品")
# 打印所有产品,因为测试样本较少
for i, product in enumerate(products):
print(f"\n产品 {i+1}:")
print(f" ID: {product.get('product_id', '未知')}")
print(f" 名称: {product.get('text-base', '未知')}")
print(f" 链接: {product.get('text-base href', '未知')}")
print(f" 描述: {product.get('text-sm', '未知')}")
print(f" 收入: {product.get('text-base (3)', '未知')}")
print(f" 排名: {product.get('text-center', '未知')}")
print(f" 图片URL: {product.get('el-image__inner src', '无')}")
# 验证字段是否正确处理
print("\n=== 验证字段处理 ===")
# 验证空值处理
empty_image_product = next((p for p in products if p.get('text-base') == '产品D'), None)
if empty_image_product:
print(f"空图片URL处理: {'正确' if empty_image_product.get('el-image__inner src') == '' else '错误'}")
empty_revenue_product = next((p for p in products if p.get('text-base') == '产品E'), None)
if empty_revenue_product:
print(f"空收入处理: {'正确' if empty_revenue_product.get('text-base (3)') == '未知收入' else '错误'}")
# 验证产品ID是否正确分配
id_check = all(p.get('product_id') for p in products)
print(f"产品ID分配: {'正确' if id_check else '错误'}")
print("\n=== 测试完成 ===")
return True
except Exception as e:
print(f"测试过程中出错: {e}")
return False
if __name__ == "__main__":
# 确保logs目录存在
if not os.path.exists('logs'):
os.makedirs('logs')
success = main()
print(f"\n测试结果: {'成功' if success else '失败'}")