Skip to content

Commit

Permalink
Add request cache tests for FieldIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
andbag committed May 19, 2016
1 parent 1986237 commit 3fc3498
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions src/Products/PluginIndexes/FieldIndex/tests.py
Expand Up @@ -15,8 +15,8 @@

import unittest

from Products.PluginIndexes.FieldIndex.FieldIndex import FieldIndex

from OFS.SimpleItem import SimpleItem
from Testing.makerequest import makerequest

class Dummy(object):

Expand All @@ -35,9 +35,17 @@ def __str__(self):
class FieldIndexTests(unittest.TestCase):
"""Test FieldIndex objects.
"""
def _getTargetClass(self):
from Products.PluginIndexes.FieldIndex.FieldIndex \
import FieldIndex
return FieldIndex

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

def setUp(self):
self._index = FieldIndex('foo')
self._index = self._makeOne('foo')
self._marker = []
self._values = [(0, Dummy('a')),
(1, Dummy('ab')),
Expand Down Expand Up @@ -101,9 +109,9 @@ def test_interfaces(self):
from Products.PluginIndexes.interfaces import IUniqueValueIndex
from zope.interface.verify import verifyClass

verifyClass(IPluggableIndex, FieldIndex)
verifyClass(ISortIndex, FieldIndex)
verifyClass(IUniqueValueIndex, FieldIndex)
verifyClass(IPluggableIndex, self._getTargetClass())
verifyClass(ISortIndex, self._getTargetClass())
verifyClass(IUniqueValueIndex, self._getTargetClass())

def testEmpty(self):
"Test an empty FieldIndex."
Expand Down Expand Up @@ -218,7 +226,7 @@ def testReindex(self):

def testRange(self):
"""Test a range search"""
index = FieldIndex('foo')
index = self._makeOne('foo')
for i in range(100):
index.index_object(i, Dummy(i % 10))

Expand All @@ -241,3 +249,41 @@ def testRange(self):
r2, ignore = index._apply_index(record)
r2 = list(r2.keys())
assert r2 == r


class FieldIndexCacheTests(FieldIndexTests):

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

index = super(FieldIndexCacheTests, self).\
_makeOne(id, extra=extra)

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(FieldIndexCacheTests, 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 3fc3498

Please sign in to comment.