File size: 18,907 Bytes
4c2ff6a
 
 
00366c3
4c2ff6a
 
00366c3
 
ae185d5
4c2ff6a
 
 
ae185d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00366c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c2ff6a
 
 
 
 
 
 
 
 
 
 
a0cbb64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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"]