|
| 1 | +"""Tests for yfinance.data internal helpers.""" |
| 2 | +import unittest |
| 3 | +from functools import lru_cache |
| 4 | + |
| 5 | +from yfinance.data import lru_cache_freezeargs |
| 6 | +from yfinance.utils import frozendict |
| 7 | + |
| 8 | + |
| 9 | +class TestFrozenDict(unittest.TestCase): |
| 10 | + def test_is_dict_subclass(self): |
| 11 | + d = frozendict({"a": 1, "b": 2}) |
| 12 | + self.assertEqual(d["a"], 1) |
| 13 | + self.assertEqual(d.get("b"), 2) |
| 14 | + self.assertEqual(set(d.keys()), {"a", "b"}) |
| 15 | + self.assertIsInstance(d, dict) |
| 16 | + |
| 17 | + def test_is_hashable(self): |
| 18 | + d1 = frozendict({"a": 1, "b": 2}) |
| 19 | + d2 = frozendict({"b": 2, "a": 1}) |
| 20 | + self.assertEqual(hash(d1), hash(d2)) |
| 21 | + self.assertEqual(len({d1, d2}), 1) |
| 22 | + |
| 23 | + def test_mutation_raises(self): |
| 24 | + d = frozendict({"a": 1}) |
| 25 | + with self.assertRaises(TypeError): |
| 26 | + d["b"] = 2 |
| 27 | + with self.assertRaises(TypeError): |
| 28 | + del d["a"] |
| 29 | + with self.assertRaises(AttributeError): |
| 30 | + d.pop("a") |
| 31 | + with self.assertRaises(AttributeError): |
| 32 | + d.clear() |
| 33 | + with self.assertRaises(AttributeError): |
| 34 | + d.update({"x": 9}) |
| 35 | + |
| 36 | + def test_lru_cache_integration(self): |
| 37 | + call_count = {"n": 0} |
| 38 | + |
| 39 | + @lru_cache_freezeargs |
| 40 | + @lru_cache(maxsize=8) |
| 41 | + def fn(params): |
| 42 | + call_count["n"] += 1 |
| 43 | + return sum(params.values()) |
| 44 | + |
| 45 | + self.assertEqual(fn({"a": 1, "b": 2}), 3) |
| 46 | + self.assertEqual(fn({"a": 1, "b": 2}), 3) |
| 47 | + self.assertEqual(call_count["n"], 1) |
| 48 | + self.assertEqual(fn({"a": 1, "b": 3}), 4) |
| 49 | + self.assertEqual(call_count["n"], 2) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + unittest.main() |
0 commit comments