[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Caching/RAMCache/tests - test_RAMCache.py:1.5

Albertas Agejevas alga@codeworks.lt
Mon, 2 Dec 2002 15:04:19 -0500


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

Modified Files:
	test_RAMCache.py 
Log Message:
Added stats reporting to the RAMCache.
Had to make Zope.App.RDB.ResultSet pickleable on the way.
Renamed ResultSet.names to ResultSet.columns.
Updated SQLScript to pass the location to the cache, not self.


=== Zope3/lib/python/Zope/App/Caching/RAMCache/tests/test_RAMCache.py 1.4 => 1.5 ===
--- Zope3/lib/python/Zope/App/Caching/RAMCache/tests/test_RAMCache.py:1.4	Wed Nov 27 12:37:49 2002
+++ Zope3/lib/python/Zope/App/Caching/RAMCache/tests/test_RAMCache.py	Mon Dec  2 15:03:47 2002
@@ -38,11 +38,11 @@
     def getPhysicalPath(self):
         return self.path
 
-class TestRAMCache(PlacelessSetup, 
-                   TestCase, 
+class TestRAMCache(PlacelessSetup,
+                   TestCase,
                    BaseICacheTest,
                    ):
-    
+
     def _Test__new(self):
         from Zope.App.Caching.RAMCache.RAMCache import RAMCache
         return RAMCache()
@@ -52,9 +52,9 @@
         from Zope.App.Caching.RAMCache.IRAMCache import IRAMCache
         from Zope.App.Caching.ICache import ICache
         from Zope.Event.ISubscriber import ISubscriber
-        verifyObject(IRAMCache, RAMCache()) 
-        verifyClass(ICache, RAMCache) 
-        verifyClass(ISubscriber, RAMCache) 
+        verifyObject(IRAMCache, RAMCache())
+        verifyClass(ICache, RAMCache)
+        verifyClass(ISubscriber, RAMCache)
 
     def test_init(self):
         from Zope.App.Caching.RAMCache.RAMCache import RAMCache
@@ -63,6 +63,19 @@
         self.assertNotEquals(c1._cacheId, c2._cacheId,
                              "The cacheId is not unique")
 
+
+    def test_getStatistics(self):
+        from Zope.App.Caching.RAMCache.RAMCache import RAMCache
+        c = RAMCache()
+        c.set(42, "object", key={'foo': 'bar'})
+        c.set(43, "object", key={'foo': 'bar'})
+        c.query("object")
+        c.query("object", key={'foo': 'bar'})
+        r1 = c._getStorage().getStatistics()
+        r2 = c.getStatistics()
+
+        self.assertEqual(r1, r2, "see Storage.getStatistics() tests")
+
     def test_update(self):
         from Zope.App.Caching.RAMCache.RAMCache import RAMCache
         c = RAMCache()
@@ -107,11 +120,11 @@
         self.assertEqual(storage1, storage2,
                          "_getStorage returns different storages")
 
-    def test_buildKey(self): 
+    def test_buildKey(self):
         from Zope.App.Caching.RAMCache.RAMCache import RAMCache
 
         kw = {'foo': 1, 'bar': 2, 'baz': 3}
-        
+
         key = RAMCache._buildKey(kw)
 
         self.assertEqual(key, (('bar',2), ('baz',3), ('foo',1)), "wrong key")
@@ -121,11 +134,10 @@
         from Zope.App.Caching.RAMCache.RAMCache import RAMCache
 
         ob = ('aaa',)
-        
+
         keywords = {"answer": 42}
         value = "true"
         c = RAMCache()
-        c.requestVars = ('foo', 'bar')
         key = RAMCache._buildKey(keywords)
         c._getStorage().setEntry(ob, key, value)
 
@@ -150,7 +162,7 @@
         self.assertEqual(c._getStorage().getEntry(ob, key), value,
                          "Not stored correctly")
 
-        
+
     def test_invalidate(self):
         from Zope.App.Caching.RAMCache.RAMCache import RAMCache
 
@@ -184,7 +196,7 @@
         self.assertRaises(KeyError, c._getStorage().getEntry, ob2, key2)
         self.assertRaises(KeyError, c._getStorage().getEntry, ob2, key3)
         c._getStorage().getEntry(ob1, key1)
-        
+
         # Try something that's not there
         c.invalidate(('yadda',))
 
@@ -193,7 +205,7 @@
         from Zope.Event.ObjectEvent import ObjectModifiedEvent
 
         location = ('aaa',)
-        
+
         keywords = {"answer": 42}
         value = "true"
         c = RAMCache()
@@ -210,7 +222,7 @@
 
 
 class TestStorage(TestCase):
-        
+
     def test_getEntry(self):
         from Zope.App.Caching.RAMCache.RAMCache import Storage
         s = Storage()
@@ -218,15 +230,35 @@
         key = ('view', (), ('answer', 42))
         value = 'yes'
         timestamp = time()
-        
+
         s._data = {object: {key: [value, timestamp, 1]}}
         self.assertEqual(s.getEntry(object, key), value, 'got wrong value')
-        
+
         self.assert_(s._data[object][key][2] == 2, 'access count not updated')
 
+        # See if _misses are updated
+        try:
+            s.getEntry(object, "Nonexistent")
+        except KeyError:
+            pass
+        else:
+            raise "ExpectedKeyError"
+
+        self.assertEqual(s._misses[object], 1)
+
+        object2 = "second"
+        self.assert_(not s._misses.has_key(object2))
+        try:
+            s.getEntry(object2, "Nonexistent")
+        except KeyError:
+            pass
+        else:
+            raise "ExpectedKeyError"
+        self.assertEqual(s._misses[object2], 1)
+
     def test_setEntry(self):
         from Zope.App.Caching.RAMCache.RAMCache import Storage
-        
+
         s = Storage(cleanupInterval=300, maxAge=300)
         object = 'object'
         key = ('view', (), ('answer', 42))
@@ -239,7 +271,7 @@
 
         timestamp = s._data[object][key][1]
         self.failUnless(t1 <= timestamp <= t2, 'wrong timestamp')
-        
+
         self.assertEqual(s._data, {object: {key: [value, timestamp, 0]}},
                          'stored data incorrectly')
 
@@ -251,7 +283,7 @@
         timestamp = s._data[object][key2][1]
         self.assertEqual(s._data, {object: {key2: [value, timestamp, 0]}},
                          'cleanup not called')
-        
+
 
     def test_set_get(self):
         from Zope.App.Caching.RAMCache.RAMCache import Storage
@@ -275,9 +307,11 @@
         s._data = {object:  {key: [value, ts, 0],
                              key2: [value, ts, 0]},
                    object2: {key: [value, ts, 0]}}
+        s._misses[object] = 42
         s._do_invalidate(object)
         self.assertEqual(s._data, {object2: {key: [value, ts, 0]}},
                          'invalidation failed')
+        self.assertEqual(s._misses[object], 0, "misses counter not cleared")
 
         s._data = {object:  {key: [value, ts, 0],
                              key2: [value, ts, 0]},
@@ -300,7 +334,7 @@
         s._data = {object:  {key: [value, ts, 0],
                              key2: [value, ts, 0]},
                    object2: {key: [value, ts, 0]}}
-        
+
         s.writelock.acquire()
         try:
             s.invalidate(object)
@@ -315,7 +349,7 @@
         s.invalidate(object)
         self.assertEqual(s._data, {object2: {key: [value, ts, 0]}},
                          "not invalidated")
-        
+
     def test_invalidate_queued(self):
         from Zope.App.Caching.RAMCache.RAMCache import Storage
         s = Storage()
@@ -369,7 +403,7 @@
         self.assertEqual(s._data, {object2: {key: [value, timestamp-90, 0]}},
                          'stale records removed incorrectly')
 
-        
+
         s = Storage(maxAge=0)
         s._data = {object:  {key: [value, timestamp, 2]},
                    object2: {key: [value, timestamp-90, 0]}}
@@ -436,6 +470,7 @@
                    object2: {key1: [value, 4, 2],
                              key2: [value, 5, 1],
                              key3: [value, 6, 1]}}
+        s._misses = {object: 4, object2: 2}
 
         cleared = {object:  {key1: [value, 1, 0],
                              key2: [value, 2, 0],
@@ -443,9 +478,50 @@
                    object2: {key1: [value, 4, 0],
                              key2: [value, 5, 0],
                              key3: [value, 6, 0]}}
+        clearMisses = {object: 0, object2: 0}
 
         s._clearAccessCounters()
         self.assertEqual(s._data, cleared, "access counters not cleared")
+        self.assertEqual(s._misses, clearMisses, "misses counter not cleared")
+
+    def test_getStatistics(self):
+        from Zope.App.Caching.RAMCache.RAMCache import Storage
+        from pickle import dumps
+        s = Storage(maxEntries=3)
+        object = 'object'
+        object2 = 'object2'
+        key1 = ('view1', (), ('answer', 42))
+        key2 = ('view2', (), ('answer', 42))
+        key3 = ('view3', (), ('answer', 42))
+        value = 'yes'
+        timestamp = time()
+        s._data = {object:  {key1: [value, 1, 10],
+                             key2: [value, 2, 5],
+                             key3: [value, 3, 2]},
+                   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):
@@ -457,7 +533,7 @@
             self.failUnless(writelock.locked(), "locks don't work")
         finally:
             writelock.release()
-    
+
 #############################################################################
 
 class Test(TestCase):