|
| 1 | +""" |
| 2 | +Test suite for join index caching (Phase 4 optimization). |
| 3 | +
|
| 4 | +These tests verify that: |
| 5 | +1. Cache is populated on first subframe access |
| 6 | +2. Cache is reused for subsequent columns from same subframe |
| 7 | +3. Cache statistics are tracked correctly |
| 8 | +4. Cache is cleared after materialize_aliases completes |
| 9 | +""" |
| 10 | + |
| 11 | +import pytest |
| 12 | +import pandas as pd |
| 13 | +import numpy as np |
| 14 | +import sys |
| 15 | +import os |
| 16 | + |
| 17 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 18 | +from AliasDataFrame import AliasDataFrame |
| 19 | + |
| 20 | + |
| 21 | +class TestJoinIndexCaching: |
| 22 | + """Tests for join index caching optimization.""" |
| 23 | + |
| 24 | + @pytest.fixture |
| 25 | + def setup_with_subframe(self): |
| 26 | + """Create ADF with subframe containing multiple columns.""" |
| 27 | + np.random.seed(42) |
| 28 | + main_df = pd.DataFrame({ |
| 29 | + 'idx': np.random.randint(0, 100, 1000), |
| 30 | + 'x': np.random.randn(1000) |
| 31 | + }) |
| 32 | + |
| 33 | + sub_df = pd.DataFrame({ |
| 34 | + 'idx': np.arange(100), |
| 35 | + 'val_a': np.random.randn(100), |
| 36 | + 'val_b': np.random.randn(100), |
| 37 | + 'val_c': np.random.randn(100), |
| 38 | + 'val_d': np.random.randn(100), |
| 39 | + 'val_e': np.random.randn(100), |
| 40 | + }) |
| 41 | + |
| 42 | + adf = AliasDataFrame(main_df) |
| 43 | + adf.register_subframe('T', AliasDataFrame(sub_df), index_columns='idx') |
| 44 | + |
| 45 | + return adf |
| 46 | + |
| 47 | + def test_cache_initialized_empty(self, setup_with_subframe): |
| 48 | + """Cache should be empty before any subframe access.""" |
| 49 | + adf = setup_with_subframe |
| 50 | + assert adf._join_index_cache == {} |
| 51 | + assert adf._join_cache_hits == 0 |
| 52 | + assert adf._join_cache_misses == 0 |
| 53 | + |
| 54 | + def test_cache_populated_on_first_access(self, setup_with_subframe): |
| 55 | + """First subframe column access should populate cache.""" |
| 56 | + adf = setup_with_subframe |
| 57 | + adf.add_alias('col_a', 'T.val_a') |
| 58 | + adf.materialize_alias('col_a') |
| 59 | + |
| 60 | + assert 'T' in adf._join_index_cache |
| 61 | + cache_entry = adf._join_index_cache['T'] |
| 62 | + assert 'indices' in cache_entry |
| 63 | + assert 'missing_mask' in cache_entry |
| 64 | + assert cache_entry['n_rows'] == len(adf.df) |
| 65 | + |
| 66 | + def test_cache_hit_on_subsequent_access(self, setup_with_subframe): |
| 67 | + """Subsequent columns from same subframe should use cache.""" |
| 68 | + adf = setup_with_subframe |
| 69 | + |
| 70 | + adf.add_alias('col_a', 'T.val_a') |
| 71 | + adf.add_alias('col_b', 'T.val_b') |
| 72 | + adf.add_alias('col_c', 'T.val_c') |
| 73 | + |
| 74 | + adf._join_cache_hits = 0 |
| 75 | + adf._join_cache_misses = 0 |
| 76 | + |
| 77 | + adf.materialize_aliases(pattern=r'col_.*') |
| 78 | + |
| 79 | + assert adf._join_cache_misses == 1, f"Expected 1 miss, got {adf._join_cache_misses}" |
| 80 | + assert adf._join_cache_hits == 2, f"Expected 2 hits, got {adf._join_cache_hits}" |
| 81 | + |
| 82 | + def test_cache_cleared_after_materialize_batch(self, setup_with_subframe): |
| 83 | + """Cache should be cleared after materialize_aliases completes.""" |
| 84 | + adf = setup_with_subframe |
| 85 | + adf.add_alias('col_a', 'T.val_a') |
| 86 | + adf.materialize_aliases(pattern=r'col_.*') |
| 87 | + |
| 88 | + assert adf._join_index_cache == {}, "Cache should be empty after materialize_aliases" |
| 89 | + |
| 90 | + def test_cache_stats_reset_on_new_batch(self, setup_with_subframe): |
| 91 | + """Cache stats should reset at start of each materialize_aliases call.""" |
| 92 | + adf = setup_with_subframe |
| 93 | + |
| 94 | + adf.add_alias('col_a', 'T.val_a') |
| 95 | + adf.add_alias('col_b', 'T.val_b') |
| 96 | + adf.materialize_aliases(pattern=r'col_.*') |
| 97 | + |
| 98 | + adf.add_alias('col_c', 'T.val_c') |
| 99 | + adf.add_alias('col_d', 'T.val_d') |
| 100 | + |
| 101 | + adf.materialize_aliases(pattern=r'col_[cd]') |
| 102 | + |
| 103 | + assert adf._join_cache_misses == 1 |
| 104 | + assert adf._join_cache_hits == 1 |
| 105 | + |
| 106 | + def test_multiple_subframes_cached_separately(self): |
| 107 | + """Each subframe should have its own cache entry.""" |
| 108 | + np.random.seed(42) |
| 109 | + main_df = pd.DataFrame({ |
| 110 | + 'idx1': np.random.randint(0, 50, 500), |
| 111 | + 'idx2': np.random.randint(0, 50, 500), |
| 112 | + 'x': np.random.randn(500) |
| 113 | + }) |
| 114 | + |
| 115 | + sub1_df = pd.DataFrame({ |
| 116 | + 'idx1': np.arange(50), |
| 117 | + 'val1': np.random.randn(50) |
| 118 | + }) |
| 119 | + |
| 120 | + sub2_df = pd.DataFrame({ |
| 121 | + 'idx2': np.arange(50), |
| 122 | + 'val2': np.random.randn(50) |
| 123 | + }) |
| 124 | + |
| 125 | + adf = AliasDataFrame(main_df) |
| 126 | + adf.register_subframe('S1', AliasDataFrame(sub1_df), index_columns='idx1') |
| 127 | + adf.register_subframe('S2', AliasDataFrame(sub2_df), index_columns='idx2') |
| 128 | + |
| 129 | + adf.add_alias('from_s1', 'S1.val1') |
| 130 | + adf.add_alias('from_s2', 'S2.val2') |
| 131 | + |
| 132 | + adf._join_cache_hits = 0 |
| 133 | + adf._join_cache_misses = 0 |
| 134 | + |
| 135 | + adf.materialize_aliases() |
| 136 | + |
| 137 | + assert adf._join_cache_misses == 2 |
| 138 | + assert adf._join_cache_hits == 0 |
| 139 | + |
| 140 | + def test_cache_produces_correct_values(self, setup_with_subframe): |
| 141 | + """Cached and non-cached paths should produce identical results.""" |
| 142 | + adf = setup_with_subframe |
| 143 | + |
| 144 | + sub_adf = adf.get_subframe('T') |
| 145 | + expected = adf.df.merge( |
| 146 | + sub_adf.df[['idx', 'val_a', 'val_b']], |
| 147 | + on='idx', |
| 148 | + how='left' |
| 149 | + ) |
| 150 | + |
| 151 | + adf.add_alias('col_a', 'T.val_a') |
| 152 | + adf.add_alias('col_b', 'T.val_b') |
| 153 | + adf.materialize_aliases(pattern=r'col_.*') |
| 154 | + |
| 155 | + np.testing.assert_array_almost_equal( |
| 156 | + adf.df['col_a'].values, |
| 157 | + expected['val_a'].values, |
| 158 | + err_msg="Cached col_a values don't match expected" |
| 159 | + ) |
| 160 | + np.testing.assert_array_almost_equal( |
| 161 | + adf.df['col_b'].values, |
| 162 | + expected['val_b'].values, |
| 163 | + err_msg="Cached col_b values don't match expected" |
| 164 | + ) |
| 165 | + |
| 166 | + def test_cache_handles_missing_keys(self): |
| 167 | + """Cache should correctly handle missing keys with fill config.""" |
| 168 | + main_df = pd.DataFrame({ |
| 169 | + 'idx': [0, 1, 2, 999, 998], |
| 170 | + 'x': [1.0, 2.0, 3.0, 4.0, 5.0] |
| 171 | + }) |
| 172 | + |
| 173 | + sub_df = pd.DataFrame({ |
| 174 | + 'idx': [0, 1, 2], |
| 175 | + 'val_a': [10.0, 20.0, 30.0], |
| 176 | + 'val_b': [100.0, 200.0, 300.0] |
| 177 | + }) |
| 178 | + |
| 179 | + adf = AliasDataFrame(main_df) |
| 180 | + adf.register_subframe('T', AliasDataFrame(sub_df), index_columns='idx') |
| 181 | + adf.set_subframe_fill('T', fill_missing=-999.0) |
| 182 | + |
| 183 | + adf.add_alias('col_a', 'T.val_a') |
| 184 | + adf.add_alias('col_b', 'T.val_b') |
| 185 | + adf.materialize_aliases(pattern=r'col_.*') |
| 186 | + |
| 187 | + assert adf.df['col_a'].iloc[3] == -999.0 |
| 188 | + assert adf.df['col_a'].iloc[4] == -999.0 |
| 189 | + assert adf.df['col_b'].iloc[3] == -999.0 |
| 190 | + assert adf.df['col_b'].iloc[4] == -999.0 |
| 191 | + |
| 192 | + assert adf.df['col_a'].iloc[0] == 10.0 |
| 193 | + assert adf.df['col_b'].iloc[0] == 100.0 |
| 194 | + |
| 195 | + def test_five_column_batch_cache_stats(self, setup_with_subframe): |
| 196 | + """Materializing 5 columns from one subframe should show 1 miss, 4 hits.""" |
| 197 | + adf = setup_with_subframe |
| 198 | + |
| 199 | + adf.add_alias('col_a', 'T.val_a') |
| 200 | + adf.add_alias('col_b', 'T.val_b') |
| 201 | + adf.add_alias('col_c', 'T.val_c') |
| 202 | + adf.add_alias('col_d', 'T.val_d') |
| 203 | + adf.add_alias('col_e', 'T.val_e') |
| 204 | + |
| 205 | + adf._join_cache_hits = 0 |
| 206 | + adf._join_cache_misses = 0 |
| 207 | + |
| 208 | + adf.materialize_aliases(pattern=r'col_.*') |
| 209 | + |
| 210 | + assert adf._join_cache_misses == 1, f"Expected 1 miss for 5 columns, got {adf._join_cache_misses}" |
| 211 | + assert adf._join_cache_hits == 4, f"Expected 4 hits for 5 columns, got {adf._join_cache_hits}" |
| 212 | + |
| 213 | + assert adf._join_cache_misses + adf._join_cache_hits == 5 |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == '__main__': |
| 217 | + pytest.main([__file__, '-v']) |
0 commit comments