|
2 | 2 |
|
3 | 3 | import pandas as pd |
4 | 4 | import pytest |
5 | | -from unittest.mock import patch, MagicMock |
6 | 5 | from openbb_yfinance.utils.helpers import ( |
7 | 6 | df_transform_numbers, |
8 | 7 | get_futures_data, |
9 | | - get_custom_screener, |
10 | | - get_defined_screener, |
11 | | - get_futures_symbols, |
12 | | - yf_download, |
13 | 8 | ) |
14 | 9 |
|
15 | 10 | # pylint: disable=redefined-outer-name, unused-argument |
@@ -38,117 +33,3 @@ def test_df_transform_numbers(): |
38 | 33 | transformed = df_transform_numbers(data, ["Value", "% Change"]) |
39 | 34 | assert transformed["Value"].equals(pd.Series([1e6, 2.5e9, 3e12])) |
40 | 35 | assert transformed["% Change"].equals(pd.Series([1 / 100, -2 / 100, 3.5 / 100])) |
41 | | - |
42 | | - |
43 | | -@pytest.mark.asyncio |
44 | | -async def test_get_custom_screener_no_session(): |
45 | | - """Test that get_custom_screener does not pass session to YfData.""" |
46 | | - with patch("yfinance.data.YfData") as mock_yfdata: |
47 | | - mock_instance = MagicMock() |
48 | | - mock_response = MagicMock() |
49 | | - mock_response.json.return_value = { |
50 | | - "finance": { |
51 | | - "result": [ |
52 | | - { |
53 | | - "quotes": [ |
54 | | - { |
55 | | - "symbol": "AAPL", |
56 | | - "exchangeTimezoneName": "America/New_York", |
57 | | - "regularMarketTime": 1700000000, |
58 | | - } |
59 | | - ], |
60 | | - "total": 1, |
61 | | - } |
62 | | - ] |
63 | | - } |
64 | | - } |
65 | | - mock_response.raise_for_status = MagicMock() |
66 | | - mock_instance.post.return_value = mock_response |
67 | | - mock_yfdata.return_value = mock_instance |
68 | | - |
69 | | - await get_custom_screener({"query": {}}, limit=1) |
70 | | - |
71 | | - # Verify YfData was called without session parameter |
72 | | - mock_yfdata.assert_called_once() |
73 | | - call_kwargs = mock_yfdata.call_args[1] if mock_yfdata.call_args[1] else {} |
74 | | - assert ( |
75 | | - "session" not in call_kwargs |
76 | | - ), "YfData should not be called with session parameter" |
77 | | - |
78 | | - |
79 | | -@pytest.mark.asyncio |
80 | | -async def test_get_defined_screener_no_session(): |
81 | | - """Test that get_defined_screener does not pass session to yf.screen.""" |
82 | | - with patch("yfinance.screen") as mock_screen: |
83 | | - mock_screen.return_value = { |
84 | | - "quotes": [ |
85 | | - { |
86 | | - "symbol": "AAPL", |
87 | | - "exchangeTimezoneName": "America/New_York", |
88 | | - "regularMarketTime": 1700000000, |
89 | | - "regularMarketChange": 1.23, |
90 | | - "regularMarketVolume": 1000, |
91 | | - } |
92 | | - ], |
93 | | - "total": 1, |
94 | | - } |
95 | | - |
96 | | - await get_defined_screener("day_gainers", limit=1) |
97 | | - |
98 | | - # Verify yf.screen was called without session parameter |
99 | | - assert mock_screen.called |
100 | | - for call in mock_screen.call_args_list: |
101 | | - call_kwargs = call[1] if len(call) > 1 and call[1] else {} |
102 | | - assert ( |
103 | | - "session" not in call_kwargs |
104 | | - ), "yf.screen should not be called with session parameter" |
105 | | - |
106 | | - |
107 | | -def test_get_futures_symbols_no_session(): |
108 | | - """Test that get_futures_symbols does not pass session to YfData.""" |
109 | | - with patch("yfinance.data.YfData") as mock_yfdata: |
110 | | - mock_instance = MagicMock() |
111 | | - mock_instance.get_raw_json.return_value = { |
112 | | - "quoteSummary": { |
113 | | - "result": [{"futuresChain": {"futures": [{"symbol": "ES=F"}]}}] |
114 | | - } |
115 | | - } |
116 | | - mock_yfdata.return_value = mock_instance |
117 | | - |
118 | | - get_futures_symbols("ES") |
119 | | - |
120 | | - # Verify YfData was called without session parameter |
121 | | - mock_yfdata.assert_called_once() |
122 | | - call_kwargs = mock_yfdata.call_args[1] if mock_yfdata.call_args[1] else {} |
123 | | - assert ( |
124 | | - "session" not in call_kwargs |
125 | | - ), "YfData should not be called with session parameter" |
126 | | - |
127 | | - |
128 | | -def test_yf_download_no_session(): |
129 | | - """Test that yf_download does not pass session to yf.download.""" |
130 | | - with patch("yfinance.download") as mock_download: |
131 | | - # Mock DataFrame with MultiIndex columns as returned by yfinance.download with group_by="ticker" |
132 | | - columns = pd.MultiIndex.from_tuples( |
133 | | - [ |
134 | | - ("AAPL", "Open"), |
135 | | - ("AAPL", "High"), |
136 | | - ("AAPL", "Low"), |
137 | | - ("AAPL", "Close"), |
138 | | - ("AAPL", "Adj Close"), |
139 | | - ] |
140 | | - ) |
141 | | - idx = pd.to_datetime(["2023-01-03"]) |
142 | | - idx.name = "Date" |
143 | | - mock_data = pd.DataFrame([[100, 110, 90, 105, 105]], columns=columns, index=idx) |
144 | | - mock_download.return_value = mock_data |
145 | | - |
146 | | - yf_download("AAPL", start_date="2023-01-01", end_date="2023-01-10") |
147 | | - |
148 | | - # Verify yf.download was called without session parameter |
149 | | - assert mock_download.called |
150 | | - for call in mock_download.call_args_list: |
151 | | - call_kwargs = call[1] if len(call) > 1 and call[1] else {} |
152 | | - assert ( |
153 | | - "session" not in call_kwargs |
154 | | - ), "yf.download should not be called with session parameter" |
0 commit comments