DockerSpace / tests /test_margin_flow.py
DennisChan0909's picture
Backup current stock predictor strategies
ee37d63
Raw
History Blame Contribute Delete
1.09 kB
import logging
import pytest
from data import margin_flow
class _BadResponse:
def raise_for_status(self):
return None
def json(self):
raise ValueError("not json")
def test_margin_fetch_failure_is_debug_by_default(monkeypatch, caplog):
margin_flow._DAILY_CACHE.clear()
monkeypatch.delenv("ENABLE_MARGIN_FETCH_WARNINGS", raising=False)
monkeypatch.setattr(margin_flow.requests, "get", lambda *args, **kwargs: _BadResponse())
with caplog.at_level(logging.WARNING):
rows = margin_flow._fetch_daily("2025-04-28")
assert rows == {}
assert "margin fetch failed" not in caplog.text
def test_margin_fetch_failure_warning_can_be_enabled(monkeypatch, caplog):
margin_flow._DAILY_CACHE.clear()
monkeypatch.setenv("ENABLE_MARGIN_FETCH_WARNINGS", "1")
monkeypatch.setattr(margin_flow.requests, "get", lambda *args, **kwargs: _BadResponse())
with caplog.at_level(logging.WARNING):
rows = margin_flow._fetch_daily("2025-04-29")
assert rows == {}
assert "margin fetch failed for 2025-04-29" in caplog.text