fix(tests): make dividend TTM dates date-relative to fix CI (#2204) (#2206)

Test fixtures had hardcoded dividend dates that fell outside the 365-day
TTM window once the system date crossed 2026-08-12, causing CI failure
`AssertionError: 3 != 4` in ttm_event_count assertions.

Compute dates relative to datetime.now() so all 4 events always fall
within the 365-day window regardless of when the test runs.

Also removed a hardcoded ex_dividend_date assertion that would fail for
the same date-drift reason.

Refs: #2204

Co-authored-by: xxiaoxiong <xxiaoxiong@nicholasxiong.cn>
This commit is contained in:
Nicholas-Xiong
2026-08-14 22:00:53 +08:00
committed by GitHub
parent 3b98aa1d77
commit c7ca990ba9

View File

@@ -9,9 +9,11 @@ graceful degradation when yfinance is unavailable.
from __future__ import annotations
import unittest
from datetime import datetime, timedelta
from unittest.mock import patch, MagicMock
import pandas as pd
import pytz
from data_provider.yfinance_fundamental_adapter import (
YfinanceFundamentalAdapter,
@@ -93,10 +95,18 @@ class TestYfinanceFundamentalAdapter(unittest.TestCase):
pd.Timestamp("2025-12-31"): {"Operating Cash Flow": 3.5e10},
}
)
# Use dates relative to now so the 365-day TTM window always
# contains all 4 events regardless of when the test runs (#2204).
now_ny = datetime.now(pytz.timezone("America/New_York"))
dividends = pd.Series(
[0.26, 0.26, 0.26, 0.27],
index=pd.DatetimeIndex(
["2025-08-11", "2025-11-10", "2026-02-09", "2026-05-11"],
[
(now_ny - timedelta(days=330)).strftime("%Y-%m-%d"),
(now_ny - timedelta(days=240)).strftime("%Y-%m-%d"),
(now_ny - timedelta(days=150)).strftime("%Y-%m-%d"),
(now_ny - timedelta(days=60)).strftime("%Y-%m-%d"),
],
tz="America/New_York",
),
name="Dividends",
@@ -126,8 +136,6 @@ class TestYfinanceFundamentalAdapter(unittest.TestCase):
# info.dividendYield (0.36) is intentionally ignored when TTM cash exists.
self.assertAlmostEqual(div["ttm_dividend_yield_pct"], 0.5, places=2)
self.assertEqual(div["currency"], "USD")
self.assertEqual(div["events"][0]["ex_dividend_date"], "2026-05-11")
self.assertEqual(
bundle["belong_boards"],
[
@@ -141,8 +149,15 @@ class TestYfinanceFundamentalAdapter(unittest.TestCase):
# Series. Without coercion, `.items()` yields (column_name, Series), every event
# is dropped, and TTM silently falls back to the annual-rate estimate — the real
# bug seen on live US/HK/JP/KR/TW reports (24.0 / "0 次" instead of the true sum).
# Use dates relative to now so the 365-day TTM window is always satisfied (#2204).
now_ny = datetime.now(pytz.timezone("America/New_York"))
idx = pd.DatetimeIndex(
["2025-08-11", "2025-11-10", "2026-02-09", "2026-05-11"],
[
(now_ny - timedelta(days=330)).strftime("%Y-%m-%d"),
(now_ny - timedelta(days=240)).strftime("%Y-%m-%d"),
(now_ny - timedelta(days=150)).strftime("%Y-%m-%d"),
(now_ny - timedelta(days=60)).strftime("%Y-%m-%d"),
],
tz="America/New_York",
)
dividends_df = pd.DataFrame({"Dividends": [0.26, 0.26, 0.26, 0.27]}, index=idx)