import logging from data import institutional_flow class _BadResponse: def raise_for_status(self): return None def json(self): raise ValueError("not json") def test_institutional_fetch_failure_is_debug_by_default(monkeypatch, caplog): institutional_flow._DAILY_CACHE.clear() monkeypatch.delenv("ENABLE_INSTITUTIONAL_FETCH_WARNINGS", raising=False) monkeypatch.setattr(institutional_flow.requests, "get", lambda *args, **kwargs: _BadResponse()) with caplog.at_level(logging.WARNING): rows = institutional_flow._fetch_daily_market("TWSE", "2026-05-22") assert rows == {} assert "institutional TWSE fetch failed" not in caplog.text def test_institutional_fetch_failure_warning_can_be_enabled(monkeypatch, caplog): institutional_flow._DAILY_CACHE.clear() monkeypatch.setenv("ENABLE_INSTITUTIONAL_FETCH_WARNINGS", "1") monkeypatch.setattr(institutional_flow.requests, "get", lambda *args, **kwargs: _BadResponse()) with caplog.at_level(logging.WARNING): rows = institutional_flow._fetch_daily_market("TWSE", "2026-05-22") assert rows == {} assert "institutional TWSE fetch failed for 2026-05-22" in caplog.text