class MakoCacheImplTestCase(unittest.TestCase): """ Test the ``MakoCacheImpl``. """ def setUp(self): from wheezy.web.templates import MakoCacheImpl self.mock_cache = Mock() mock_mako_cache = Mock() mock_mako_cache.template.cache_args = { 'cache': self.mock_cache } mock_mako_cache.id = 'prefix-' self.cache = MakoCacheImpl(mock_mako_cache) def test_init(self): """ __init__. """ assert 'prefix-' == self.cache.prefix assert self.mock_cache == self.cache.cache def test_not_implemented(self): """ set, get and invalidate raise error. """ self.assertRaises(NotImplementedError, lambda: self.cache.set('key', 1)) self.assertRaises(NotImplementedError, lambda: self.cache.get('key')) self.assertRaises(NotImplementedError, lambda: self.cache.invalidate('key')) def test_get_or_create_missing_in_cache(self): """ Requested item is missing in cache. """ self.mock_cache.get.return_value = None mock_creation_function = Mock(return_value='html') assert 'html' == self.cache.get_or_create( 'key', mock_creation_function, namespace='namespace', time='100') self.mock_cache.get.assert_called_once_with( 'prefix-key', 'namespace') self.mock_cache.add.assert_called_once_with( 'prefix-key', 'html', 100, 'namespace') def test_get_or_create_found_in_cache(self): """ Requested item found in cache. """ self.mock_cache.get.return_value = 'html' mock_creation_function = Mock() assert 'html' == self.cache.get_or_create( 'key', mock_creation_function, namespace='namespace', time='100') self.mock_cache.get.assert_called_once_with( 'prefix-key', 'namespace') assert not mock_creation_function.called
def setUp(self): from wheezy.web.templates import MakoCacheImpl self.mock_cache = Mock() mock_mako_cache = Mock() mock_mako_cache.template.cache_args = { 'cache': self.mock_cache } mock_mako_cache.id = 'prefix-' self.cache = MakoCacheImpl(mock_mako_cache)