Back to index

plone3  3.1.7
test_caching.py
Go to the documentation of this file.
00001 """
00002 """
00003 
00004 import unittest
00005 
00006 from Products.CMFCore.utils import getToolByName
00007 from PlonePASTestCase import PlonePASTestCase
00008 
00009 from Products.PluggableAuthService.interfaces.authservice \
00010      import IPluggableAuthService
00011 
00012 class IntrospectorMethodWrapper:
00013     count = 0
00014 
00015     def __init__(self, klass, name):
00016         self.klass = klass
00017         self.name = name
00018         self.origname = "__introspected_%s__" % name
00019         method = self.genWrapper()
00020         old_method = getattr(klass, name)
00021         setattr(klass, self.origname, old_method)
00022         setattr(klass, name, method)
00023 
00024     def _getOriginalMethod(self, instance):
00025         return getattr(instance, self.origname)
00026 
00027     def genWrapper(self):
00028         introspector = self
00029         def wrapper(self, *args, **kw):
00030             result = introspector._getOriginalMethod(self)(*args, **kw)
00031             return introspector.introspect(self, args, kw, result)
00032         return wrapper
00033 
00034     def introspect(self, instance, args, kw, result):
00035         return result
00036 
00037     def __del__(self):
00038         setattr(self.klass, self.name, self.klass.__counted_method__)
00039         del self.klass.__counted_method__
00040 
00041 class CollectResultsWrapper(IntrospectorMethodWrapper):
00042 
00043     def __init__(self, klass, name):
00044         IntrospectorMethodWrapper.__init__(self, klass, name)
00045         self.results = []
00046 
00047     def introspect(self, instance, args, kw, result):
00048         #self.results.append((args, kw, result))
00049         self.results.append(result)
00050         return result
00051 
00052 class BasicOpsTestCase(PlonePASTestCase):
00053 
00054     def afterSetUp(self):
00055         self.loginAsPortalOwner()
00056         self.acl_users = self.portal.acl_users
00057         self.acl_users.ZCacheable_setManagerId('RAMCache')
00058 
00059     def createUser(self, name="created_user", password="secret",
00060                    roles=[], groups=[], domains=()):
00061         self.acl_users.userFolderAddUser(
00062             name,
00063             password,
00064             roles = roles,
00065             groups = groups,
00066             domains = domains,
00067             )
00068 
00069     def test_getUser_is_cached(self):
00070         # monitor the caching method
00071         collector = CollectResultsWrapper(self.acl_users.aq_base.__class__,
00072                                           'ZCacheable_get')
00073 
00074         self.assertEquals(collector.results, [])
00075         # creating a user already populates the cache, but it also looks
00076         # the user up, so we must have got only misses so far
00077         self.createUser()
00078         self.assertEquals(collector.results, [None] * len(collector.results))
00079         # creating a user does not necessarily insert a cache entry,
00080         # so retrieve the user twice to test caching.
00081         u = self.acl_users.getUser("created_user")
00082         u = self.acl_users.getUser("created_user")
00083         self.assertEquals(id(collector.results[-1]), id(u.aq_base),
00084                           "%r is not %r" % (collector.results[-1],
00085                                             u.aq_base))
00086 
00087 def test_suite():
00088     suite = unittest.TestSuite()
00089     suite.addTest(unittest.makeSuite(BasicOpsTestCase))
00090     return suite
00091