def test_clear_cache_all(df): @orca.table(cache=True) def table(): return df @orca.column('table', cache=True) def z(table): return df.a @orca.injectable(cache=True) def x(): return 'x' @orca.injectable(autocall=False, memoize=True) def y(s): return s + 'y' orca.eval_variable('table.z') orca.eval_variable('x') orca.get_injectable('y')('x') assert list(orca._TABLE_CACHE.keys()) == ['table'] assert list(orca._COLUMN_CACHE.keys()) == [('table', 'z')] assert list(orca._INJECTABLE_CACHE.keys()) == ['x'] assert orca._MEMOIZED['y'].value.cache == {(('x',), None): 'xy'} orca.clear_cache() assert orca._TABLE_CACHE == {} assert orca._COLUMN_CACHE == {} assert orca._INJECTABLE_CACHE == {} assert orca._MEMOIZED['y'].value.cache == {}
def test_column_cache(df): orca.add_injectable('x', 2) series = pd.Series([1, 2, 3], index=['x', 'y', 'z']) key = ('table', 'col') @orca.table() def table(): return df @orca.column(*key, cache=True) def column(variable='x'): return series * variable def c(): return orca._COLUMNS[key] pdt.assert_series_equal(c()(), series * 2) orca.add_injectable('x', 3) pdt.assert_series_equal(c()(), series * 2) c().clear_cached() pdt.assert_series_equal(c()(), series * 3) orca.add_injectable('x', 4) pdt.assert_series_equal(c()(), series * 3) orca.clear_cache() pdt.assert_series_equal(c()(), series * 4) orca.add_injectable('x', 5) pdt.assert_series_equal(c()(), series * 4) orca.get_table('table').clear_cached() pdt.assert_series_equal(c()(), series * 5) orca.add_injectable('x', 6) pdt.assert_series_equal(c()(), series * 5) orca.add_column(*key, column=column, cache=True) pdt.assert_series_equal(c()(), series * 6)
def test_table_func_cache(df): orca.add_injectable('x', 2) @orca.table(cache=True) def table(variable='x'): return df * variable pdt.assert_frame_equal(orca.get_table('table').to_frame(), df * 2) orca.add_injectable('x', 3) pdt.assert_frame_equal(orca.get_table('table').to_frame(), df * 2) orca.get_table('table').clear_cached() pdt.assert_frame_equal(orca.get_table('table').to_frame(), df * 3) orca.add_injectable('x', 4) pdt.assert_frame_equal(orca.get_table('table').to_frame(), df * 3) orca.clear_cache() pdt.assert_frame_equal(orca.get_table('table').to_frame(), df * 4) orca.add_injectable('x', 5) pdt.assert_frame_equal(orca.get_table('table').to_frame(), df * 4) orca.add_table('table', table) pdt.assert_frame_equal(orca.get_table('table').to_frame(), df * 5)
def test_injectables_cache(): x = 2 @orca.injectable(autocall=True, cache=True) def inj(): return x * x def i(): return orca._INJECTABLES['inj'] assert i()() == 4 x = 3 assert i()() == 4 i().clear_cached() assert i()() == 9 x = 4 assert i()() == 9 orca.clear_cache() assert i()() == 16 x = 5 assert i()() == 16 orca.add_injectable('inj', inj, autocall=True, cache=True) assert i()() == 25