Skip to content

Commit f2e57ee

Browse files
committed
Add more caching tests
1 parent 751aaee commit f2e57ee

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

src/Products/ZCatalog/cache.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,18 @@ def decorator(*args, **kwargs):
126126

127127

128128
# Make sure we provide test isolation, works only for ramcache
129-
def _cache_clear():
129+
def _get_cache():
130130
cache_adapter = ram.store_in_cache(_apply_query_plan_cachekey)
131131
if hasattr(cache_adapter, 'ramcache'):
132-
cache_adapter.ramcache.invalidateAll()
132+
return cache_adapter.ramcache
133133
else:
134134
raise AttributeError('Only ramcache supported for testing')
135135

136+
137+
def _cache_clear():
138+
ram_cache = _get_cache()
139+
ram_cache.invalidateAll()
140+
136141
from zope.testing.cleanup import addCleanUp # NOQA
137142
addCleanUp(_cache_clear)
138143
del addCleanUp

src/Products/ZCatalog/tests/test_cache.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from Products.ZCatalog.Catalog import Catalog
2626
from Products.ZCatalog.ZCatalog import ZCatalog
2727
from Products.ZCatalog.cache import CatalogCacheKey
28+
from Products.ZCatalog.cache import _get_cache
2829

2930

3031
class Dummy(object):
@@ -88,15 +89,82 @@ def setUp(self):
8889

8990
self.zcat = zcat
9091

92+
def _apply_query(self, query):
93+
cache = _get_cache()
94+
95+
res1 = self.zcat.search(query)
96+
stats = cache.getStatistics()
97+
98+
hits = stats[0]['hits']
99+
misses = stats[0]['misses']
100+
101+
res2 = self.zcat.search(query)
102+
stats = cache.getStatistics()
103+
104+
# check if chache hits
105+
self.assertEqual(stats[0]['hits'], hits + 1)
106+
self.assertEqual(stats[0]['misses'], misses)
107+
108+
# compare result
109+
rset1 = map(lambda x: x.getRID(), res1)
110+
rset2 = map(lambda x: x.getRID(), res2)
111+
self.assertEqual(rset1, rset2)
112+
91113
def _get_cache_key(self, query=None):
92114
catalog = self.zcat._catalog
115+
query = catalog.make_query(query)
93116
return CatalogCacheKey(catalog, query=query).key
94117

95118
def test_make_key(self):
96119
query = {'big': True}
97-
key = (('catalog',), frozenset([('big', self.length, (True,))]))
120+
key = (('catalog',),
121+
frozenset([('big', self.length, (True,))]))
98122
self.assertEquals(self._get_cache_key(query), key)
99123

100-
#class TestCatalogCaching(unittest.TestCase):
101-
#
102-
# def test_caching
124+
query = {'start': '2013-07-01'}
125+
key = (('catalog',),
126+
frozenset([('start', self.length, ('2013-07-01',))]))
127+
self.assertEquals(self._get_cache_key(query), key)
128+
129+
query = {'path': '/1', 'date': '2013-07-05', 'numbers': [1, 3]}
130+
key = (('catalog',),
131+
frozenset([('date', 9, ('2013-07-05',)),
132+
('numbers', 9, (1, 3)),
133+
('path', 9, ('/1',))]))
134+
self.assertEquals(self._get_cache_key(query), key)
135+
136+
def test_cache(self):
137+
query = {'big': True}
138+
self._apply_query(query)
139+
140+
query = {'start': '2013-07-01'}
141+
self._apply_query(query)
142+
143+
query = {'path': '/1', 'date': '2013-07-05', 'numbers': [1, 3]}
144+
self._apply_query(query)
145+
146+
def test_cache_invalidate(self):
147+
cache = _get_cache()
148+
query = {'big': False}
149+
150+
res1 = self.zcat.search(query)
151+
stats = cache.getStatistics()
152+
153+
hits = stats[0]['hits']
154+
misses = stats[0]['misses']
155+
156+
# catalog new object
157+
obj = Dummy(20)
158+
self.zcat.catalog_object(obj, str(20))
159+
160+
res2 = self.zcat.search(query)
161+
stats = cache.getStatistics()
162+
163+
# check if chache misses
164+
self.assertEqual(stats[0]['hits'], hits)
165+
self.assertEqual(stats[0]['misses'], misses + 1)
166+
167+
# compare result
168+
rset1 = map(lambda x: x.getRID(), res1)
169+
rset2 = map(lambda x: x.getRID(), res2)
170+
self.assertEqual(rset1, rset2)

src/Products/ZCatalog/tests/test_zcatalog.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
##############################################################################
1313

1414
import unittest
15+
from zope.testing import cleanup
1516

1617
from AccessControl.SecurityManagement import setSecurityManager
1718
from AccessControl.SecurityManagement import noSecurityManager
@@ -98,17 +99,18 @@ def validate(self, accessed, container, name, value):
9899
raise Unauthorized(name)
99100

100101

101-
class ZCatalogBase(object):
102+
class ZCatalogBase(cleanup.CleanUp):
102103

103104
def _makeOne(self):
104105
from Products.ZCatalog.ZCatalog import ZCatalog
105-
return ZCatalog('Catalog-%s' % id(self))
106+
return ZCatalog('Catalog')
106107

107108
def _makeOneIndex(self, name):
108109
from Products.PluginIndexes.FieldIndex.FieldIndex import FieldIndex
109110
return FieldIndex(name)
110111

111112
def setUp(self):
113+
cleanup.CleanUp.setUp(self)
112114
self._catalog = self._makeOne()
113115

114116
def tearDown(self):

0 commit comments

Comments
 (0)