trendscout 1.1.0__tar.gz → 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {trendscout-1.1.0 → trendscout-1.2.0}/PKG-INFO +5 -1
- {trendscout-1.1.0 → trendscout-1.2.0}/README.md +1 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/pyproject.toml +8 -1
- trendscout-1.2.0/tests/test_basic.py +496 -0
- trendscout-1.2.0/tests/test_schemas_mcp.py +64 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout/__init__.py +46 -2
- trendscout-1.2.0/trendscout/mcp_server.py +175 -0
- trendscout-1.2.0/trendscout/schemas.py +80 -0
- trendscout-1.2.0/trendscout/sources/github_trending.py +124 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout/sources/rss.py +2 -1
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout.egg-info/PKG-INFO +5 -1
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout.egg-info/SOURCES.txt +5 -0
- trendscout-1.2.0/trendscout.egg-info/entry_points.txt +2 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout.egg-info/requires.txt +4 -0
- trendscout-1.1.0/tests/test_basic.py +0 -198
- {trendscout-1.1.0 → trendscout-1.2.0}/setup.cfg +0 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout/scraper.py +0 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout/sentiment.py +0 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout/sources/__init__.py +0 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout/sources/hackernews.py +0 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout/velocity.py +0 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout.egg-info/dependency_links.txt +0 -0
- {trendscout-1.1.0 → trendscout-1.2.0}/trendscout.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: trendscout
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Real-time trend intelligence with HackerNews, RSS feeds, trend velocity, and multi-domain sentiment
|
|
5
5
|
Author-email: Teyfik OZ <teyfikoz@yahoo.com>
|
|
6
6
|
License: MIT
|
|
@@ -25,12 +25,16 @@ Requires-Dist: textblob>=0.17.0
|
|
|
25
25
|
Provides-Extra: dev
|
|
26
26
|
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
27
27
|
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
28
|
+
Provides-Extra: mcp
|
|
29
|
+
Requires-Dist: mcp>=1.0; extra == "mcp"
|
|
30
|
+
Requires-Dist: pydantic>=2.0; extra == "mcp"
|
|
28
31
|
|
|
29
32
|
# TrendScout
|
|
30
33
|
|
|
31
34
|
**Real-time trend intelligence** — HackerNews search, RSS feed parsing, trend velocity scoring, and multi-domain sentiment analysis. No API key required.
|
|
32
35
|
|
|
33
36
|
[](https://pypi.org/project/trendscout/)
|
|
37
|
+
[](https://github.com/teyfikoz/trendscout/actions/workflows/publish.yml)
|
|
34
38
|
[](https://github.com/teyfikoz/trendscout/actions/workflows/ci.yml)
|
|
35
39
|
[](https://www.python.org/)
|
|
36
40
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
**Real-time trend intelligence** — HackerNews search, RSS feed parsing, trend velocity scoring, and multi-domain sentiment analysis. No API key required.
|
|
4
4
|
|
|
5
5
|
[](https://pypi.org/project/trendscout/)
|
|
6
|
+
[](https://github.com/teyfikoz/trendscout/actions/workflows/publish.yml)
|
|
6
7
|
[](https://github.com/teyfikoz/trendscout/actions/workflows/ci.yml)
|
|
7
8
|
[](https://www.python.org/)
|
|
8
9
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "trendscout"
|
|
7
|
-
version = "1.
|
|
7
|
+
version = "1.2.0"
|
|
8
8
|
description = "Real-time trend intelligence with HackerNews, RSS feeds, trend velocity, and multi-domain sentiment"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [
|
|
@@ -35,6 +35,13 @@ dev = [
|
|
|
35
35
|
"pytest>=7.0",
|
|
36
36
|
"ruff>=0.1.0",
|
|
37
37
|
]
|
|
38
|
+
mcp = [
|
|
39
|
+
"mcp>=1.0",
|
|
40
|
+
"pydantic>=2.0",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[project.scripts]
|
|
44
|
+
trendscout-mcp = "trendscout.mcp_server:main"
|
|
38
45
|
|
|
39
46
|
[project.urls]
|
|
40
47
|
Homepage = "https://github.com/teyfikoz/trendscout"
|
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
"""Tests for TrendScout v1.0.0."""
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_version():
|
|
8
|
+
import trendscout
|
|
9
|
+
assert trendscout.__version__ == "1.2.0"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_all_exports():
|
|
13
|
+
from trendscout import TrendScout, TrendScraper, SentimentAnalyzer, SentimentResult
|
|
14
|
+
from trendscout import TrendVelocity, VelocityResult
|
|
15
|
+
assert all(cls is not None for cls in [TrendScout, SentimentAnalyzer, TrendVelocity])
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# ── SentimentAnalyzer ─────────────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
def test_sentiment_basic():
|
|
21
|
+
from trendscout import SentimentAnalyzer
|
|
22
|
+
analyzer = SentimentAnalyzer()
|
|
23
|
+
result = analyzer.analyze("This is a fantastic product!")
|
|
24
|
+
assert "polarity" in result
|
|
25
|
+
assert "label" in result
|
|
26
|
+
assert "intensity" in result
|
|
27
|
+
assert "market_signal" in result
|
|
28
|
+
assert isinstance(result["polarity"], float)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_sentiment_positive():
|
|
32
|
+
from trendscout import SentimentAnalyzer
|
|
33
|
+
analyzer = SentimentAnalyzer()
|
|
34
|
+
result = analyzer.analyze("Incredible growth, record profits, soaring revenue!")
|
|
35
|
+
assert result["label"] == "positive"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_sentiment_negative():
|
|
39
|
+
from trendscout import SentimentAnalyzer
|
|
40
|
+
analyzer = SentimentAnalyzer()
|
|
41
|
+
result = analyzer.analyze("Terrible crash, massive loss, declining performance.")
|
|
42
|
+
assert result["label"] == "negative"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_sentiment_rich_result():
|
|
46
|
+
from trendscout import SentimentAnalyzer, SentimentResult
|
|
47
|
+
analyzer = SentimentAnalyzer()
|
|
48
|
+
result = analyzer.analyze_rich("The market surged 30% today.")
|
|
49
|
+
assert isinstance(result, SentimentResult)
|
|
50
|
+
assert hasattr(result, "market_signal")
|
|
51
|
+
assert hasattr(result, "intensity")
|
|
52
|
+
assert hasattr(result, "confidence")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_sentiment_domain_financial():
|
|
56
|
+
from trendscout import SentimentAnalyzer
|
|
57
|
+
analyzer = SentimentAnalyzer()
|
|
58
|
+
r_bullish = analyzer.analyze("Revenue surged 40% beating all analyst estimates.", domain="financial")
|
|
59
|
+
r_bearish = analyzer.analyze("Massive crash, terrible loss, slump continues.", domain="financial")
|
|
60
|
+
assert r_bullish["polarity"] > r_bearish["polarity"]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_sentiment_dataframe():
|
|
64
|
+
from trendscout import SentimentAnalyzer
|
|
65
|
+
analyzer = SentimentAnalyzer()
|
|
66
|
+
df = pd.DataFrame({"text": ["Great!", "Terrible crash!", "Neutral news"]})
|
|
67
|
+
result = analyzer.analyze_dataframe(df, "text")
|
|
68
|
+
assert "polarity" in result.columns
|
|
69
|
+
assert "sentiment_label" in result.columns
|
|
70
|
+
assert "market_signal" in result.columns
|
|
71
|
+
assert len(result) == 3
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# ── TrendScraper (mock fallback) ──────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
def test_trend_scraper_mock():
|
|
77
|
+
from trendscout import TrendScraper
|
|
78
|
+
scraper = TrendScraper(use_mock=True)
|
|
79
|
+
df = scraper.get_trends("technology", limit=5)
|
|
80
|
+
assert isinstance(df, pd.DataFrame)
|
|
81
|
+
assert "title" in df.columns
|
|
82
|
+
assert len(df) > 0
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_trend_scraper_volatility():
|
|
86
|
+
from trendscout import TrendScraper
|
|
87
|
+
scraper = TrendScraper()
|
|
88
|
+
v = scraper.get_volatility("AI")
|
|
89
|
+
assert "keyword" in v
|
|
90
|
+
assert "volatility" in v
|
|
91
|
+
assert 0.0 <= v["volatility"] <= 1.0
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# ── TrendVelocity ─────────────────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
def test_velocity_empty_stories():
|
|
97
|
+
from trendscout import TrendVelocity
|
|
98
|
+
tv = TrendVelocity()
|
|
99
|
+
result = tv.calculate("empty topic", [])
|
|
100
|
+
assert result.status == "unknown"
|
|
101
|
+
assert result.score == 0.0
|
|
102
|
+
assert result.article_count == 0
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_velocity_high_engagement():
|
|
106
|
+
from trendscout import TrendVelocity
|
|
107
|
+
tv = TrendVelocity()
|
|
108
|
+
stories = [{"score": 500, "num_comments": 100} for _ in range(25)]
|
|
109
|
+
result = tv.calculate("viral topic", stories)
|
|
110
|
+
assert result.score > 0.5
|
|
111
|
+
assert result.status in ("emerging", "peak")
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_velocity_low_engagement():
|
|
115
|
+
from trendscout import TrendVelocity
|
|
116
|
+
tv = TrendVelocity()
|
|
117
|
+
stories = [{"score": 5, "num_comments": 1} for _ in range(2)]
|
|
118
|
+
result = tv.calculate("niche topic", stories)
|
|
119
|
+
assert result.status in ("declining", "stable", "peak")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# ── TrendScout facade ─────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
def test_trendscout_analyze_sentiment():
|
|
125
|
+
from trendscout import TrendScout, SentimentResult
|
|
126
|
+
ts = TrendScout()
|
|
127
|
+
result = ts.analyze_sentiment("The market is booming with record highs!")
|
|
128
|
+
assert isinstance(result, SentimentResult)
|
|
129
|
+
assert result.label in ("positive", "negative", "neutral")
|
|
130
|
+
assert 0.0 <= result.confidence <= 1.0
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_trendscout_analyze_dataframe():
|
|
134
|
+
from trendscout import TrendScout
|
|
135
|
+
ts = TrendScout()
|
|
136
|
+
df = pd.DataFrame({"headline": ["Great news!", "Terrible losses", "Mixed results"]})
|
|
137
|
+
result = ts.analyze_dataframe(df, "headline")
|
|
138
|
+
assert "sentiment_label" in result.columns
|
|
139
|
+
assert len(result) == 3
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_trendscout_backward_compat_volatility():
|
|
143
|
+
from trendscout import TrendScout
|
|
144
|
+
ts = TrendScout()
|
|
145
|
+
result = ts.get_trend_volatility("python")
|
|
146
|
+
assert "keyword" in result
|
|
147
|
+
assert "volatility" in result
|
|
148
|
+
assert "status" in result
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_trendscout_emerging_trends_fallback():
|
|
152
|
+
from trendscout import TrendScout
|
|
153
|
+
ts = TrendScout()
|
|
154
|
+
df = ts.get_emerging_trends(domain="technology", limit=5)
|
|
155
|
+
assert isinstance(df, pd.DataFrame)
|
|
156
|
+
assert len(df) > 0
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# ── RSS Source (unit test — no network needed) ────────────────────────────────
|
|
160
|
+
|
|
161
|
+
def test_rss_parse_rss2():
|
|
162
|
+
from trendscout.sources.rss import RSSFeedSource
|
|
163
|
+
rss = RSSFeedSource()
|
|
164
|
+
xml = """<?xml version="1.0"?>
|
|
165
|
+
<rss version="2.0"><channel>
|
|
166
|
+
<item><title>Test Article</title><link>https://example.com/1</link>
|
|
167
|
+
<description>A test item</description><pubDate>Sat, 24 May 2026 00:00:00 GMT</pubDate></item>
|
|
168
|
+
</channel></rss>"""
|
|
169
|
+
items = rss._parse(xml, "test-feed", 10)
|
|
170
|
+
assert len(items) == 1
|
|
171
|
+
assert items[0].title == "Test Article"
|
|
172
|
+
assert items[0].url == "https://example.com/1"
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def test_rss_parse_atom():
|
|
176
|
+
from trendscout.sources.rss import RSSFeedSource
|
|
177
|
+
rss = RSSFeedSource()
|
|
178
|
+
xml = """<?xml version="1.0"?>
|
|
179
|
+
<feed xmlns="http://www.w3.org/2005/Atom">
|
|
180
|
+
<entry>
|
|
181
|
+
<title>Atom Article</title>
|
|
182
|
+
<link href="https://example.com/atom/1"/>
|
|
183
|
+
<summary>An atom entry</summary>
|
|
184
|
+
<updated>2026-05-24T00:00:00Z</updated>
|
|
185
|
+
</entry>
|
|
186
|
+
</feed>"""
|
|
187
|
+
items = rss._parse(xml, "atom-feed", 10)
|
|
188
|
+
assert len(items) == 1
|
|
189
|
+
assert items[0].title == "Atom Article"
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
# ── HackerNews Source (instantiation only — no live calls in tests) ───────────
|
|
193
|
+
|
|
194
|
+
def test_hackernews_source_instantiation():
|
|
195
|
+
from trendscout.sources.hackernews import HackerNewsSource
|
|
196
|
+
hn = HackerNewsSource()
|
|
197
|
+
assert hn is not None
|
|
198
|
+
assert hn.timeout == 10
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# ── HackerNews story to_dict ─────────────────────────────────────────────────
|
|
202
|
+
|
|
203
|
+
def test_hackernews_story_to_dict():
|
|
204
|
+
from trendscout.sources.hackernews import HNStory
|
|
205
|
+
story = HNStory(id=1, title="Test", url="https://example.com", score=100,
|
|
206
|
+
num_comments=50, author="user", created_at="2026-01-01")
|
|
207
|
+
d = story.to_dict()
|
|
208
|
+
assert d["title"] == "Test"
|
|
209
|
+
assert d["score"] == 100
|
|
210
|
+
assert d["source"] == "hackernews"
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
# ── HackerNews search returns [] on network error (mocked) ───────────────────
|
|
214
|
+
|
|
215
|
+
def test_hackernews_search_network_error(monkeypatch):
|
|
216
|
+
from trendscout.sources.hackernews import HackerNewsSource
|
|
217
|
+
import requests
|
|
218
|
+
|
|
219
|
+
def raise_exc(*args, **kwargs):
|
|
220
|
+
raise requests.exceptions.ConnectionError("no network")
|
|
221
|
+
|
|
222
|
+
hn = HackerNewsSource(timeout=1)
|
|
223
|
+
monkeypatch.setattr(hn._session, "get", raise_exc)
|
|
224
|
+
result = hn.search("ai", limit=5)
|
|
225
|
+
assert result == []
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
# ── RSS item to_dict ──────────────────────────────────────────────────────────
|
|
229
|
+
|
|
230
|
+
def test_rss_item_to_dict():
|
|
231
|
+
from trendscout.sources.rss import RSSItem
|
|
232
|
+
item = RSSItem(title="News", url="https://example.com/news",
|
|
233
|
+
description="A test", published="2026-01-01", feed_name="test")
|
|
234
|
+
d = item.to_dict()
|
|
235
|
+
assert d["title"] == "News"
|
|
236
|
+
assert d["source"] == "rss"
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
# ── RSS parse invalid XML returns [] ─────────────────────────────────────────
|
|
240
|
+
|
|
241
|
+
def test_rss_parse_invalid_xml():
|
|
242
|
+
from trendscout.sources.rss import RSSFeedSource
|
|
243
|
+
rss = RSSFeedSource()
|
|
244
|
+
result = rss._parse("<not valid xml >>>", "test", 10)
|
|
245
|
+
assert result == []
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
# ── RSS parse items with missing fields ──────────────────────────────────────
|
|
249
|
+
|
|
250
|
+
def test_rss_parse_items_missing_fields():
|
|
251
|
+
from trendscout.sources.rss import RSSFeedSource
|
|
252
|
+
rss = RSSFeedSource()
|
|
253
|
+
xml = """<?xml version="1.0"?>
|
|
254
|
+
<rss version="2.0"><channel>
|
|
255
|
+
<item><title>No Link Article</title></item>
|
|
256
|
+
<item></item>
|
|
257
|
+
</channel></rss>"""
|
|
258
|
+
items = rss._parse(xml, "test-feed", 10)
|
|
259
|
+
# Only items with title should be returned
|
|
260
|
+
assert all(i.title for i in items)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
# ── RSS fetch_multiple ────────────────────────────────────────────────────────
|
|
264
|
+
|
|
265
|
+
def test_rss_fetch_multiple_network_error(monkeypatch):
|
|
266
|
+
from trendscout.sources.rss import RSSFeedSource
|
|
267
|
+
import requests
|
|
268
|
+
|
|
269
|
+
def raise_exc(*args, **kwargs):
|
|
270
|
+
raise requests.exceptions.ConnectionError("no network")
|
|
271
|
+
|
|
272
|
+
rss = RSSFeedSource()
|
|
273
|
+
monkeypatch.setattr(rss._session, "get", raise_exc)
|
|
274
|
+
result = rss.fetch_multiple(["http://feed1.example", "http://feed2.example"], limit=10)
|
|
275
|
+
assert result == []
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
# ── VelocityResult statuses ──────────────────────────────────────────────────
|
|
279
|
+
|
|
280
|
+
def test_velocity_peak_status():
|
|
281
|
+
from trendscout import TrendVelocity
|
|
282
|
+
tv = TrendVelocity()
|
|
283
|
+
# 10 stories with medium engagement → "peak"
|
|
284
|
+
stories = [{"score": 150, "num_comments": 30} for _ in range(10)]
|
|
285
|
+
result = tv.calculate("midtopic", stories)
|
|
286
|
+
assert result.status in ("emerging", "peak", "stable", "declining")
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def test_velocity_stable_status():
|
|
290
|
+
from trendscout import TrendVelocity
|
|
291
|
+
tv = TrendVelocity()
|
|
292
|
+
stories = [{"score": 30, "num_comments": 5} for _ in range(5)]
|
|
293
|
+
result = tv.calculate("slow topic", stories)
|
|
294
|
+
assert result.status in ("stable", "declining")
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
# ── TrendScout get_rss_trends (mock network) ─────────────────────────────────
|
|
298
|
+
|
|
299
|
+
def test_trendscout_get_rss_trends_empty(monkeypatch):
|
|
300
|
+
from trendscout import TrendScout
|
|
301
|
+
import requests
|
|
302
|
+
|
|
303
|
+
def raise_exc(*args, **kwargs):
|
|
304
|
+
raise requests.exceptions.ConnectionError("no network")
|
|
305
|
+
|
|
306
|
+
ts = TrendScout()
|
|
307
|
+
rss = ts._get_rss()
|
|
308
|
+
monkeypatch.setattr(rss._session, "get", raise_exc)
|
|
309
|
+
result = ts.get_rss_trends(category="tech", limit=5)
|
|
310
|
+
assert isinstance(result, pd.DataFrame)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
# ── TrendScout analyze_topics ────────────────────────────────────────────────
|
|
314
|
+
|
|
315
|
+
def test_trendscout_analyze_topics_mock(monkeypatch):
|
|
316
|
+
from trendscout import TrendScout
|
|
317
|
+
import requests
|
|
318
|
+
|
|
319
|
+
def raise_exc(*args, **kwargs):
|
|
320
|
+
raise requests.exceptions.ConnectionError("no network")
|
|
321
|
+
|
|
322
|
+
ts = TrendScout()
|
|
323
|
+
hn = ts._get_hn()
|
|
324
|
+
monkeypatch.setattr(hn._session, "get", raise_exc)
|
|
325
|
+
result = ts.analyze_topics(["python", "rust"], source="hackernews")
|
|
326
|
+
assert isinstance(result, pd.DataFrame)
|
|
327
|
+
assert "topic" in result.columns
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
# ── TrendScout get_rss_trends with custom feeds ──────────────────────────────
|
|
331
|
+
|
|
332
|
+
def test_trendscout_get_rss_trends_custom_feeds(monkeypatch):
|
|
333
|
+
from trendscout import TrendScout
|
|
334
|
+
import requests
|
|
335
|
+
|
|
336
|
+
def raise_exc(*args, **kwargs):
|
|
337
|
+
raise requests.exceptions.ConnectionError("no network")
|
|
338
|
+
|
|
339
|
+
ts = TrendScout()
|
|
340
|
+
rss = ts._get_rss()
|
|
341
|
+
monkeypatch.setattr(rss._session, "get", raise_exc)
|
|
342
|
+
result = ts.get_rss_trends(feeds=["http://example.com/feed.xml"], limit=5)
|
|
343
|
+
assert isinstance(result, pd.DataFrame)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
# ── TrendScout get_trend_velocity with rss source ────────────────────────────
|
|
347
|
+
|
|
348
|
+
def test_trendscout_velocity_rss_source(monkeypatch):
|
|
349
|
+
from trendscout import TrendScout
|
|
350
|
+
import requests
|
|
351
|
+
|
|
352
|
+
def raise_exc(*args, **kwargs):
|
|
353
|
+
raise requests.exceptions.ConnectionError("no network")
|
|
354
|
+
|
|
355
|
+
ts = TrendScout()
|
|
356
|
+
rss = ts._get_rss()
|
|
357
|
+
monkeypatch.setattr(rss._session, "get", raise_exc)
|
|
358
|
+
result = ts.get_trend_velocity("tech", source="rss")
|
|
359
|
+
assert hasattr(result, "score")
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
# ── HackerNews get_top_stories network error ─────────────────────────────────
|
|
363
|
+
|
|
364
|
+
def test_hackernews_top_stories_network_error(monkeypatch):
|
|
365
|
+
from trendscout.sources.hackernews import HackerNewsSource
|
|
366
|
+
import requests
|
|
367
|
+
|
|
368
|
+
def raise_exc(*args, **kwargs):
|
|
369
|
+
raise requests.exceptions.ConnectionError("no network")
|
|
370
|
+
|
|
371
|
+
hn = HackerNewsSource(timeout=1)
|
|
372
|
+
monkeypatch.setattr(hn._session, "get", raise_exc)
|
|
373
|
+
result = hn.get_top_stories(limit=5)
|
|
374
|
+
assert result == []
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
# ── HackerNews search returns items when mocked ───────────────────────────────
|
|
378
|
+
|
|
379
|
+
def test_hackernews_search_mocked_response(monkeypatch):
|
|
380
|
+
from trendscout.sources.hackernews import HackerNewsSource
|
|
381
|
+
import json
|
|
382
|
+
|
|
383
|
+
mock_response = {
|
|
384
|
+
"hits": [
|
|
385
|
+
{"objectID": "123", "title": "Python AI Tools", "url": "https://example.com/1",
|
|
386
|
+
"points": 300, "num_comments": 50, "author": "user1", "created_at": "2026-01-01T00:00:00Z"},
|
|
387
|
+
{"objectID": "456", "title": "Rust Performance", "url": "https://example.com/2",
|
|
388
|
+
"points": 200, "num_comments": 30, "author": "user2", "created_at": "2026-01-02T00:00:00Z"},
|
|
389
|
+
]
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
class MockResp:
|
|
393
|
+
def raise_for_status(self): pass
|
|
394
|
+
def json(self): return mock_response
|
|
395
|
+
|
|
396
|
+
hn = HackerNewsSource()
|
|
397
|
+
monkeypatch.setattr(hn._session, "get", lambda *a, **kw: MockResp())
|
|
398
|
+
stories = hn.search("python", limit=5)
|
|
399
|
+
assert len(stories) == 2
|
|
400
|
+
assert stories[0].score >= stories[1].score # sorted by score
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
# ── SentimentAnalyzer neutral text ───────────────────────────────────────────
|
|
404
|
+
|
|
405
|
+
def test_sentiment_neutral():
|
|
406
|
+
from trendscout import SentimentAnalyzer
|
|
407
|
+
analyzer = SentimentAnalyzer()
|
|
408
|
+
result = analyzer.analyze("The meeting is scheduled for Tuesday.")
|
|
409
|
+
assert result["label"] in ("positive", "negative", "neutral")
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
# ── HackerNews get_top_stories mocked ────────────────────────────────────────
|
|
413
|
+
|
|
414
|
+
def test_hackernews_top_stories_mocked(monkeypatch):
|
|
415
|
+
from trendscout.sources.hackernews import HackerNewsSource
|
|
416
|
+
import json, unittest.mock as mock
|
|
417
|
+
|
|
418
|
+
call_count = [0]
|
|
419
|
+
|
|
420
|
+
top_ids = [111, 222]
|
|
421
|
+
items_by_id = {
|
|
422
|
+
111: {"type": "story", "title": "Story One", "url": "https://ex.com/1",
|
|
423
|
+
"score": 100, "descendants": 20, "by": "alice", "time": 1716000000},
|
|
424
|
+
222: {"type": "story", "title": "Story Two", "url": None,
|
|
425
|
+
"score": 80, "descendants": 10, "by": "bob", "time": 1716000001},
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
def mock_get(url, *args, **kwargs):
|
|
429
|
+
resp = mock.MagicMock()
|
|
430
|
+
resp.raise_for_status = lambda: None
|
|
431
|
+
if "topstories" in url:
|
|
432
|
+
resp.json.return_value = top_ids
|
|
433
|
+
else:
|
|
434
|
+
sid = int(url.split("/item/")[1].split(".json")[0])
|
|
435
|
+
resp.json.return_value = items_by_id.get(sid, {})
|
|
436
|
+
return resp
|
|
437
|
+
|
|
438
|
+
hn = HackerNewsSource()
|
|
439
|
+
monkeypatch.setattr(hn._session, "get", mock_get)
|
|
440
|
+
monkeypatch.setattr("trendscout.sources.hackernews.time.sleep", lambda x: None)
|
|
441
|
+
stories = hn.get_top_stories(limit=5)
|
|
442
|
+
assert len(stories) >= 1
|
|
443
|
+
assert stories[0].title in ("Story One", "Story Two")
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
# ── TrendScout get_emerging_trends exception fallback ─────────────────────────
|
|
447
|
+
|
|
448
|
+
def test_trendscout_emerging_trends_exception_fallback(monkeypatch):
|
|
449
|
+
from trendscout import TrendScout
|
|
450
|
+
|
|
451
|
+
ts = TrendScout()
|
|
452
|
+
|
|
453
|
+
def raise_exc(topic, limit):
|
|
454
|
+
raise RuntimeError("simulated error")
|
|
455
|
+
|
|
456
|
+
monkeypatch.setattr(ts, "get_hackernews_trends", raise_exc)
|
|
457
|
+
result = ts.get_emerging_trends(domain="ai", limit=3)
|
|
458
|
+
assert isinstance(result, pd.DataFrame)
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
# ── GitHub Trending ───────────────────────────────────────────────────────────
|
|
462
|
+
|
|
463
|
+
def test_github_trending_source_import():
|
|
464
|
+
from trendscout.sources.github_trending import GitHubTrendingSource, GitHubRepo
|
|
465
|
+
src = GitHubTrendingSource()
|
|
466
|
+
assert src is not None
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
def test_github_trending_empty_on_network_failure(monkeypatch):
|
|
470
|
+
"""fetch() returns empty list when network is unavailable."""
|
|
471
|
+
from trendscout.sources.github_trending import GitHubTrendingSource
|
|
472
|
+
import requests
|
|
473
|
+
|
|
474
|
+
def _fail(*a, **kw):
|
|
475
|
+
raise requests.exceptions.ConnectionError("offline")
|
|
476
|
+
|
|
477
|
+
monkeypatch.setattr(requests, "get", _fail)
|
|
478
|
+
src = GitHubTrendingSource()
|
|
479
|
+
repos = src.fetch(language="python", since="daily", limit=5)
|
|
480
|
+
assert repos == []
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
def test_trendscout_get_github_trending_returns_dataframe(monkeypatch):
|
|
484
|
+
"""get_github_trending returns a DataFrame (may be empty if offline)."""
|
|
485
|
+
import pandas as pd
|
|
486
|
+
from trendscout import TrendScout
|
|
487
|
+
import requests
|
|
488
|
+
|
|
489
|
+
def _fail(*a, **kw):
|
|
490
|
+
raise requests.exceptions.ConnectionError("offline")
|
|
491
|
+
|
|
492
|
+
monkeypatch.setattr(requests, "get", _fail)
|
|
493
|
+
ts = TrendScout()
|
|
494
|
+
df = ts.get_github_trending(language="python", since="daily", limit=5)
|
|
495
|
+
assert isinstance(df, pd.DataFrame)
|
|
496
|
+
assert set(["name", "url", "stars", "source"]).issubset(set(df.columns))
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""Tests for TrendScout v1.2.0 — Pydantic schemas + MCP tools."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TestPydanticSchemas:
|
|
7
|
+
def test_sentiment_schema(self):
|
|
8
|
+
from trendscout import TrendScout
|
|
9
|
+
from trendscout.schemas import SentimentSchema
|
|
10
|
+
ts = TrendScout()
|
|
11
|
+
result = ts.analyze_sentiment("AI stocks surge to record highs", domain="finance")
|
|
12
|
+
schema = SentimentSchema.from_result(result)
|
|
13
|
+
assert schema.polarity != 0 or schema.label in ("positive", "negative", "neutral")
|
|
14
|
+
assert schema.market_signal in ("bullish", "bearish", "neutral")
|
|
15
|
+
assert 0.0 <= schema.confidence <= 1.0
|
|
16
|
+
d = schema.model_dump()
|
|
17
|
+
assert "polarity" in d and "label" in d
|
|
18
|
+
|
|
19
|
+
def test_velocity_schema(self):
|
|
20
|
+
from trendscout import TrendScout
|
|
21
|
+
from trendscout.schemas import VelocitySchema
|
|
22
|
+
ts = TrendScout()
|
|
23
|
+
result = ts.get_trend_velocity("Python", source="hackernews")
|
|
24
|
+
schema = VelocitySchema.from_result(result)
|
|
25
|
+
assert schema.keyword == "Python"
|
|
26
|
+
assert 0.0 <= schema.score <= 1.0
|
|
27
|
+
assert schema.status in ("emerging", "peak", "stable", "declining", "unknown")
|
|
28
|
+
assert schema.summary
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TestMCPTools:
|
|
32
|
+
def test_analyze_sentiment_tool(self):
|
|
33
|
+
from trendscout.mcp_server import analyze_sentiment
|
|
34
|
+
result = analyze_sentiment("Tech stocks are crashing hard today", domain="finance")
|
|
35
|
+
assert "polarity" in result
|
|
36
|
+
assert result["label"] in ("positive", "negative", "neutral")
|
|
37
|
+
assert "market_signal" in result
|
|
38
|
+
|
|
39
|
+
def test_get_trend_velocity_tool(self):
|
|
40
|
+
from trendscout.mcp_server import get_trend_velocity
|
|
41
|
+
result = get_trend_velocity("machine learning")
|
|
42
|
+
assert "score" in result
|
|
43
|
+
assert "status" in result
|
|
44
|
+
assert result["keyword"] == "machine learning"
|
|
45
|
+
|
|
46
|
+
def test_analyze_topics_tool(self):
|
|
47
|
+
from trendscout.mcp_server import analyze_topics
|
|
48
|
+
texts = [
|
|
49
|
+
"Python is great for data science",
|
|
50
|
+
"Machine learning with PyTorch",
|
|
51
|
+
"Deep learning transformers",
|
|
52
|
+
"Stock market crashes today",
|
|
53
|
+
"Federal reserve raises rates",
|
|
54
|
+
]
|
|
55
|
+
result = analyze_topics(texts)
|
|
56
|
+
assert "topics" in result or "error" in result # graceful if no nlp model
|
|
57
|
+
|
|
58
|
+
def test_mcp_tools_registered(self):
|
|
59
|
+
from trendscout.mcp_server import mcp
|
|
60
|
+
tool_names = [t.name for t in mcp._tool_manager.list_tools()]
|
|
61
|
+
for expected in ["analyze_sentiment", "get_trend_velocity",
|
|
62
|
+
"get_hackernews_trends", "get_github_trending",
|
|
63
|
+
"get_emerging_trends", "analyze_topics"]:
|
|
64
|
+
assert expected in tool_names, f"Missing tool: {expected}"
|