[Zope3-checkins] CVS: Zope3/src/zope/app/cache/tests - __init__.py:1.2 test_annotationcacheable.py:1.2 test_cachename.py:1.2 test_caching.py:1.2 test_icache.py:1.2 test_ramcache.py:1.2

Jim Fulton jim@zope.com
Wed, 25 Dec 2002 09:13:46 -0500


Update of /cvs-repository/Zope3/src/zope/app/cache/tests
In directory cvs.zope.org:/tmp/cvs-serv15352/src/zope/app/cache/tests

Added Files:
	__init__.py test_annotationcacheable.py test_cachename.py 
	test_caching.py test_icache.py test_ramcache.py 
Log Message:
Grand renaming:

- Renamed most files (especially python modules) to lower case.

- Moved views and interfaces into separate hierarchies within each
  project, where each top-level directory under the zope package
  is a separate project.

- Moved everything to src from lib/python.

  lib/python will eventually go away. I need access to the cvs
  repository to make this happen, however.

There are probably some bits that are broken. All tests pass
and zope runs, but I haven't tried everything. There are a number
of cleanups I'll work on tomorrow.



=== Zope3/src/zope/app/cache/tests/__init__.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:46 2002
+++ Zope3/src/zope/app/cache/tests/__init__.py	Wed Dec 25 09:12:45 2002
@@ -0,0 +1,2 @@
+#
+# This file is necessary to make this directory a package.


=== Zope3/src/zope/app/cache/tests/test_annotationcacheable.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:46 2002
+++ Zope3/src/zope/app/cache/tests/test_annotationcacheable.py	Wed Dec 25 09:12:45 2002
@@ -0,0 +1,97 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Unit test for AnnotationCacheable adapter.
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from zope.app.tests.placelesssetup import PlacelessSetup
+from zope.component import getService
+from zope.app.interfaces.annotation import IAnnotations
+from zope.app.interfaces.annotation import IAttributeAnnotatable
+from zope.app.attributeannotations import AttributeAnnotations
+from zope.app.cache.annotationcacheable import AnnotationCacheable
+from zope.app.interfaces.cache.cache import ICachingService
+from zope.component.service 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):
+        PlacelessSetup.setUp(self)
+        getService(None, "Adapters").provideAdapter(
+            IAttributeAnnotatable, IAnnotations,
+            AttributeAnnotations)
+        self.service = CachingServiceStub()
+        sm.defineService('Caching', ICachingService)
+        sm.provideService('Caching', self.service)
+
+    def testNormal(self):
+        ob = ObjectStub()
+        adapter = AnnotationCacheable(ob)
+        self.assertEquals(adapter.getCacheId(), None,
+                          "initially cache ID should be None")
+
+        adapter.setCacheId("my_id")
+        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():
+    suite = TestSuite()
+    suite.addTest(makeSuite(TestAnnotationCacheable))
+    return suite
+
+
+if __name__ == '__main__':
+    main(defaultTest='test_suite')


