Example #1
0
    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, {})
Example #2
0
    def test_invalidate_and_rebuild(self):
        cache_name = self.check_cache_gone('invalidate_rebuild')
        cache = PersistentCache(cache_name, cache_manager=self.manager, contents={ 'foo': 'bar' })
        cache.save()

        cache.invalidate_and_rebuild()
        self.assert_contents_equal(cache, {})

        cache.load() # Saved content should get replaced
        self.assert_contents_equal(cache, {})
Example #3
0
    def test_persistent_cache_wrap(self):
        cache_name = self.check_cache_gone('persistent')

        cache = PersistentCache(cache_name, cache_manager=self.manager, contents={})
        cache['foo'] = 'bar'
        cache.save()

        self.check_cache(cache_name, True)
        cache.load() # Reload
        self.check_cache(cache_name, True)
        self.assertEqual(cache['foo'], 'bar')
Example #4
0
    def test_post_processor(self):
        cache_name = self.check_cache_gone('post_process')
        cache = PersistentCache(cache_name, cache_manager=self.manager, contents={ 'foo': 'bar' },
            post_processor=lambda c: { 'foo2': c.get('foo', 'missing') })
        self.assert_contents_equal(cache, { 'foo': 'bar' })
        cache.save()
        # Postprocessor should not have applied to save or cache
        self.assert_contents_equal(cache, { 'foo': 'bar' })

        cache.load() # Load and apply postprocessor changes
        self.assert_contents_equal(cache, { 'foo2': 'bar' })
Example #5
0
    def test_save_and_load(self):
        cache_name = self.check_cache_gone('save_load')
        cache = PersistentCache(cache_name, cache_manager=self.manager)
        cache['foo'] = 'bar'
        cache.save()

        cache.contents = {}
        self.assert_contents_equal(cache, {})

        cache.load()
        self.assertEqual(cache['foo'], 'bar')
Example #6
0
    def test_pre_processor(self):
        cache_name = self.check_cache_gone('validation')
        cache = PersistentCache(cache_name, cache_manager=self.manager, contents={ 'foo': 'bar' },
            pre_processor=lambda c: { 'foo2': c['foo'] })
        self.assert_contents_equal(cache, { 'foo': 'bar' })
        cache.save()
        # Preprocessor should have applied to save, but not cache
        self.assert_contents_equal(cache, { 'foo': 'bar' })

        cache.load() # Load the preprocessor changes
        self.assert_contents_equal(cache, { 'foo2': 'bar' })
Example #7
0
    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, {})
Example #8
0
    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)
Example #9
0
    def test_content_driven_cache_wrap(self):
        cache_name = self.check_cache_gone('content')

        # Pass a non-empty list as content
        cache = PersistentCache(cache_name, cache_manager=self.manager, contents=[''],
            builder=lambda *args: [])
        cache[0] = 'foo'
        cache.append('bar')
        cache.save()

        cache.load()
        self.assertTrue(isinstance(cache, PersistentCache))
        self.assertTrue(isinstance(cache.contents, list))
        self.check_cache(cache_name, True)
        self.assertEqual(cache[0], 'foo')
        self.assertEqual(cache[1], 'bar')

        cache.invalidate_and_rebuild()
        self.assertTrue(isinstance(cache, PersistentCache))
        self.assertTrue(isinstance(cache.contents, list))
        self.assert_contents_equal(cache, [])