-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathtest_news.py
More file actions
452 lines (392 loc) · 15.5 KB
/
Copy pathtest_news.py
File metadata and controls
452 lines (392 loc) · 15.5 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"""Tests for preprocess.news — PR/wire candidate normalisation."""
import hashlib
import unittest
from datetime import datetime, timedelta, timezone
from pathlib import Path
import httpx
import respx
from quantmind.preprocess.fetch.rss import FeedItem
from quantmind.preprocess.news import (
RawNewsDocument,
build_news_identity,
build_sec_news_identity,
canonicalize_source_url,
extract_exchange_ticker_hints,
feed_item_to_news_document,
fetch_news_document,
news_content_hash,
normalize_news_text,
preprocess_feed_item,
preprocess_news_document,
)
from quantmind.preprocess.time import parse_news_datetime
_FIXTURES = Path(__file__).parent / "fixtures" / "pr_newswire"
class NewsTimeTests(unittest.TestCase):
def test_parse_rfc822_news_datetime(self):
result = parse_news_datetime("Mon, 15 Apr 2024 10:30:00 GMT")
self.assertEqual(result.tzinfo, timezone.utc)
self.assertEqual(result.year, 2024)
self.assertEqual(result.hour, 10)
def test_parse_news_datetime_with_offset(self):
result = parse_news_datetime("Mon, 15 Apr 2024 10:30:00 -0400")
self.assertEqual(result.tzinfo, timezone.utc)
self.assertEqual(result.hour, 14)
class NewsPreprocessTests(unittest.TestCase):
def test_normalize_news_text_and_hash(self):
text = (
"NVIDIA\u2014reported revenue\n\n\nNVIDIA\u2014reported revenue"
)
normalized = normalize_news_text(text)
self.assertEqual(
normalized,
"NVIDIA-reported revenue\n\nNVIDIA-reported revenue",
)
self.assertEqual(
news_content_hash(normalized),
hashlib.sha256(normalized.encode("utf-8")).hexdigest(),
)
def test_normalize_news_text_stabilizes_email_protection_links(self):
text_a = (
"Contact [[email protected]](/cdn-cgi/l/email-protection#abc123)"
)
text_b = (
"Contact [[email protected]](/cdn-cgi/l/email-protection#def456)"
)
self.assertEqual(
normalize_news_text(text_a), normalize_news_text(text_b)
)
def test_canonicalize_source_url_drops_tracking(self):
result = canonicalize_source_url(
"HTTPS://Example.COM/path/?b=2&utm_source=x&a=1#frag"
)
self.assertEqual(result, "https://example.com/path?a=1&b=2")
def test_exchange_ticker_hints_are_deduped(self):
hints = extract_exchange_ticker_hints(
"NVIDIA Corporation (NASDAQ: NVDA) and IBM NYSE: IBM. "
"Again (NASDAQ: NVDA)."
)
self.assertEqual([h.symbol for h in hints], ["NVDA", "IBM"])
self.assertEqual(hints[0].exchange, "NASDAQ")
self.assertEqual(hints[1].exchange, "NYSE")
def test_exchange_ticker_hints_ignore_markdown_decoration(self):
cases = (
(
"link",
"Carnival Corporation & plc "
"(NYSE: [CCL](#financial-modal)) today announced...",
("CCL", "NYSE", "(NYSE: CCL)"),
),
(
"bold emphasis",
"Example Corporation (NASDAQ: **ABC**) today announced...",
("ABC", "NASDAQ", "(NASDAQ: ABC)"),
),
(
"italic emphasis",
"Example Corporation (NASDAQ: *ABC*) today announced...",
("ABC", "NASDAQ", "(NASDAQ: ABC)"),
),
)
for name, text, expected in cases:
with self.subTest(name=name):
hints = extract_exchange_ticker_hints(text)
self.assertEqual(len(hints), 1)
self.assertEqual(
(hints[0].symbol, hints[0].exchange, hints[0].raw),
expected,
)
def test_exchange_ticker_hints_capture_shared_prefix_comma_list(self):
hints = extract_exchange_ticker_hints(
"Example issuer (NYSE: EVEX, EVEXW) announced results."
)
self.assertEqual(
tuple((hint.symbol, hint.exchange, hint.raw) for hint in hints),
(
("EVEX", "NYSE", "(NYSE: EVEX, EVEXW)"),
("EVEXW", "NYSE", "(NYSE: EVEX, EVEXW)"),
),
)
def test_exchange_ticker_hints_stop_shared_list_at_semicolon(self):
hints = extract_exchange_ticker_hints(
"Example issuer (NYSE: EVEX, EVEXW; B3: EVEB31) announced results."
)
self.assertEqual(
tuple((hint.symbol, hint.exchange, hint.raw) for hint in hints),
(
(
"EVEX",
"NYSE",
"(NYSE: EVEX, EVEXW; B3: EVEB31)",
),
(
"EVEXW",
"NYSE",
"(NYSE: EVEX, EVEXW; B3: EVEB31)",
),
),
)
def test_exchange_ticker_hints_capture_new_shared_list_members(self):
hints = extract_exchange_ticker_hints(
"Prior (NYSE: EVEX). Offering (NYSE: EVEX, EVEXW)."
)
self.assertEqual(
tuple((hint.symbol, hint.exchange, hint.raw) for hint in hints),
(
("EVEX", "NYSE", "(NYSE: EVEX)"),
("EVEXW", "NYSE", "(NYSE: EVEX, EVEXW)"),
),
)
def test_exchange_ticker_hints_stop_at_shared_prefix_boundaries(self):
cases = (
(
"balanced mention followed by prose",
"Shares of (NYSE: IBM), and (NASDAQ: AAPL) rose today.",
(
("IBM", "NYSE", "(NYSE: IBM)"),
("AAPL", "NASDAQ", "(NASDAQ: AAPL)"),
),
),
("unsupported exchange", "(OTCID: QVCAQ, QVCGQ, QVCPQ)", ()),
(
"conjunction boundary",
"(NYSE: TME and HKEX: 1698)",
(("TME", "NYSE", "(NYSE: TME"),),
),
(
"semicolon boundary TSXV",
"(NASDAQ: VMAR; TSXV: VMAR)",
(("VMAR", "NASDAQ", "(NASDAQ: VMAR"),),
),
(
"semicolon boundary BMV",
"(NYSE: ASR; BMV: ASUR)",
(("ASR", "NYSE", "(NYSE: ASR"),),
),
(
"ordinary prose inside parentheses",
"(NYSE: IBM, and revenue increased)",
(("IBM", "NYSE", "(NYSE: IBM"),),
),
(
"uppercase token before ordinary prose",
"(NYSE: IBM, ABC and revenue increased)",
(("IBM", "NYSE", "(NYSE: IBM"),),
),
(
"uppercase token before non-exchange semicolon",
"(NYSE: IBM, ABC; revenue increased)",
(("IBM", "NYSE", "(NYSE: IBM"),),
),
)
for name, text, expected in cases:
with self.subTest(name=name):
hints = extract_exchange_ticker_hints(text)
self.assertEqual(
tuple(
(hint.symbol, hint.exchange, hint.raw) for hint in hints
),
expected,
)
def test_build_sec_news_identity(self):
self.assertEqual(
build_sec_news_identity(
accession_number="0001045810-26-000123",
section_key="EX99.1",
),
"sec:0001045810-26-000123:ex99.1",
)
def test_build_news_identity_requires_source_reference(self):
with self.assertRaises(ValueError):
build_news_identity(source_type="press_release")
def test_preprocess_news_document_builds_candidate_contract(self):
published = datetime(
2024, 4, 15, 10, 30, tzinfo=timezone(timedelta(hours=-4))
)
raw = RawNewsDocument(
body_text=(
"NVIDIA Corporation (NASDAQ: NVDA) today reported "
"record quarterly revenue."
),
source_url="https://example.com/pr/nvidia-results?utm_source=feed",
title="NVIDIA Announces Results",
publisher="PR Newswire",
published_at=published,
payload_id="gnw-123",
)
candidate = preprocess_news_document(raw)
self.assertEqual(candidate.source_type, "press_release")
self.assertTrue(candidate.identity.startswith("wire:"))
self.assertEqual(
candidate.source_url, "https://example.com/pr/nvidia-results"
)
self.assertEqual(candidate.published_at.tzinfo, timezone.utc)
self.assertEqual(candidate.published_at.hour, 14)
self.assertEqual(candidate.ticker_hints[0].symbol, "NVDA")
self.assertEqual(
candidate.content_hash,
hashlib.sha256(candidate.body_text.encode("utf-8")).hexdigest(),
)
def test_preprocess_news_document_rejects_empty_body(self):
raw = RawNewsDocument(
body_text=" ",
source_url="https://example.com/pr/empty",
)
with self.assertRaises(ValueError):
preprocess_news_document(raw)
def test_wire_markup_fixture_meets_ticker_recall_control(self):
body_text = (_FIXTURES / "ticker_markup.md").read_text(encoding="utf-8")
raw = RawNewsDocument(
body_text=body_text,
source_url="https://www.prnewswire.com/news-releases/example.html",
)
candidate = preprocess_news_document(raw)
expected_hints = {
("ABC", "NASDAQ"),
("CCL", "NYSE"),
("PODD", "NASDAQ"),
}
actual_hints = {
(hint.symbol, hint.exchange) for hint in candidate.ticker_hints
}
recall = len(expected_hints & actual_hints) / len(expected_hints)
self.assertEqual(recall, 1.0)
self.assertIn("[CCL](#financial-modal)", candidate.body_text)
self.assertIn(
"(NASDAQ:\n\n[PODD](#financial-modal))",
candidate.body_text,
)
self.assertIn("**ABC**", candidate.body_text)
self.assertEqual(
candidate.content_hash,
news_content_hash(candidate.body_text),
)
class NewsFeedItemTests(unittest.IsolatedAsyncioTestCase):
async def test_feed_item_to_news_document_uses_inline_content(self):
item = FeedItem(
title="NVIDIA Announces Results",
url="https://example.com/pr/nvidia-results",
id="gnw-123",
published_at=datetime(2024, 4, 15, tzinfo=timezone.utc),
content_html=(
"<html><body><article><p>NVIDIA Corporation "
"(NASDAQ: NVDA) reported results.</p></article></body></html>"
),
source_feed_url="https://example.com/rss",
)
raw = await feed_item_to_news_document(item, publisher="PR Newswire")
self.assertEqual(raw.payload_id, "gnw-123")
self.assertEqual(raw.publisher, "PR Newswire")
self.assertIn("NVIDIA", raw.body_text)
self.assertEqual(
raw.metadata["source_feed_url"], "https://example.com/rss"
)
self.assertEqual(raw.metadata["body_source"], "feed")
async def test_article_source_fetches_despite_nonempty_teaser(self):
item = FeedItem(
title="NVIDIA Announces Results",
url="https://example.com/pr/nvidia-results",
id="wire-123",
summary_html="<p>This non-empty teaser is not the full body.</p>",
source_feed_url="https://example.com/rss",
)
html = """
<html><body><article>
<h1>NVIDIA Announces Results</h1>
<p>The complete release contains full financial details.</p>
</article></body></html>
"""
with respx.mock(assert_all_called=True) as router:
router.get(item.url).mock(
return_value=httpx.Response(
200,
headers={"Content-Type": "text/html"},
text=html,
)
)
raw = await feed_item_to_news_document(
item,
body_source="article",
)
self.assertIn("complete release", raw.body_text)
self.assertNotIn("non-empty teaser", raw.body_text)
self.assertEqual(raw.metadata["body_source"], "article")
async def test_feed_source_does_not_fetch_item_url(self):
item = FeedItem(
title="NVIDIA Announces Results",
url="https://example.com/pr/nvidia-results",
summary_html="<p>Feed body stays local.</p>",
)
with respx.mock(assert_all_called=True):
raw = await feed_item_to_news_document(
item,
body_source="feed",
)
self.assertIn("Feed body", raw.body_text)
async def test_article_source_requires_item_url(self):
item = FeedItem(title="Missing URL", summary_html="A teaser")
with self.assertRaisesRegex(ValueError, "requires an item URL"):
await feed_item_to_news_document(
item,
body_source="article",
)
async def test_article_extraction_failure_is_explicit(self):
item = FeedItem(
title="Unsupported body",
url="https://example.com/release.pdf",
summary_html="A teaser",
)
with respx.mock(assert_all_called=True) as router:
router.get(item.url).mock(
return_value=httpx.Response(
200,
headers={"Content-Type": "application/pdf"},
content=b"not html",
)
)
with self.assertRaisesRegex(ValueError, "Unsupported"):
await feed_item_to_news_document(
item,
body_source="article",
)
async def test_preprocess_feed_item_returns_candidate(self):
item = FeedItem(
title="NVIDIA Announces Results",
url="https://example.com/pr/nvidia-results",
id="gnw-123",
summary_html="<p>NVIDIA Corporation (NASDAQ: NVDA) reported results.</p>",
)
candidate = await preprocess_feed_item(item, publisher="PR Newswire")
self.assertEqual(candidate.publisher, "PR Newswire")
self.assertEqual(candidate.ticker_hints[0].symbol, "NVDA")
class FetchNewsDocumentTests(unittest.IsolatedAsyncioTestCase):
async def test_fetch_news_document_extracts_html(self):
html = """
<html>
<body>
<article>
<h1>NVIDIA Announces Results</h1>
<p>NVIDIA Corporation (NASDAQ: NVDA) reported record revenue.</p>
</article>
</body>
</html>
"""
with respx.mock(assert_all_called=True) as router:
router.get("https://example.com/pr/nvidia-results").mock(
return_value=httpx.Response(
200,
headers={"Content-Type": "text/html; charset=utf-8"},
content=html.encode(),
)
)
raw = await fetch_news_document(
"https://example.com/pr/nvidia-results",
title="NVIDIA Announces Results",
payload_id="gnw-123",
)
self.assertEqual(
raw.source_url, "https://example.com/pr/nvidia-results"
)
self.assertEqual(raw.metadata["content_type"], "text/html")
self.assertIn("record revenue", raw.body_text)
if __name__ == "__main__":
unittest.main()