primer-app / tests /test_transcript.py
Viney's picture
deploy: Primer app for HF Spaces — clean orphan history
35676b4
from unittest.mock import patch, MagicMock
from ingestion.transcript import fetch_transcript, _parse_transcript
def _mock_response(data: dict) -> MagicMock:
m = MagicMock()
m.json.return_value = data
m.raise_for_status = MagicMock()
return m
@patch("ingestion.transcript._next_key", return_value="test-key")
@patch("ingestion.transcript.httpx.get")
def test_fetch_transcript_list_format(mock_get, mock_key):
mock_get.return_value = _mock_response({
"symbol": "AAPL",
"quarter": "2024Q4",
"transcript": [
{"speaker": "Tim Cook", "title": "CEO", "content": "We had a record quarter."},
{"speaker": "Luca Maestri", "title": "CFO", "content": "Gross margin was 46.2%."},
]
})
text = fetch_transcript("AAPL", "2024Q4")
assert "Tim Cook" in text
assert "record quarter" in text
assert "Gross margin" in text
@patch("ingestion.transcript._next_key", return_value="test-key")
@patch("ingestion.transcript.httpx.get")
def test_fetch_transcript_string_format(mock_get, mock_key):
mock_get.return_value = _mock_response({
"symbol": "IBM",
"quarter": "2024Q1",
"transcript": "Operator: Good afternoon. CEO: Revenue grew 5%."
})
text = fetch_transcript("IBM", "2024Q1")
assert "Revenue grew 5%" in text
@patch("ingestion.transcript._next_key", return_value="test-key")
@patch("ingestion.transcript.httpx.get")
def test_fetch_transcript_rate_limited_returns_empty(mock_get, mock_key):
mock_get.return_value = _mock_response({
"Information": "Thank you for using Alpha Vantage! Our standard API rate limit is 25 requests per day."
})
assert fetch_transcript("AAPL", "2024Q4") == ""
@patch("ingestion.transcript._next_key", return_value="test-key")
@patch("ingestion.transcript.httpx.get")
def test_fetch_transcript_http_error_returns_empty(mock_get, mock_key):
mock_get.side_effect = Exception("connection error")
assert fetch_transcript("AAPL", "2024Q4") == ""
def test_parse_transcript_list():
data = {
"transcript": [
{"speaker": "CEO", "content": "Hello world"},
{"speaker": "CFO", "content": "Numbers here"},
]
}
result = _parse_transcript(data)
assert "CEO: Hello world" in result
assert "CFO: Numbers here" in result
def test_parse_transcript_string():
data = {"transcript": "Plain text transcript"}
assert _parse_transcript(data) == "Plain text transcript"
def test_parse_transcript_missing_key():
assert _parse_transcript({}) == ""
def test_parse_transcript_null_value():
assert _parse_transcript({"transcript": None}) == ""
@patch("ingestion.transcript._next_key", return_value="test-key")
@patch("ingestion.transcript.httpx.get")
def test_fetch_transcript_note_key_returns_empty(mock_get, mock_key):
mock_get.return_value = _mock_response({
"Note": "Thank you for using Alpha Vantage! This is the standard API note."
})
assert fetch_transcript("AAPL", "2024Q4") == ""