Tag Archives: plone.memoize

Testing memoized methods

For a recent Plone project we needed to write unit tests for methods that were using plone.memoize for caching return values.

With a standard Plone’s PloneTestCase I got the following error:

Error in test test_main_image (niteoweb.elcond.tests.test_stol.TestContent)
Traceback (most recent call last):
 File "/Users/zupo/work/python/parts/opt/lib/python2.6/unittest.py", line 279, in run
 File "/Users/zupo/work/niteoweb.elcond/src/niteoweb/elcond/tests/test_stol.py", line 75, in test_main_image
 image = self.portal.stol.main_image()
 File "/Users/zupo/work/niteoweb.elcond/src/niteoweb/elcond/content/stol.py", line 91, in main_image
 images = self.images(sort_limit=1)
 File "/Users/zupo/.buildout/eggs/plone.memoize-1.1-py2.6.egg/plone/memoize/view.py", line 21, in memogetter
 annotations = IAnnotations(request)
TypeError: ('Could not adapt', None, <InterfaceClass zope.annotation.interfaces.IAnnotations>)

The cause of the problem is that TestRequest (used by PloneTestCase) does not allow IAnnotations adapter to store data in an attribute named __annotations__. The solution is to add the lines below to afterSetUp() method of your TestCase.

from zope.annotation.interfaces import IAttributeAnnotatable
from zope.interface import directlyProvides
from zope.publisher.browser import TestRequest

request = TestRequest()
directlyProvides(request, IAttributeAnnotatable)