-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_core.py
More file actions
298 lines (245 loc) · 10.8 KB
/
Copy pathtest_core.py
File metadata and controls
298 lines (245 loc) · 10.8 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
InsightPulse 核心模块测试
"""
import sys
import os
import json
import time
import unittest
# 确保可以导入模块
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from insightpulse.nlp.analyzer import TextAnalyzer
from insightpulse.core.models import ContentItem, SearchResult, TrendData
from insightpulse.core.config import Config
from insightpulse.core.cache import CacheManager
from insightpulse.core.database import Database
from insightpulse.sources.common import (
extract_text, clean_html, parse_relative_time, truncate_text
)
class TestTextAnalyzer(unittest.TestCase):
"""文本分析器测试"""
def setUp(self):
self.analyzer = TextAnalyzer()
def test_extract_keywords_chinese(self):
"""测试中文关键词提取"""
text = "人工智能大模型是当前科技领域最热门的话题,GPT和Claude等大语言模型正在改变世界"
keywords = self.analyzer.extract_keywords(text, top_n=5)
self.assertIsInstance(keywords, list)
self.assertTrue(len(keywords) > 0)
self.assertTrue(all(isinstance(kw, str) for kw in keywords))
def test_extract_keywords_english(self):
"""测试英文关键词提取"""
text = "Python is a great programming language for machine learning and artificial intelligence"
keywords = self.analyzer.extract_keywords(text, top_n=5)
self.assertIsInstance(keywords, list)
self.assertTrue(len(keywords) > 0)
def test_extract_keywords_empty(self):
"""测试空文本"""
keywords = self.analyzer.extract_keywords("")
self.assertEqual(keywords, [])
def test_analyze_sentiment_positive(self):
"""测试正面情感分析"""
text = "这个产品非常好用,功能强大,推荐给大家"
sentiment = self.analyzer.analyze_sentiment(text)
self.assertEqual(sentiment, "positive")
def test_analyze_sentiment_negative(self):
"""测试负面情感分析"""
text = "这个软件太差了,经常崩溃,有很多bug,非常失望"
sentiment = self.analyzer.analyze_sentiment(text)
self.assertEqual(sentiment, "negative")
def test_analyze_sentiment_neutral(self):
"""测试中性情感分析"""
text = "今天会议讨论了项目进度"
sentiment = self.analyzer.analyze_sentiment(text)
self.assertEqual(sentiment, "neutral")
def test_analyze_sentiment_empty(self):
"""测试空文本情感"""
sentiment = self.analyzer.analyze_sentiment("")
self.assertEqual(sentiment, "neutral")
def test_summarize(self):
"""测试文本摘要"""
text = "人工智能是计算机科学的一个分支。它试图理解智能的实质。并生产出一种新的能以人类智能相似的方式做出反应的智能机器。人工智能的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。"
summary = self.analyzer.summarize(text, max_sentences=2)
self.assertIsInstance(summary, str)
self.assertTrue(len(summary) > 0)
self.assertTrue(len(summary) <= len(text))
def test_detect_language_chinese(self):
"""测试中文语言检测"""
result = self.analyzer.detect_language("这是一个中文测试")
self.assertEqual(result, "zh")
def test_detect_language_english(self):
"""测试英文语言检测"""
result = self.analyzer.detect_language("This is an English test")
self.assertEqual(result, "en")
def test_detect_language_empty(self):
"""测试空文本语言检测"""
result = self.analyzer.detect_language("")
self.assertEqual(result, "unknown")
class TestModels(unittest.TestCase):
"""数据模型测试"""
def test_content_item_creation(self):
"""测试内容项创建"""
item = ContentItem(
title="测试标题",
url="https://example.com",
source="zhihu",
author="测试作者",
)
self.assertEqual(item.title, "测试标题")
self.assertEqual(item.source, "zhihu")
self.assertEqual(item.relevance_score, 0.0)
def test_content_item_to_dict(self):
"""测试内容项序列化"""
item = ContentItem(title="测试", url="https://example.com", source="test")
d = item.to_dict()
self.assertIsInstance(d, dict)
self.assertEqual(d["title"], "测试")
self.assertIn("source", d)
def test_search_result_creation(self):
"""测试搜索结果创建"""
result = SearchResult(keyword="AI", total_items=10, sources_used=["zhihu", "weibo"])
self.assertEqual(result.keyword, "AI")
self.assertEqual(result.total_items, 10)
def test_search_result_to_dict(self):
"""测试搜索结果序列化"""
item = ContentItem(title="测试", source="test")
result = SearchResult(keyword="test", items=[item], sources_used=["test"])
d = result.to_dict()
self.assertIsInstance(d, dict)
self.assertEqual(d["keyword"], "test")
def test_search_result_to_markdown(self):
"""测试Markdown输出"""
item = ContentItem(title="测试标题", source="zhihu", author="作者", summary="测试摘要")
result = SearchResult(keyword="AI", items=[item], sources_used=["zhihu"])
md = result.to_markdown()
self.assertIn("AI", md)
self.assertIn("测试标题", md)
self.assertIn("知乎", md)
def test_search_result_to_html(self):
"""测试HTML输出"""
item = ContentItem(title="测试标题", source="zhihu", summary="测试摘要")
result = SearchResult(keyword="AI", items=[item], sources_used=["zhihu"])
html = result.to_html()
self.assertIn("<!DOCTYPE html>", html)
self.assertIn("AI", html)
def test_trend_data(self):
"""测试趋势数据"""
trend = TrendData(keyword="AI", period_days=7, peak_value=100, avg_value=50.0, growth_rate=25.0)
d = trend.to_dict()
self.assertEqual(d["keyword"], "AI")
self.assertEqual(d["peak_value"], 100)
self.assertEqual(d["growth_rate"], 25.0)
class TestCommonUtils(unittest.TestCase):
"""通用工具测试"""
def test_extract_text(self):
"""测试HTML文本提取"""
html = "<h1>标题</h1><p>正文内容</p><script>var x = 1;</script>"
text = extract_text(html)
self.assertIn("标题", text)
self.assertIn("正文内容", text)
self.assertNotIn("var x", text)
def test_clean_html(self):
"""测试HTML清理"""
html = "<p>Hello</p><br/>World<script>alert(1)</script>"
cleaned = clean_html(html)
self.assertNotIn("<p>", cleaned)
self.assertNotIn("<script>", cleaned)
self.assertIn("Hello", cleaned)
def test_parse_relative_time_chinese(self):
"""测试中文相对时间解析"""
result = parse_relative_time("3小时前")
self.assertIsNotNone(result)
self.assertIn("T", result)
def test_parse_relative_time_none(self):
"""测试空时间"""
result = parse_relative_time("")
self.assertIsNone(result)
def test_truncate_text(self):
"""测试文本截断"""
text = "a" * 1000
truncated = truncate_text(text, 100)
self.assertTrue(len(truncated) <= 110)
def test_truncate_text_short(self):
"""测试短文本不截断"""
text = "hello"
truncated = truncate_text(text, 100)
self.assertEqual(truncated, "hello")
class TestConfig(unittest.TestCase):
"""配置模块测试"""
def test_default_config(self):
"""测试默认配置"""
config = Config()
self.assertEqual(config.default_days, 30)
self.assertEqual(config.language, "zh-CN")
self.assertTrue(len(config.sources) > 0)
def test_config_set(self):
"""测试配置设置"""
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
config = Config(cache_dir=tmpdir, db_path=os.path.join(tmpdir, "test.db"))
config.set("default_days", "7")
self.assertEqual(config.default_days, 7)
def test_config_display(self):
"""测试配置显示"""
config = Config()
# 不应抛出异常
config.display()
class TestCache(unittest.TestCase):
"""缓存模块测试"""
def test_cache_set_get(self):
"""测试缓存读写"""
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
config = Config(cache_dir=tmpdir)
cache = CacheManager(config)
cache.set("test", "zhihu", 30, {"items": [{"title": "测试"}], "total": 1})
result = cache.get("test", "zhihu", 30)
self.assertIsNotNone(result)
self.assertEqual(result["total"], 1)
def test_cache_miss(self):
"""测试缓存未命中"""
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
config = Config(cache_dir=tmpdir)
cache = CacheManager(config)
result = cache.get("nonexistent", "zhihu", 30)
self.assertIsNone(result)
def test_cache_clear(self):
"""测试缓存清除"""
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
config = Config(cache_dir=tmpdir)
cache = CacheManager(config)
cache.set("test", "zhihu", 30, {"total": 1})
cache.clear()
result = cache.get("test", "zhihu", 30)
self.assertIsNone(result)
class TestDatabase(unittest.TestCase):
"""数据库模块测试"""
def test_database_init(self):
"""测试数据库初始化"""
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
config = Config(db_path=os.path.join(tmpdir, "test.db"))
db = Database(config)
stats = db.get_stats()
self.assertEqual(stats["total_searches"], 0)
self.assertEqual(stats["total_items"], 0)
def test_database_save_and_retrieve(self):
"""测试数据库保存和检索"""
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
config = Config(db_path=os.path.join(tmpdir, "test.db"))
db = Database(config)
items = [
ContentItem(title="测试1", url="https://example.com/1", source="zhihu"),
ContentItem(title="测试2", url="https://example.com/2", source="weibo"),
]
db.save_search("AI", items, ["zhihu", "weibo"])
history = db.get_search_history("AI", 30)
self.assertTrue(len(history) > 0)
stats = db.get_stats()
self.assertEqual(stats["total_searches"], 1)
self.assertEqual(stats["total_items"], 2)
if __name__ == "__main__":
unittest.main(verbosity=2)