def test_dependents(self): dependent_cache_name = self.check_cache_gone('dependent') dependent_cache = PersistentCache(dependent_cache_name, cache_manager=self.manager) parent_cache_name = self.check_cache_gone('parent') parent_cache = PersistentCache(parent_cache_name, cache_manager=self.manager, dependents=[dependent_cache]) dependent_cache['foo'] = 'bar' parent_cache.save(True) dependent_cache['foo'] = 'saved' parent_cache.load(True) self.assertEqual(dependent_cache['foo'], 'bar') dependent_cache['foo'] = 'invalid' parent_cache.invalidate(True) self.assertEqual(dependent_cache['foo'], 'bar') parent_cache.delete_saved_content(True) parent_cache.load(True) self.assertIsNone(dependent_cache.contents) parent_cache.load_or_build(True) self.assertDictEqual(dependent_cache.contents, {}) dependent_cache['foo'] = 'bar' dependent_cache.save() parent_cache.invalidate_and_rebuild(True) self.assertDictEqual(dependent_cache.contents, {})
def test_delete_saved(self): cache_name = self.check_cache_gone('delete_saved') cache = PersistentCache(cache_name, cache_manager=self.manager, contents={ 'foo': 'bar' }) cache.delete_saved_content() self.check_cache_gone(cache_name) self.assertEqual(cache['foo'], 'bar') # Shouldn't delete memory cache.load() self.assertIsNone(cache.contents) # No content to load cache.load_or_build() self.assert_contents_equal(cache, {})
def test_validation(self): cache_name = self.check_cache_gone('validation') cache = PersistentCache(cache_name, cache_manager=self.manager, contents={ 'foo': 'bar' }, validator=lambda *args: False, builder=lambda *args: ['built']) cache.save() cache.load_or_build() # Invalid load, force rebuild self.assert_contents_equal(cache, ['built']) cache[0] = 'changed' cache.save() cache.validator = lambda *args: True cache.load() self.assert_contents_equal(cache, ['changed']) # Raising an exception in validator should invalidate the cache cache.validator = lambda *args: args['not legal'] cache.load() self.assert_contents_equal(cache, None)