Spaces:
Running
Running
Download tests/test_universe_cache.py from V1deh/PaperTrade: direct link, hf CLI and curl.
- Browser
- Download file 5.32 kB
-
https://huggingface.co/spaces/V1deh/PaperTrade/resolve/main/tests/test_universe_cache.py
- Command line
-
hf download hf://spaces/V1deh/PaperTrade/tests/test_universe_cache.py
-
curl -L -o test_universe_cache.py https://huggingface.co/spaces/V1deh/PaperTrade/resolve/main/tests/test_universe_cache.py
5.32 kB
| import json | |
| import os | |
| import tempfile | |
| import time | |
| import unittest | |
| from unittest.mock import Mock, patch, mock_open | |
| import universe | |
| class UniverseCacheTests(unittest.TestCase): | |
| def test_stale_cache_returns_without_synchronous_network_fetch(self): | |
| cached = {"RELIANCE.NS": "Reliance Industries Limited"} | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| cache_path = os.path.join(tmpdir, ".universe_cache.json") | |
| with open(cache_path, "w") as cache_file: | |
| json.dump({"ts": time.time() - universe._TTL - 1, "data": cached}, cache_file) | |
| with ( | |
| patch.object(universe, "_CACHE_PATH", cache_path), | |
| patch.object(universe, "_fetch_universe") as fetch_universe, | |
| patch.object(universe, "_schedule_universe_refresh") as schedule_refresh, | |
| ): | |
| result = universe.get_universe() | |
| self.assertEqual(result, cached) | |
| fetch_universe.assert_not_called() | |
| schedule_refresh.assert_called_once() | |
| class Nifty50UniverseTests(unittest.TestCase): | |
| def setUp(self): | |
| self.members = {f"STOCK{number}.NS": f"Company {number}" for number in range(50)} | |
| def test_official_csv_requires_exactly_fifty_unique_equities(self): | |
| rows = ["Company Name,Industry,Symbol,Series,ISIN"] | |
| rows.extend(f"{name},Bank,{ticker[:-3]},EQ,ISIN" for ticker, name in self.members.items()) | |
| response = Mock(text="\n".join(rows)) | |
| with patch("requests.get", return_value=response): | |
| self.assertEqual(universe._fetch_nifty50(), self.members) | |
| response.text = "\n".join(rows[:-1]) | |
| with patch("requests.get", return_value=response): | |
| with self.assertRaises(ValueError): | |
| universe._fetch_nifty50() | |
| def test_fresh_membership_cache_does_not_fetch_network(self): | |
| with patch.object(universe, "_read_nifty50_cache", return_value=(self.members, 10)), patch.object( | |
| universe, "_fetch_nifty50" | |
| ) as fetch: | |
| self.assertEqual(universe.get_nifty50_universe(), self.members) | |
| fetch.assert_not_called() | |
| def test_fetch_failure_uses_recent_verified_membership_only(self): | |
| with patch.object(universe, "_read_nifty50_cache", return_value=(self.members, universe._TTL + 10)), patch.object( | |
| universe, "_fetch_nifty50", side_effect=ValueError("offline") | |
| ): | |
| self.assertEqual(universe.get_nifty50_universe(), self.members) | |
| def test_missing_or_expired_membership_never_uses_whole_market(self): | |
| for cache in (({}, float("inf")), (self.members, 8 * 86400)): | |
| with self.subTest(cache_age=cache[1]), patch.object( | |
| universe, "_read_nifty50_cache", return_value=cache | |
| ), patch.object(universe, "_fetch_nifty50", side_effect=ValueError("offline")), patch.object( | |
| universe, "get_universe" | |
| ) as whole_market: | |
| with self.assertRaisesRegex(ValueError, "Nifty 50"): | |
| universe.get_nifty50_universe() | |
| whole_market.assert_not_called() | |
| def test_new_membership_is_saved(self): | |
| with patch.object(universe, "_read_nifty50_cache", return_value=({}, float("inf"))), patch.object( | |
| universe, "_fetch_nifty50", return_value=self.members | |
| ), patch.object(universe, "_write_nifty50_cache") as save: | |
| self.assertEqual(universe.get_nifty50_universe(), self.members) | |
| save.assert_called_once_with(self.members) | |
| def test_membership_cache_rejects_corrupt_or_incomplete_data(self): | |
| for content in ("not-json", json.dumps({"data": {"RELIANCE.NS": "Reliance"}, "ts": 1000})): | |
| with self.subTest(content=content), patch("builtins.open", mock_open(read_data=content)): | |
| self.assertEqual(universe._read_nifty50_cache(), ({}, float("inf"))) | |
| def test_membership_cache_reads_verified_data_and_age(self): | |
| content = json.dumps({"data": self.members, "ts": 1000}) | |
| with patch("builtins.open", mock_open(read_data=content)), patch("universe.time.time", return_value=1100): | |
| self.assertEqual(universe._read_nifty50_cache(), (self.members, 100)) | |
| def test_membership_cache_is_atomically_replaced(self): | |
| with patch("tempfile.NamedTemporaryFile") as writer, patch("universe.os.fsync") as sync, patch( | |
| "universe.os.replace" | |
| ) as replace: | |
| writer.return_value.__enter__.return_value.name = "/tmp/nifty50-test-cache" | |
| universe._write_nifty50_cache(self.members) | |
| sync.assert_called_once() | |
| replace.assert_called_once_with("/tmp/nifty50-test-cache", universe._NIFTY50_CACHE_PATH) | |
| def test_failed_atomic_replace_cleans_up_temporary_file(self): | |
| with patch("tempfile.NamedTemporaryFile") as writer, patch("universe.os.fsync"), patch( | |
| "universe.os.replace", side_effect=OSError("disk unavailable") | |
| ), patch("universe.os.path.exists", return_value=True), patch("universe.os.unlink") as unlink: | |
| writer.return_value.__enter__.return_value.name = "/tmp/nifty50-test-cache" | |
| universe._write_nifty50_cache(self.members) | |
| unlink.assert_called_once_with("/tmp/nifty50-test-cache") | |
| if __name__ == "__main__": | |
| unittest.main() |