File size: 1,090 Bytes
ee37d63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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