[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Caching/tests - testAnnotationCacheable.py:1.4

Albertas Agejevas alga@codeworks.lt
Wed, 9 Oct 2002 09:08:45 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Caching/tests
In directory cvs.zope.org:/tmp/cvs-serv4391/tests

Modified Files:
	testAnnotationCacheable.py 
Log Message:
Views for ICacheable

=== Zope3/lib/python/Zope/App/Caching/tests/testAnnotationCacheable.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/App/Caching/tests/testAnnotationCacheable.py:1.3	Fri Oct  4 14:37:12 2002
+++ Zope3/lib/python/Zope/App/Caching/tests/testAnnotationCacheable.py	Wed Oct  9 09:08:44 2002
@@ -23,10 +23,31 @@
 from Zope.App.OFS.Annotation.IAttributeAnnotatable import IAttributeAnnotatable
 from Zope.App.OFS.Annotation.AttributeAnnotations import AttributeAnnotations
 from Zope.App.Caching.AnnotationCacheable import AnnotationCacheable
+from Zope.App.Caching.ICachingService import ICachingService
+from Zope.ComponentArchitecture.GlobalServiceManager import \
+     serviceManager as sm
 
 class ObjectStub:
     __implements__ = IAttributeAnnotatable
 
+class CacheStub:
+
+    def __init__(self):
+        self.invalidated = []
+
+    def invalidate(self, obj):
+        self.invalidated.append(obj)
+
+class CachingServiceStub:
+
+    __implements__ = ICachingService
+
+    def __init__(self):
+        self.caches = {}
+
+    def getCache(self, name):
+        return self.caches[name]
+
 class TestAnnotationCacheable(PlacelessSetup, TestCase):
 
     def setUp(self):
@@ -34,6 +55,9 @@
         getService(None, "Adapters").provideAdapter(
             IAttributeAnnotatable, IAnnotations,
             AttributeAnnotations)
+        self.service = CachingServiceStub()
+        sm.defineService('Caching', ICachingService)
+        sm.provideService('Caching', self.service)
 
     def testNormal(self):
         ob = ObjectStub()
@@ -45,6 +69,21 @@
         self.assertEquals(adapter.getCacheId(), "my_id",
                           "failed to set cache ID")
 
+    def testInvalidate(self):
+        """Test that setting a different cache ID invalidates the old
+        cached value"""
+        self.service.caches['cache1'] = cache1 = CacheStub()
+        self.service.caches['cache2'] = cache2 = CacheStub()
+        ob = ObjectStub()
+        adapter = AnnotationCacheable(ob)
+        adapter.setCacheId('cache1')
+        self.assertEquals(cache1.invalidated, [], "called invalidate too early")
+        adapter.setCacheId('cache2')
+        self.assertEquals(cache1.invalidated, [ob], "did not call invalidate")
+        adapter.setCacheId('cache2')
+        self.assertEquals(cache2.invalidated, [],
+                          "called invalidate when reassigning to the same cache")
+                
 
 def test_suite():
     return TestSuite((