def test_cache_clear(self, CacheStats): store = MagicMock() c = Cache(store=MagicMock(return_value=store)) c.clear() store.clear.assert_called_once_with() CacheStats.return_value.clear.assert_called_once_with()
def test_cache_delete(self, CacheStats): store = MagicMock() c = Cache(store=MagicMock(return_value=store)) c.delete('foo') store.__delitem__.assert_called_once_with('foo') CacheStats.return_value.delete.assert_called_once_with()
def test_create_get_cache(self, Cache): m = CacheManager() m.create_cache('region', foo=1, bar=2, baz=3) Cache.assert_called_once_with(foo=1, bar=2, baz=3) c = m.get_cache('region') self.assertEqual(c, Cache.return_value)
def test_cache_get_missing_key(self, CacheStats): store = MagicMock() store.__getitem__.return_value = None c = Cache(store=MagicMock(return_value=store)) foo = c.get('foo') CacheStats.return_value.miss.assert_called_once_with() self.assertIsNone(foo)
def test_cache_get_expired_key(self, CacheStats): store = MagicMock() store.__getitem__.return_value.is_expired.return_value = True c = Cache(store=MagicMock(return_value=store)) foo = c.get('foo') store.__delitem__.assert_called_once_with('foo') CacheStats.return_value.miss.assert_called_once_with(expired=True) self.assertIsNone(foo)
def test_cache_put(self, CacheEntry, CacheStats): store = MagicMock() c = Cache(store=MagicMock(return_value=store)) c.put('foo', 'Value Of Foo', new=True) CacheEntry.assert_called_once_with('Value Of Foo') store.__setitem__.assert_called_once_with('foo', CacheEntry.return_value) CacheStats.return_value.put.assert_called_once_with(new=True)
def test_cache_get_existing_key(self, CacheStats): store = MagicMock() store.__getitem__.return_value.is_expired.return_value = False c = Cache(store=MagicMock(return_value=store)) foo = c.get('foo') store.__getitem__.assert_called_once_with('foo') CacheStats.return_value.hit.assert_called_once_with() store.__getitem__.return_value.touch.assert_called_once_with() self.assertEqual(foo, store.__getitem__.return_value.value)
def test_cache_size(self, CacheStats): store = MagicMock() c = Cache(store=MagicMock(return_value=store)) s = c.size store.__len__.assert_called_once_with() self.assertEqual(s, store.__len__.return_value)
def test_cache_init_creates_store_and_stats(self, CacheStats): store = MagicMock() c = Cache(store=store, ttl=100, tti=10, foo=1, bar=2, baz=3) CacheStats.assert_called_once_with() store.assert_called_once_with(foo=1, bar=2, baz=3) self.assertEqual(c.store, store.return_value) self.assertEqual(c.stats, CacheStats.return_value)
def test_cache_max_entries(self, CacheStats): cache = Cache(store=MemoryStore, store_opts={'max_entries': 2}) for i in range(1,10): cache.put(i, i) self.assertEqual(2, len(cache.store)) self.assertEqual(list(cache.store.store.keys()), [8,9])