|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from microdf import MicroDataFrame |
| 6 | + |
| 7 | +from policyengine.core import Simulation |
| 8 | +from policyengine.core.cache import LRUCache |
| 9 | +from policyengine.tax_benefit_models.uk import ( |
| 10 | + PolicyEngineUKDataset, |
| 11 | + UKYearData, |
| 12 | + uk_latest, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def test_simulation_cache_hit(): |
| 17 | + """Test that simulation caching works with UK simulations.""" |
| 18 | + person_df = MicroDataFrame( |
| 19 | + pd.DataFrame( |
| 20 | + { |
| 21 | + "person_id": [1, 2, 3], |
| 22 | + "benunit_id": [1, 1, 2], |
| 23 | + "household_id": [1, 1, 2], |
| 24 | + "age": [30, 25, 40], |
| 25 | + "employment_income": [50000, 30000, 60000], |
| 26 | + "person_weight": [1.0, 1.0, 1.0], |
| 27 | + } |
| 28 | + ), |
| 29 | + weights="person_weight", |
| 30 | + ) |
| 31 | + |
| 32 | + benunit_df = MicroDataFrame( |
| 33 | + pd.DataFrame( |
| 34 | + { |
| 35 | + "benunit_id": [1, 2], |
| 36 | + "benunit_weight": [1.0, 1.0], |
| 37 | + } |
| 38 | + ), |
| 39 | + weights="benunit_weight", |
| 40 | + ) |
| 41 | + |
| 42 | + household_df = MicroDataFrame( |
| 43 | + pd.DataFrame( |
| 44 | + { |
| 45 | + "household_id": [1, 2], |
| 46 | + "household_weight": [1.0, 1.0], |
| 47 | + } |
| 48 | + ), |
| 49 | + weights="household_weight", |
| 50 | + ) |
| 51 | + |
| 52 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 53 | + filepath = os.path.join(tmpdir, "test.h5") |
| 54 | + |
| 55 | + dataset = PolicyEngineUKDataset( |
| 56 | + name="Test", |
| 57 | + description="Test dataset", |
| 58 | + filepath=filepath, |
| 59 | + year=2024, |
| 60 | + data=UKYearData( |
| 61 | + person=person_df, benunit=benunit_df, household=household_df |
| 62 | + ), |
| 63 | + ) |
| 64 | + |
| 65 | + simulation = Simulation( |
| 66 | + dataset=dataset, |
| 67 | + tax_benefit_model_version=uk_latest, |
| 68 | + output_dataset=dataset, |
| 69 | + ) |
| 70 | + |
| 71 | + # Import the cache |
| 72 | + from policyengine.core.simulation import _cache |
| 73 | + |
| 74 | + # Manually add to cache (simulating what ensure() does) |
| 75 | + _cache.add(simulation.id, simulation) |
| 76 | + |
| 77 | + # Verify simulation is in cache |
| 78 | + assert simulation.id in _cache._cache |
| 79 | + assert len(_cache) >= 1 |
| 80 | + |
| 81 | + # Verify cache returns same object |
| 82 | + cached_sim = _cache.get(simulation.id) |
| 83 | + assert cached_sim is simulation |
| 84 | + |
| 85 | + # Clear cache for other tests |
| 86 | + _cache.clear() |
| 87 | + |
| 88 | + |
| 89 | +def test_lru_cache_eviction(): |
| 90 | + """Test that LRU cache properly evicts old items.""" |
| 91 | + cache = LRUCache[str](max_size=3) |
| 92 | + |
| 93 | + cache.add("a", "value_a") |
| 94 | + cache.add("b", "value_b") |
| 95 | + cache.add("c", "value_c") |
| 96 | + |
| 97 | + assert len(cache) == 3 |
| 98 | + assert cache.get("a") == "value_a" |
| 99 | + |
| 100 | + # Add fourth item, should evict 'b' (least recently used) |
| 101 | + cache.add("d", "value_d") |
| 102 | + |
| 103 | + assert len(cache) == 3 |
| 104 | + assert cache.get("b") is None |
| 105 | + assert cache.get("a") == "value_a" |
| 106 | + assert cache.get("c") == "value_c" |
| 107 | + assert cache.get("d") == "value_d" |
| 108 | + |
| 109 | + |
| 110 | +def test_lru_cache_access_updates_order(): |
| 111 | + """Test that accessing items updates their position in LRU order.""" |
| 112 | + cache = LRUCache[str](max_size=3) |
| 113 | + |
| 114 | + cache.add("a", "value_a") |
| 115 | + cache.add("b", "value_b") |
| 116 | + cache.add("c", "value_c") |
| 117 | + |
| 118 | + # Access 'a' to move it to most recently used |
| 119 | + cache.get("a") |
| 120 | + |
| 121 | + # Add fourth item, should evict 'b' (now least recently used) |
| 122 | + cache.add("d", "value_d") |
| 123 | + |
| 124 | + assert cache.get("a") == "value_a" |
| 125 | + assert cache.get("b") is None |
| 126 | + assert cache.get("c") == "value_c" |
| 127 | + assert cache.get("d") == "value_d" |
| 128 | + |
| 129 | + |
| 130 | +def test_lru_cache_clear(): |
| 131 | + """Test that clearing cache works properly.""" |
| 132 | + cache = LRUCache[str](max_size=10) |
| 133 | + |
| 134 | + cache.add("a", "value_a") |
| 135 | + cache.add("b", "value_b") |
| 136 | + cache.add("c", "value_c") |
| 137 | + |
| 138 | + assert len(cache) == 3 |
| 139 | + |
| 140 | + cache.clear() |
| 141 | + |
| 142 | + assert len(cache) == 0 |
| 143 | + assert cache.get("a") is None |
| 144 | + assert cache.get("b") is None |
| 145 | + assert cache.get("c") is None |
0 commit comments