Skip to content

Commit

Permalink
Add cache tests for UUIDIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
andbag committed May 20, 2016
1 parent c07b529 commit 98f5254
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions src/Products/PluginIndexes/UUIDIndex/tests.py
Expand Up @@ -13,7 +13,8 @@

import unittest

from Products.PluginIndexes.UUIDIndex.UUIDIndex import UUIDIndex
from OFS.SimpleItem import SimpleItem
from Testing.makerequest import makerequest


class Dummy:
Expand All @@ -32,8 +33,17 @@ def __str__(self):

class UUIDIndexTests(unittest.TestCase):

def _getTargetClass(self):
from Products.PluginIndexes.UUIDIndex.UUIDIndex \
import UUIDIndex
return UUIDIndex

def _makeOne(self, id):
klass = self._getTargetClass()
return klass(id)

def setUp(self):
self._index = UUIDIndex('foo')
self._index = self._makeOne('foo')
self._marker = []
self._values = [
(0, Dummy('a')),
Expand Down Expand Up @@ -68,11 +78,15 @@ def test_interfaces(self):
from Products.PluginIndexes.interfaces import IPluggableIndex
from Products.PluginIndexes.interfaces import ISortIndex
from Products.PluginIndexes.interfaces import IUniqueValueIndex
from Products.PluginIndexes.interfaces import IRequestCacheIndex
from zope.interface.verify import verifyClass

verifyClass(IPluggableIndex, UUIDIndex)
verifyClass(ISortIndex, UUIDIndex)
verifyClass(IUniqueValueIndex, UUIDIndex)
klass = self._getTargetClass()

verifyClass(IPluggableIndex, klass)
verifyClass(ISortIndex, klass)
verifyClass(IUniqueValueIndex, klass)
verifyClass(IRequestCacheIndex, klass)

def test_empty(self):
self.assertEqual(len(self._index), 0)
Expand All @@ -88,7 +102,7 @@ def test_populated(self):
self.assertTrue(self._index.getEntryForObject(10) is None)
self._checkApply({'foo': 'not'}, [])

self._index.unindex_object(10) # nothrow
self._index.unindex_object(10) # nothrow

for k, v in values:
self.assertEqual(self._index.getEntryForObject(k), v.foo())
Expand Down Expand Up @@ -163,3 +177,51 @@ def test_getCounter(self):

index.clear()
self.assertEqual(index.getCounter(), 0)


class UUIDIndexTestsCache(UUIDIndexTests):

def _dummy_test(self):
# dummy function
pass

# not required for cache test
test_interfaces = _dummy_test
test_getCounter = _dummy_test

def _makeOne(self, id, extra=None):

index = super(UUIDIndexTestsCache, self).\
_makeOne(id)

class DummyZCatalog(SimpleItem):
id = 'DummyZCatalog'

# Build pseudo catalog and REQUEST environment
catalog = makerequest(DummyZCatalog())
indexes = SimpleItem()

indexes = indexes.__of__(catalog)
index = index.__of__(indexes)

return index

def _checkApply(self, req, expectedValues):

checkApply = super(UUIDIndexTestsCache, self)._checkApply
index = self._index

cache = index.getRequestCache()
cache.clear()

# first call
checkApply(req, expectedValues)
self.assertEqual(cache._hits, 0)
self.assertEqual(cache._sets, 1)
self.assertEqual(cache._misses, 1)

# second call
checkApply(req, expectedValues)
self.assertEqual(cache._hits, 1)


0 comments on commit 98f5254

Please sign in to comment.