Skip to content

Commit 5159bd7

Browse files
authored
fix: stabilize yfinance dividend TTM fixtures (#2214)
1 parent 2942108 commit 5159bd7

1 file changed

Lines changed: 13 additions & 29 deletions

File tree

tests/test_yfinance_fundamental_adapter.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@
88
"""
99
from __future__ import annotations
1010

11-
from datetime import datetime, timezone
1211
import unittest
13-
from datetime import datetime, timedelta
12+
from datetime import datetime, timezone
1413
from unittest.mock import patch, MagicMock
1514

1615
import pandas as pd
17-
import pytz
1816

1917
from data_provider.yfinance_fundamental_adapter import (
2018
YfinanceFundamentalAdapter,
2119
_convert_to_yf_symbol,
2220
)
2321

2422

23+
_DIVIDEND_EVENT_DATES = (
24+
"2025-08-11",
25+
"2025-11-10",
26+
"2026-02-09",
27+
"2026-05-11",
28+
)
29+
30+
2531
def _build_mock_ticker(
2632
info: dict,
2733
income_stmt: pd.DataFrame | None = None,
@@ -82,14 +88,6 @@ def test_populates_growth_earnings_dividend_boards_for_us_stock(self) -> None:
8288
"trailingAnnualDividendRate": 1.04,
8389
"dividendYield": 0.36,
8490
}
85-
income_df = pd.DataFrame(
86-
{
87-
pd.Timestamp("2026-03-31"): {"Total Revenue": 1.11e11, "Net Income": 2.95e10},
88-
pd.Timestamp("2025-12-31"): {"Total Revenue": 1.24e11, "Net Income": 3.62e10},
89-
pd.Timestamp("2025-09-30"): {"Total Revenue": 9.49e10, "Net Income": 2.49e10},
90-
pd.Timestamp("2025-06-30"): {"Total Revenue": 9.40e10, "Net Income": 2.34e10},
91-
}
92-
)
9391
# Need at least 5 columns to trigger statement-derived YoY.
9492
income_df_with_yoy = pd.DataFrame(
9593
{
@@ -106,18 +104,10 @@ def test_populates_growth_earnings_dividend_boards_for_us_stock(self) -> None:
106104
pd.Timestamp("2025-12-31"): {"Operating Cash Flow": 3.5e10},
107105
}
108106
)
109-
# Use dates relative to now so the 365-day TTM window always
110-
# contains all 4 events regardless of when the test runs (#2204).
111-
now_ny = datetime.now(pytz.timezone("America/New_York"))
112107
dividends = pd.Series(
113108
[0.26, 0.26, 0.26, 0.27],
114109
index=pd.DatetimeIndex(
115-
[
116-
(now_ny - timedelta(days=330)).strftime("%Y-%m-%d"),
117-
(now_ny - timedelta(days=240)).strftime("%Y-%m-%d"),
118-
(now_ny - timedelta(days=150)).strftime("%Y-%m-%d"),
119-
(now_ny - timedelta(days=60)).strftime("%Y-%m-%d"),
120-
],
110+
_DIVIDEND_EVENT_DATES,
121111
tz="America/New_York",
122112
),
123113
name="Dividends",
@@ -147,6 +137,7 @@ def test_populates_growth_earnings_dividend_boards_for_us_stock(self) -> None:
147137
# info.dividendYield (0.36) is intentionally ignored when TTM cash exists.
148138
self.assertAlmostEqual(div["ttm_dividend_yield_pct"], 0.3762, places=4)
149139
self.assertEqual(div["currency"], "USD")
140+
self.assertEqual(div["events"][0]["ex_dividend_date"], "2026-05-11")
150141
self.assertEqual(
151142
bundle["belong_boards"],
152143
[
@@ -160,15 +151,8 @@ def test_dividends_parsed_from_single_column_dataframe(self) -> None:
160151
# Series. Without coercion, `.items()` yields (column_name, Series), every event
161152
# is dropped, and TTM silently falls back to the annual-rate estimate — the real
162153
# bug seen on live US/HK/JP/KR/TW reports (24.0 / "0 次" instead of the true sum).
163-
# Use dates relative to now so the 365-day TTM window is always satisfied (#2204).
164-
now_ny = datetime.now(pytz.timezone("America/New_York"))
165154
idx = pd.DatetimeIndex(
166-
[
167-
(now_ny - timedelta(days=330)).strftime("%Y-%m-%d"),
168-
(now_ny - timedelta(days=240)).strftime("%Y-%m-%d"),
169-
(now_ny - timedelta(days=150)).strftime("%Y-%m-%d"),
170-
(now_ny - timedelta(days=60)).strftime("%Y-%m-%d"),
171-
],
155+
_DIVIDEND_EVENT_DATES,
172156
tz="America/New_York",
173157
)
174158
dividends_df = pd.DataFrame({"Dividends": [0.26, 0.26, 0.26, 0.27]}, index=idx)
@@ -191,7 +175,7 @@ def test_dividends_parsed_from_single_column_dataframe(self) -> None:
191175

192176
def test_ttm_dividend_window_uses_as_of_date_cutoff(self) -> None:
193177
idx = pd.DatetimeIndex(
194-
["2025-08-11", "2025-11-10", "2026-02-09", "2026-05-11"],
178+
_DIVIDEND_EVENT_DATES,
195179
tz="America/New_York",
196180
)
197181
dividends = pd.Series([0.26, 0.26, 0.26, 0.27], index=idx, name="Dividends")

0 commit comments

Comments
 (0)