=== Zope3/src/zope/app/cache/tests/test_cachename.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:46 2002
+++ Zope3/src/zope/app/cache/tests/test_cachename.py	Wed Dec 25 09:12:45 2002
@@ -0,0 +1,55 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Test the CacheName field
+
+In particular, test the proper getting of cache names in allowed_values.
+
+$Id$
+"""
+
+import unittest
+
+from zope.proxy.context import ContextWrapper
+
+from zope.app.interfaces.cache.cache import CacheName
+from zope.app.services.tests.placefulsetup import PlacefulSetup
+from zope.app.services.tests.servicemanager import TestingServiceManager
+
+class CachingServiceStub(object):
+
+    def getAvailableCaches(self):
+        return 'foo', 'bar', 'baz'
+
+
+class CacheNameTest(PlacefulSetup, unittest.TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        self.buildFolders()
+        sm = TestingServiceManager()
+        self.rootFolder.setServiceManager(sm)
+        sm.Caching = CachingServiceStub()
+
+    def test(self):
+        field = CacheName().bind(self.rootFolder)
+        allowed = list(field.allowed_values)
+        allowed.sort()
+        self.assertEqual(allowed, ['', 'bar', 'baz', 'foo'])
+
+
+def test_suite():
+    return unittest.makeSuite(CacheNameTest)
+
+if __name__=='__main__':
+    unittest.main(defaultTest='test_suite')


=== Zope3/src/zope/app/cache/tests/test_caching.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:46 2002
+++ Zope3/src/zope/app/cache/tests/test_caching.py	Wed Dec 25 09:12:45 2002
@@ -0,0 +1,81 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Unit tests for caching helpers.
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from zope.app.interfaces.cache.cache import ICacheable
+from zope.app.interfaces.cache.cache import ICachingService
+from zope.app.cache.caching import getCacheForObj
+from zope.app.cache.annotationcacheable import AnnotationCacheable
+from zope.app.interfaces.annotation import IAnnotatable
+from zope.app.interfaces.annotation import IAnnotations
+from zope.app.interfaces.annotation import IAttributeAnnotatable
+from zope.app.attributeannotations import AttributeAnnotations
+from zope.component import getAdapter
+from zope.component import getService
+from zope.app.tests.placelesssetup import PlacelessSetup
+from zope.component.service import \
+     serviceManager as sm
+
+class ObjectStub:
+    __implements__ = IAttributeAnnotatable
+
+class CacheStub:
+    pass
+
+class CachingServiceStub:
+
+    __implements__ = ICachingService
+
+    def __init__(self):
+        self.caches = {}
+
+    def getCache(self, name):
+        return self.caches[name]
+
+class Test(PlacelessSetup, TestCase):
+
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+        getService(None, "Adapters").provideAdapter(
+            IAttributeAnnotatable, IAnnotations,
+            AttributeAnnotations)
+        getService(None, "Adapters").provideAdapter(
+            IAnnotatable, ICacheable,
+            AnnotationCacheable)
+        self.service = CachingServiceStub()
+        sm.defineService('Caching', ICachingService)
+        sm.provideService('Caching', self.service)
+
+    def testGetCacheForObj(self):
+        self.service.caches['my_cache'] = my_cache = CacheStub()
+
+        obj = ObjectStub()
+        self.assertEquals(getCacheForObj(obj), None)
+
+        getAdapter(obj, ICacheable).setCacheId("my_cache")
+
+        self.assertEquals(getCacheForObj(obj), my_cache)
+
+
+def test_suite():
+    return TestSuite((
+        makeSuite(Test),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Zope3/src/zope/app/cache/tests/test_icache.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:46 2002
+++ Zope3/src/zope/app/cache/tests/test_icache.py	Wed Dec 25 09:12:45 2002
@@ -0,0 +1,76 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Unit tests for ICache interface
+
+$Id$
+"""
+
+from unittest import TestSuite, main
+from zope.interface.verify import verifyObject
+from zope.app.interfaces.cache.cache import ICache
+
+
+class BaseICacheTest:
+    """Base class for ICache unit tests.  Subclasses should provide a
+    _Test__new() method that returns a new empty cache object.
+    """
+
+    def testVerifyICache(self):
+        # Verify that the object implements ICache
+        verifyObject(ICache, self._Test__new())
+
+    def testCaching(self):
+        # Verify basic caching
+        cache = self._Test__new()
+        ob = "obj"
+        data = "data"
+        marker = []
+        self.failIf(cache.query(ob, None, default=marker) is not marker,
+                    "empty cache should not contain anything")
+
+        cache.set(data, ob, key={'id': 35})
+        self.assertEquals(cache.query(ob, {'id': 35}), data,
+                    "should return cached result")
+        self.failIf(cache.query(ob, {'id': 33}, default=marker) is not marker,
+                    "should not return cached result for a different key")
+
+        cache.invalidate(ob, {"id": 33})
+        self.assertEquals(cache.query(ob, {'id': 35}), data,
+                          "should return cached result")
+        self.failIf(cache.query(ob, {'id': 33}, default=marker) is not marker,
+                    "should not return cached result after invalidate")
+
+    def testInvalidateAll(self):
+        cache = self._Test__new()
+        ob1 = object()
+        ob2 = object()
+        cache.set("data1", ob1)
+        cache.set("data2", ob2, key={'foo': 1})
+        cache.set("data3", ob2, key={'foo': 2})
+        cache.invalidateAll()
+        marker = []
+        self.failIf(cache.query(ob1, default=marker) is not marker,
+                    "should not return cached result after invalidateAll")
+        self.failIf(cache.query(ob2, {'foo': 1}, default=marker) is not marker,
+                    "should not return cached result after invalidateAll")
+        self.failIf(cache.query(ob2, {'foo': 2}, default=marker) is not marker,
+                    "should not return cached result after invalidateAll")
+
+
+def test_suite():
+    return TestSuite((
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Zope3/src/zope/app/cache/tests/test_ramcache.py 1.1 => 1.2 === (480/580 lines abridged)
--- /dev/null	Wed Dec 25 09:13:46 2002
+++ Zope3/src/zope/app/cache/tests/test_ramcache.py	Wed Dec 25 09:12:45 2002
@@ -0,0 +1,577 @@
+#############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Unit tests for RAM Cache.
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from zope.app.cache.tests.test_icache import BaseICacheTest
+from zope.app.tests.placelesssetup import PlacelessSetup
+from zope.app.interfaces.traversing.physicallylocatable import IPhysicallyLocatable
+from zope.proxy.context import Wrapper
+from zope.component.adapter import provideAdapter
+from zope.interface.verify import verifyClass, verifyObject
+from time import time
+
+
+class Locatable:
+    __implements__ = IPhysicallyLocatable
+
+    def __init__(self, path=('a', 'b')):
+        self.path = path
+
+    def getPhysicalRoot(self):
+        return self
+
+    def getPhysicalPath(self):
+        return self.path
+
+class TestRAMCache(PlacelessSetup,
+                   TestCase,
+                   BaseICacheTest,
+                   ):
+
+    def _Test__new(self):
+        from zope.app.cache.ram import RAMCache

[-=- -=- -=- 480 lines omitted -=- -=- -=-]

+                   object2: {key1: [value, 4, 2],
+                             key2: [value, 5, 1],
+                             key3: [value, 6, 1]}}
+        s._misses = {object: 11, object2: 42}
+        len1 = len(dumps(s._data[object]))
+        len2 = len(dumps(s._data[object2]))
+
+        expected = ({'path': object,
+                     'hits': 17,
+                     'misses': 11,
+                     'size': len1,
+                     'entries': 3
+                     },
+                    {'path': object2,
+                     'hits': 4,
+                     'misses': 42,
+                     'size': len2,
+                     'entries': 3
+                     },
+                    )
+
+        result = s.getStatistics()
+
+        self.assertEqual(result, expected, "got unexpected stats")
+
+
+class TestModule(TestCase):
+
+    def test_locking(self):
+        from zope.app.cache.ram import writelock
+        writelock.acquire()
+        try:
+            self.failUnless(writelock.locked(), "locks don't work")
+        finally:
+            writelock.release()
+
+#############################################################################
+
+class Test(TestCase):
+    pass
+
+def test_suite():
+    return TestSuite((
+        makeSuite(TestRAMCache),
+        makeSuite(TestStorage),
+        makeSuite(TestModule),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')