PaperTrade / tests /test_data_sources_cache.py
Khanna, Videh Rakesh Rakesh
Claude Sonnet 5.5
Fix INTRADAY validation for tickers Yahoo cannot serve 60d of 15m bars
a0cbb64
Raw History Blame Contribute Delete
18.9 kB
import os
import tempfile
import unittest
from unittest.mock import Mock, patch
import data_sources
import intraday_live
import pandas as pd
import requests
class DataSourcesCacheTests(unittest.TestCase):
def setUp(self):
environment = patch.dict(os.environ, {"FINANCE_QUERY_ENABLED": "0", "JUGAAD_LIVE_ENABLED": "0"})
environment.start()
self.addCleanup(environment.stop)
def test_finance_query_quote_preserves_delay_and_upstream(self):
now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
response = Mock()
response.json.return_value = {
"symbol": "RELIANCE.NS", "exchange": "NSI", "currency": "INR",
"regularMarketPrice": 1214.5, "regularMarketTime": now - 2,
"exchangeDataDelayedBy": 15,
}
with patch.dict(os.environ, {"FINANCE_QUERY_ENABLED": "1"}), patch.object(
data_sources.time, "time", return_value=now
), patch.object(data_sources._SESSION, "get", return_value=response) as request:
quote = data_sources.fetch_quote_finance_query("RELIANCE.NS")
self.assertEqual(quote["price"], 1214.5)
self.assertEqual(quote["source"], "finance_query")
self.assertEqual(quote["upstream"], "yahoo")
self.assertEqual(quote["delay_minutes"], 15)
self.assertFalse(quote["is_live"])
self.assertLessEqual(request.call_args.kwargs["timeout"], 8)
def test_finance_query_quote_rejects_wrong_symbol(self):
response = Mock()
response.json.return_value = {
"symbol": "TCS.NS", "exchange": "NSI", "currency": "INR",
"regularMarketPrice": 1214.5, "regularMarketTime": 1792054514,
}
with patch.dict(os.environ, {"FINANCE_QUERY_ENABLED": "1"}), patch.object(
data_sources._SESSION, "get", return_value=response
):
self.assertIsNone(data_sources.fetch_quote_finance_query("RELIANCE.NS"))
def test_jugaad_quote_requires_explicit_opt_in(self):
with patch.dict(os.environ, {"JUGAAD_LIVE_ENABLED": "0"}), patch.object(
data_sources, "_SESSION"
) as session:
self.assertIsNone(data_sources.fetch_quote_jugaad("RELIANCE.NS"))
session.get.assert_not_called()
def test_strict_prices_do_not_call_optional_snapshot_providers(self):
client = Mock(enabled=False, configured=False, yahoo_fallback_active=False)
client.quotes.return_value = {}
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(
data_sources, "fetch_quote_finance_query"
) as finance_query, patch.object(data_sources, "fetch_quote_jugaad") as jugaad:
self.assertIsNone(data_sources.fetch_live_price("RELIANCE.NS", allow_delayed=False))
finance_query.assert_not_called()
jugaad.assert_not_called()
def _finance_payload(self):
return {
"symbol": "RELIANCE.NS", "exchange": "NSI", "currency": "INR",
"regularMarketPrice": 1214.5, "regularMarketTime": self._quote_now - 2,
"exchangeDataDelayedBy": 15,
}
def _finance_result(self, payload):
response = Mock()
response.json.return_value = payload
with patch.dict(os.environ, {"FINANCE_QUERY_ENABLED": "1"}), patch.object(
data_sources.time, "time", return_value=self._quote_now
), patch.object(data_sources._SESSION, "get", return_value=response):
return data_sources.fetch_quote_finance_query("RELIANCE.NS")
def test_finance_query_rejects_invalid_payloads(self):
self._quote_now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
changes = (
{"regularMarketPrice": 0}, {"regularMarketPrice": -1},
{"regularMarketPrice": float("nan")}, {"regularMarketPrice": float("inf")},
{"regularMarketPrice": True}, {"regularMarketPrice": None},
{"regularMarketTime": self._quote_now - 1201},
{"regularMarketTime": self._quote_now + 6},
{"regularMarketTime": float("nan")}, {"regularMarketTime": True},
{"regularMarketTime": None}, {"exchange": "NYQ"}, {"currency": "USD"},
{"exchangeDataDelayedBy": -1}, {"exchangeDataDelayedBy": "invalid"},
{"exchangeDataDelayedBy": float("inf")}, {"exchangeDataDelayedBy": True},
)
for change in changes:
with self.subTest(change=change):
self.assertIsNone(self._finance_result({**self._finance_payload(), **change}))
self.assertIsNone(self._finance_result([]))
def test_finance_query_rejects_previous_ist_day_even_when_recent(self):
self._quote_now = pd.Timestamp("2026-10-16 00:01", tz="Asia/Kolkata").timestamp()
payload = {**self._finance_payload(), "regularMarketTime": self._quote_now - 120}
self.assertIsNone(self._finance_result(payload))
def test_finance_query_missing_delay_remains_non_live(self):
self._quote_now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
payload = self._finance_payload()
payload.pop("exchangeDataDelayedBy")
quote = self._finance_result(payload)
self.assertIsNone(quote["delay_minutes"])
self.assertFalse(quote["is_live"])
def test_finance_query_zero_delay_does_not_claim_live_entitlement(self):
self._quote_now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
quote = self._finance_result({**self._finance_payload(), "exchangeDataDelayedBy": 0})
self.assertFalse(quote["is_live"])
def test_finance_query_bse_identity_is_supported(self):
now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
response = Mock()
response.json.return_value = {
"symbol": "RELIANCE.BO", "exchange": "BSE", "currency": "INR",
"regularMarketPrice": 1214.5, "regularMarketTime": now - 2,
}
with patch.dict(os.environ, {"FINANCE_QUERY_ENABLED": "1"}), patch.object(
data_sources.time, "time", return_value=now
), patch.object(data_sources._SESSION, "get", return_value=response):
self.assertEqual(data_sources.fetch_quote_finance_query("RELIANCE.BO")["ticker"], "RELIANCE.BO")
def test_snapshot_providers_skip_unsupported_or_unsafe_symbols(self):
with patch.dict(os.environ, {"FINANCE_QUERY_ENABLED": "1", "JUGAAD_LIVE_ENABLED": "1"}), patch.object(
data_sources._SESSION, "get"
) as request:
for ticker in ("RELIANCE", "AAPL", "../RELIANCE.NS", "RELIANCE.NS?token=bad", "a" * 50 + ".NS"):
with self.subTest(ticker=ticker):
self.assertIsNone(data_sources.fetch_quote_finance_query(ticker))
self.assertIsNone(data_sources.fetch_quote_jugaad(ticker))
self.assertIsNone(data_sources.fetch_quote_jugaad("RELIANCE.BO"))
request.assert_not_called()
def test_finance_query_http_and_json_failures_return_no_quote(self):
with patch.dict(os.environ, {"FINANCE_QUERY_ENABLED": "1"}):
for error in (requests.Timeout(), requests.HTTPError(), ValueError()):
with self.subTest(error=type(error).__name__), patch.object(
data_sources._SESSION, "get", side_effect=error
):
self.assertIsNone(data_sources.fetch_quote_finance_query("RELIANCE.NS"))
def _jugaad_result(self, payload):
response = Mock()
response.json.return_value = {"equityResponse": [payload]}
with patch.dict(os.environ, {"JUGAAD_LIVE_ENABLED": "1"}), patch.object(
data_sources.time, "time", return_value=self._quote_now
), patch.object(data_sources.requests.Session, "request", return_value=response) as request:
quote = data_sources.fetch_quote_jugaad("RELIANCE.NS")
return quote, request
def _jugaad_payload(self):
return {
"metaData": {"symbol": "RELIANCE", "lastUpdateTime": "15-Oct-2026 14:24:30"},
"orderBook": {"lastPrice": 1214.1},
}
def test_jugaad_quote_uses_bounded_sdk_transport_and_snapshot_time(self):
self._quote_now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
quote, request = self._jugaad_result(self._jugaad_payload())
self.assertIsNotNone(quote)
self.assertEqual(quote["price"], 1214.1)
self.assertEqual(quote["source"], "jugaad")
self.assertEqual(quote["upstream"], "nse_website")
self.assertEqual(quote["timestamp_basis"], "provider_quote_update")
self.assertFalse(quote["is_live"])
self.assertNotIn("trade_time", quote)
self.assertEqual(request.call_count, 2)
for call in request.call_args_list:
self.assertEqual(call.kwargs["timeout"], 3)
def test_jugaad_price_info_and_top_level_timestamp_are_supported(self):
self._quote_now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
payload = {
"metaData": {"symbol": "RELIANCE"}, "lastUpdateTime": "15-Oct-2026 14:24:30",
"priceInfo": {"lastPrice": 1214.1},
}
quote, _request = self._jugaad_result(payload)
self.assertEqual(quote["price"], 1214.1)
def test_jugaad_rejects_wrong_identity_stale_and_malformed_responses(self):
self._quote_now = pd.Timestamp("2026-10-15 14:25", tz="Asia/Kolkata").timestamp()
payloads = (
{**self._jugaad_payload(), "metaData": {"symbol": "TCS"}},
{**self._jugaad_payload(), "metaData": {"symbol": "RELIANCE", "lastUpdateTime": "15-Oct-2026 14:20:00"}},
{**self._jugaad_payload(), "metaData": {"symbol": "RELIANCE", "lastUpdateTime": "invalid"}},
[],
)
for payload in payloads:
with self.subTest(payload=payload):
quote, _request = self._jugaad_result(payload)
self.assertIsNone(quote)
def test_jugaad_timeout_is_not_retried(self):
with patch.dict(os.environ, {"JUGAAD_LIVE_ENABLED": "1"}), patch.object(
data_sources.requests.Session, "request", side_effect=requests.Timeout()
) as request:
self.assertIsNone(data_sources.fetch_quote_jugaad("RELIANCE.NS"))
request.assert_called_once()
def test_jugaad_malformed_sdk_envelope_returns_no_quote(self):
for payload in ({}, {"equityResponse": []}):
response = Mock()
response.json.return_value = payload
with self.subTest(payload=payload), patch.dict(os.environ, {"JUGAAD_LIVE_ENABLED": "1"}), patch.object(
data_sources.requests.Session, "request", return_value=response
):
self.assertIsNone(data_sources.fetch_quote_jugaad("RELIANCE.NS"))
def test_optional_provider_result_is_returned_with_provenance(self):
client = Mock(enabled=False, configured=False, yahoo_fallback_active=False)
client.quotes.return_value = {}
quote = {"ticker": "RELIANCE.NS", "price": 1214.1, "source": "finance_query", "is_live": False}
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(
data_sources, "fetch_quote_finance_query", return_value=quote
), patch.object(data_sources, "fetch_quote_jugaad") as jugaad, patch.object(
data_sources, "_save_live_price_cache"
), patch.object(data_sources, "_yf_download_timed") as yahoo:
result = data_sources.fetch_live_quote("RELIANCE.NS")
self.assertEqual(result["source"], "finance_query")
self.assertFalse(result["is_live"])
jugaad.assert_not_called()
yahoo.assert_not_called()
def test_jugaad_is_next_when_finance_query_fails(self):
client = Mock(enabled=False, configured=False, yahoo_fallback_active=False)
client.quotes.return_value = {}
quote = {"ticker": "RELIANCE.NS", "price": 1214.1, "source": "jugaad", "is_live": False}
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(
data_sources, "fetch_quote_finance_query", return_value=None
), patch.object(data_sources, "fetch_quote_jugaad", return_value=quote), patch.object(
data_sources, "_save_live_price_cache"
):
self.assertEqual(data_sources.fetch_live_quote("RELIANCE.NS")["source"], "jugaad")
def test_configured_dhan_keeps_yahoo_only_fallback(self):
for paused in (False, True):
with self.subTest(paused=paused):
client = Mock(enabled=True, configured=True, yahoo_fallback_active=paused)
client.quotes.return_value = {}
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(
data_sources, "fetch_quote_finance_query"
) as finance_query, patch.object(data_sources, "fetch_quote_jugaad") as jugaad, patch.object(
data_sources, "_yf_download_timed", return_value=None
), patch.object(data_sources, "_yf_blocked", return_value=False):
self.assertIsNone(data_sources.fetch_live_price("RELIANCE.NS"))
finance_query.assert_not_called()
jugaad.assert_not_called()
def test_yahoo_daily_fallback_skips_all_other_network_providers(self):
client = Mock(enabled=True, configured=True, history_adjusted=False, yahoo_fallback_active=True)
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(
data_sources, "_load_sql_cache", return_value=None
), patch.object(data_sources, "_ohlcv_is_failed", return_value=False), patch.object(
data_sources, "_ohlcv_mark_failed"
), patch.object(data_sources, "fetch_ohlcv_yfinance", return_value=None) as yahoo, patch.object(
data_sources, "fetch_ohlcv_stooq"
) as stooq, patch.object(data_sources, "fetch_ohlcv_openchart") as openchart, patch.object(
data_sources, "fetch_ohlcv_jugaad"
) as jugaad, patch.object(data_sources, "fetch_ohlcv_bse_official") as bse:
with self.assertRaises(ValueError):
data_sources.fetch_ohlcv("RELIANCE.NS")
self.assertTrue(yahoo.called)
for provider in (stooq, openchart, jugaad, bse):
provider.assert_not_called()
def test_yahoo_daily_fallback_does_not_return_dhan_adjusted_cache(self):
client = Mock(enabled=True, configured=True, history_adjusted=True, yahoo_fallback_active=True)
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(data_sources, "_load_sql_cache") as cache:
self.assertIsNone(data_sources.fetch_ohlcv_dhan("RELIANCE.NS"))
cache.assert_not_called()
def test_yahoo_price_fallback_never_tries_bse_quotes(self):
client = Mock(enabled=True, configured=True, yahoo_fallback_active=True)
client.quotes.return_value = {}
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(data_sources, "_resolve_bse_code") as bse, patch.object(
data_sources, "_yf_download_timed", return_value=None
) as yahoo, patch.object(data_sources, "_yf_blocked", return_value=False):
self.assertIsNone(data_sources.fetch_live_price("TCS.BO"))
bse.assert_not_called()
self.assertTrue(yahoo.called)
def test_live_quote_metadata_preserves_rounded_price_and_provider(self):
with patch.object(data_sources, "_save_live_price_cache"):
price = data_sources._remember_live_price("RELIANCE.NS", 2500.254, {"source": "dhan", "price": 2500.254})
with patch.object(data_sources, "fetch_live_price", return_value=price):
quote = data_sources.fetch_live_quote("RELIANCE.NS")
self.assertEqual(quote["source"], "dhan")
self.assertEqual(quote["price"], 2500.25)
def test_dhan_live_price_is_preferred_even_in_strict_mode(self):
quote = {"ticker": "RELIANCE.NS", "price": 2500.25, "source": "dhan", "trade_time": 123,
"observed_at": 124, "stale": False}
client = Mock(enabled=True, configured=True)
client.quotes.return_value = {"RELIANCE.NS": quote}
with patch("dhan_client.get_dhan_client", return_value=client), patch(
"market_calendar.market_status", return_value={"status": "OPEN"}
), patch.object(data_sources, "_save_live_price_cache"), patch.object(data_sources, "_yf_download_timed") as yahoo:
self.assertEqual(data_sources.fetch_live_price("RELIANCE.NS", allow_delayed=False), 2500.25)
yahoo.assert_not_called()
def test_dhan_candles_are_preferred_without_changing_intraday_shape(self):
bars = pd.DataFrame({"Open": [100], "High": [102], "Low": [99], "Close": [101], "Volume": [100]},
index=pd.date_range("2026-10-12 09:15", periods=1, tz="Asia/Kolkata"))
bars.attrs["source"] = "dhan"
client = Mock()
client.history.return_value = bars
with patch("dhan_client.get_dhan_client", return_value=client), patch.object(
intraday_live, "_yf_download_bounded"
) as yahoo:
result = intraday_live.get_intraday_bars("RELIANCE.NS", timeout=10)
self.assertEqual(result.attrs["source"], "dhan")
self.assertEqual(result.iloc[0]["Close"], 101)
yahoo.assert_not_called()
def test_live_price_survives_process_memory_reset(self):
with tempfile.TemporaryDirectory() as tmpdir:
cache_path = os.path.join(tmpdir, "ohlcv_cache.db")
with patch.object(data_sources, "_OHLCV_DB_PATH", cache_path):
data_sources._init_ohlcv_db()
data_sources._save_live_price_cache("RELIANCE.NS", 2500.25)
self.assertEqual(data_sources.get_cached_price("RELIANCE.NS"), 2500.25)
if __name__ == "__main__":
unittest.main()
def test_intraday_bars_step_down_when_yahoo_rejects_the_long_window():
import types
from unittest.mock import MagicMock, patch
import pandas as pd
import intraday_live
bars = pd.DataFrame({"High": [1.0], "Low": [1.0], "Close": [1.0]},
index=pd.to_datetime(["2026-10-30 09:15"]).tz_localize("Asia/Kolkata"))
dhan = MagicMock()
dhan.history.return_value = None
with patch("dhan_client.get_dhan_client", return_value=dhan), patch.object(intraday_live, "_HAS_YF", True), \
patch.object(intraday_live, "_yf_download_bounded", side_effect=[None, bars]) as download:
result = intraday_live.get_intraday_bars("NEW.NS", interval="15m", period="60d")
assert result is not None and not result.empty
assert [call.args[1] for call in download.call_args_list] == ["60d", "30d"]