File size: 3,044 Bytes
35676b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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") == ""