financial-model-builder-v2 / tests /test_transcript_fetcher.py
bdschi1's picture
deploy
48cf766
Raw History Blame Contribute Delete
8.21 kB
"""Tests for transcript_fetcher module."""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from unittest.mock import patch, MagicMock
from transcript_fetcher import (
TranscriptFetcher,
_strip_html,
_filing_date_to_quarter,
_extract_quarter_from_text,
_is_earnings_content,
_HTMLTextExtractor,
)
# ── HTML stripping ──────────────────────────────────────────────
def test_strip_html_basic():
html = "<html><body><p>Hello <b>World</b></p></body></html>"
text = _strip_html(html)
assert "Hello" in text
assert "World" in text
assert "<" not in text
def test_strip_html_removes_scripts():
html = "<p>Before</p><script>alert('x')</script><p>After</p>"
text = _strip_html(html)
assert "Before" in text
assert "After" in text
assert "alert" not in text
def test_strip_html_empty():
assert _strip_html("") == ""
def test_strip_html_plain_text():
assert _strip_html("no tags here") == "no tags here"
# ── Quarter estimation ──────────────────────────────────────────
def test_filing_date_to_quarter_q4():
assert _filing_date_to_quarter("2024-01-25") == "Q4 2023"
assert _filing_date_to_quarter("2024-02-15") == "Q4 2023"
def test_filing_date_to_quarter_q1():
assert _filing_date_to_quarter("2024-04-20") == "Q1 2024"
assert _filing_date_to_quarter("2024-05-10") == "Q1 2024"
def test_filing_date_to_quarter_q2():
assert _filing_date_to_quarter("2024-07-15") == "Q2 2024"
assert _filing_date_to_quarter("2024-08-01") == "Q2 2024"
def test_filing_date_to_quarter_q3():
assert _filing_date_to_quarter("2024-10-20") == "Q3 2024"
assert _filing_date_to_quarter("2024-11-05") == "Q3 2024"
def test_filing_date_to_quarter_bad_input():
assert _filing_date_to_quarter("not-a-date") == "unknown"
assert _filing_date_to_quarter("") == "unknown"
assert _filing_date_to_quarter(None) == "unknown"
# ── Quarter extraction from text ────────────────────────────────
def test_extract_quarter_explicit_q():
text = "Results for Q3 2024 were strong."
assert _extract_quarter_from_text(text) == "Q3 2024"
def test_extract_quarter_spelled_out():
text = "Third quarter of 2024 results."
assert _extract_quarter_from_text(text) == "Q3 2024"
def test_extract_quarter_no_match():
assert _extract_quarter_from_text("no quarter here") == "unknown"
# ── Earnings content detection ──────────────────────────────────
def test_is_earnings_content_positive():
text = "Earnings Release: Results of Operations for the quarter. Financial results..."
assert _is_earnings_content(text) is True
def test_is_earnings_content_negative():
text = "This is a change in directors filing. Board appointment."
assert _is_earnings_content(text) is False
# ── TranscriptFetcher class ────────────────────────────────────
def test_init_defaults():
tf = TranscriptFetcher("AAPL")
assert tf.ticker == "AAPL"
assert tf._submissions is None
def test_init_lowercase_ticker():
tf = TranscriptFetcher("aapl")
assert tf.ticker == "AAPL"
def test_init_fmp_key_from_param():
tf = TranscriptFetcher("AAPL", fmp_api_key="test123")
assert tf.fmp_api_key == "test123"
def test_init_fmp_key_from_env():
with patch.dict(os.environ, {"FMP_API_KEY": "envkey"}):
tf = TranscriptFetcher("AAPL")
assert tf.fmp_api_key == "envkey"
def test_fetch_latest_returns_none_when_empty():
tf = TranscriptFetcher("AAPL")
with patch.object(tf, 'fetch_transcripts', return_value=[]):
result = tf.fetch_latest_transcript()
assert result is None
def test_fetch_latest_returns_first():
mock_transcript = {"date": "2024-10-25", "quarter": "Q3 2024",
"text": "call text", "source": "fmp"}
tf = TranscriptFetcher("AAPL")
with patch.object(tf, 'fetch_transcripts', return_value=[mock_transcript]):
result = tf.fetch_latest_transcript()
assert result == mock_transcript
def test_fetch_transcripts_tries_fmp_first():
"""With FMP key, should try FMP before SEC."""
tf = TranscriptFetcher("AAPL", fmp_api_key="testkey")
fmp_result = [{"date": "2024-10-25", "quarter": "Q3 2024",
"text": "fmp text", "source": "fmp"}]
with patch.object(tf, '_fetch_fmp_transcripts', return_value=fmp_result) as fmp_mock:
with patch.object(tf, '_fetch_sec_8k_transcripts') as sec_mock:
result = tf.fetch_transcripts(n=1)
fmp_mock.assert_called_once_with(1)
sec_mock.assert_not_called()
assert result[0]["source"] == "fmp"
def test_fetch_transcripts_falls_back_to_sec():
"""When FMP fails, should fall back to SEC."""
tf = TranscriptFetcher("AAPL", fmp_api_key="testkey")
sec_result = [{"date": "2024-10-25", "quarter": "Q3 2024",
"text": "sec text", "source": "sec_8k"}]
with patch.object(tf, '_fetch_fmp_transcripts', return_value=[]):
with patch.object(tf, '_fetch_sec_8k_transcripts', return_value=sec_result):
result = tf.fetch_transcripts(n=1)
assert result[0]["source"] == "sec_8k"
def test_fetch_transcripts_no_key_skips_fmp():
"""Without FMP key, should go straight to SEC."""
with patch.dict(os.environ, {}, clear=False):
# Ensure no FMP_API_KEY in env
os.environ.pop("FMP_API_KEY", None)
tf = TranscriptFetcher("AAPL")
tf.fmp_api_key = None
sec_result = [{"date": "2024-10-25", "quarter": "Q3 2024",
"text": "sec text", "source": "sec_8k"}]
with patch.object(tf, '_fetch_fmp_transcripts') as fmp_mock:
with patch.object(tf, '_fetch_sec_8k_transcripts', return_value=sec_result):
result = tf.fetch_transcripts(n=1)
fmp_mock.assert_not_called()
# ── Transcript dict format ──────────────────────────────────────
def test_transcript_dict_format():
"""Verify expected keys in transcript dict."""
expected_keys = {"date", "quarter", "text", "source"}
mock_t = {"date": "2024-10-25", "quarter": "Q3 2024",
"text": "content", "source": "fmp"}
assert set(mock_t.keys()) == expected_keys
# ── SEC 8-K parsing helpers ─────────────────────────────────────
def test_sec_8k_extraction_with_mock_submissions():
"""Test _fetch_sec_8k_transcripts with mocked submissions data."""
tf = TranscriptFetcher("AAPL")
tf._cik = "0000320193"
tf._submissions = {
"filings": {
"recent": {
"form": ["8-K", "10-Q", "8-K"],
"filingDate": ["2024-10-25", "2024-10-28", "2024-07-25"],
"accessionNumber": ["0000320193-24-000100", "0000320193-24-000099",
"0000320193-24-000050"],
"primaryDocument": ["filing.htm", "tenq.htm", "filing2.htm"],
"items": ["2.02", "", "7.01"],
}
}
}
earnings_html = """
<html><body>
<h1>Earnings Release</h1>
<p>Results of Operations for Q3 2024. Financial results exceeded expectations.
Press release issued. Conference call transcript follows. Quarterly results
showed strong earnings release performance.</p>
</body></html>
"""
with patch('transcript_fetcher._fetch_8k_html', return_value=earnings_html):
results = tf._fetch_sec_8k_transcripts(n=2)
assert len(results) >= 1
assert results[0]["source"] == "sec_8k"
assert "date" in results[0]
assert "quarter" in results[0]
assert "text" in results[0]