From 2340fe43964711193c7c6b68eabae13e1d3e5647 Mon Sep 17 00:00:00 2001 From: Yijia-Xiao Date: Wed, 23 Sep 2026 19:28:56 +0000 Subject: [PATCH] test(ohlcv): stamp cache mtimes in local time (#1372) - the cache reads mtimes back in local time; the tests wrote them as UTC --- tests/test_ohlcv_cache_freshness.py | 12 +++++++++--- tests/test_ohlcv_latest_bar.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/test_ohlcv_cache_freshness.py b/tests/test_ohlcv_cache_freshness.py index a30dc78d7..f11313c58 100644 --- a/tests/test_ohlcv_cache_freshness.py +++ b/tests/test_ohlcv_cache_freshness.py @@ -18,11 +18,17 @@ NOW = pd.Timestamp("2026-07-18 12:00") STALE = su.OHLCV_CACHE_TTL_SECONDS + 60 +def _stamp(path, ts): + """Set ``path``'s mtime to the wall-clock ``ts``, read back in local time as + the cache does. A naive ``pd.Timestamp.timestamp()`` would be taken as UTC.""" + t = ts.to_pydatetime().timestamp() + os.utime(path, (t, t)) + + def _write(tmp_path, name="AAPL-YFin-data.csv", age_seconds=0.0, last_date="2026-07-17"): f = tmp_path / name pd.DataFrame({"Date": [last_date], "Close": [100.0]}).to_csv(f, index=False) - written = NOW.timestamp() - age_seconds - os.utime(f, (written, written)) + _stamp(f, NOW - pd.Timedelta(seconds=age_seconds)) return f @@ -99,7 +105,7 @@ def test_one_cache_file_per_symbol_across_days(tmp_path, monkeypatch): monkeypatch.setattr(su.pd.Timestamp, "today", staticmethod(lambda now=now: now)) su.load_ohlcv("AAPL", "2026-07-17") written = list(tmp_path.glob("AAPL-*.csv")) - os.utime(written[0], (now.timestamp(), now.timestamp())) + _stamp(written[0], now) assert len(downloads) == 3, "each new day refetches" assert [p.name for p in tmp_path.iterdir()] == ["AAPL-YFin-data.csv"] diff --git a/tests/test_ohlcv_latest_bar.py b/tests/test_ohlcv_latest_bar.py index 548264cc5..1534fa36d 100644 --- a/tests/test_ohlcv_latest_bar.py +++ b/tests/test_ohlcv_latest_bar.py @@ -21,6 +21,13 @@ import pytest from tradingagents.dataflows import stockstats_utils as su from tradingagents.dataflows.symbol_utils import NoMarketDataError + +def _stamp(path, ts): + """Set ``path``'s mtime to the wall-clock ``ts``, read back in local time as + the cache does. A naive ``pd.Timestamp.timestamp()`` would be taken as UTC.""" + t = ts.to_pydatetime().timestamp() + os.utime(path, (t, t)) + # --- date normalization ----------------------------------------------------- @pytest.mark.unit @@ -93,7 +100,7 @@ def _run_load(monkeypatch, tmp_path, frame, curr_date): monkeypatch.setattr(su.pd.Timestamp, "today", staticmethod(lambda: today)) cache_file = tmp_path / "AAPL-YFin-data.csv" cache_file.write_text(frame.to_csv(index=False)) - os.utime(cache_file, (today.timestamp(), today.timestamp())) + _stamp(cache_file, today) def _fail_download(*a, **k): raise AssertionError("should use the seeded cache, not download") @@ -189,7 +196,7 @@ def test_the_snapshot_does_not_present_a_filled_price_as_reported(monkeypatch, t monkeypatch.setattr(su.pd.Timestamp, "today", staticmethod(lambda: today)) cache = tmp_path / "AAPL-YFin-data.csv" cache.write_text(frame.to_csv(index=False)) - os.utime(cache, (today.timestamp(), today.timestamp())) + _stamp(cache, today) monkeypatch.setattr(su.yf, "download", lambda *a, **k: (_ for _ in ()).throw( AssertionError("should read the seeded cache")))