Files
my-daily/test_deduplicate.py
T

96 lines
3.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
去重逻辑测试用例
验证规则:
1. 同一订阅源内标题相同的文章仅保留一篇
2. 不同订阅源间标题相同的文章全部保留
3. 援引类文章(不同源相同标题)不会被错误去重
"""
def test_deduplicate():
# 模拟测试文章数据
test_articles = [
# 场景1:同一源两篇相同标题的文章 → 只保留1篇
{"title": "美军测试新型无人机", "source": "Defense One", "published": "2026-05-12 10:00"},
{"title": "美军测试新型无人机", "source": "Defense One", "published": "2026-05-12 10:30"},
# 场景2:不同源两篇相同标题的文章 → 都保留
{"title": "俄军列装新型导弹", "source": "The War Zone", "published": "2026-05-12 09:00"},
{"title": "俄军列装新型导弹", "source": "国防科技要闻", "published": "2026-05-12 09:10"},
# 场景3:援引类文章 → 都保留
{"title": "朝鲜试射洲际导弹", "source": "韩联社", "published": "2026-05-12 08:00"},
{"title": "朝鲜试射洲际导弹", "source": "央视军事", "published": "2026-05-12 08:20"},
{"title": "朝鲜试射洲际导弹", "source": "路透社", "published": "2026-05-12 08:30"},
# 短标题过滤
{"title": "快讯", "source": "海鹰资讯", "published": "2026-05-12 07:00"},
]
# 复现程序中的去重逻辑
seen = set()
unique_articles = []
for a in test_articles:
title = a['title'].strip()
if len(title) <= 5:
continue
key = (a['source'].strip(), title)
if key not in seen:
seen.add(key)
unique_articles.append(a)
print("="*60)
print("去重逻辑测试结果")
print("="*60)
print(f"输入文章总数:{len(test_articles)} 篇")
print(f"去重后剩余:{len(unique_articles)} 篇")
print("-"*60)
# 统计各源文章数验证结果
source_count = {}
for a in unique_articles:
source = a['source']
source_count[source] = source_count.get(source, 0) + 1
print(f"[{source}] {a['title']}")
print("-"*60)
# 验证测试用例
test_passed = True
# 验证1:同一源Defense One的两篇相同标题 → 仅保留1篇
if source_count.get("Defense One", 0) == 1:
print("✅ 测试1通过:同一源相同标题仅保留1篇")
else:
print("❌ 测试1失败:同一源相同标题保留了%d篇(预期1篇)" % source_count.get("Defense One", 0))
test_passed = False
# 验证2:不同源The War Zone和国防科技要闻相同标题 → 各保留1篇,共2篇
if source_count.get("The War Zone", 0) == 1 and source_count.get("国防科技要闻", 0) == 1:
print("✅ 测试2通过:不同源相同标题全部保留")
else:
print("❌ 测试2失败:不同源相同标题未全部保留")
test_passed = False
# 验证3:3个不同源相同标题的援引类文章 → 都保留,共3篇
if source_count.get("韩联社", 0) == 1 and source_count.get("央视军事", 0) == 1 and source_count.get("路透社", 0) == 1:
print("✅ 测试3通过:援引类不同源相同标题全部保留,未被错误去重")
else:
print("❌ 测试3失败:援引类文章被错误去重")
test_passed = False
# 验证4:短标题"快讯"被过滤
if source_count.get("海鹰资讯", 0) == 0:
print("✅ 测试4通过:长度<=5的短标题被正确过滤")
else:
print("❌ 测试4失败:短标题未被过滤")
test_passed = False
print("-"*60)
if test_passed:
print("🎉 所有测试用例通过!去重逻辑符合要求")
else:
print("⚠️ 存在测试用例未通过,请检查逻辑")
return test_passed
if __name__ == "__main__":
test_deduplicate()