[CMF-checkins] CVS: CMF/CMFCore/tests - test_CachingPolicyManager.py:1.2 test_FSPageTemplate.py:1.4 test_all.py:1.15

Tres Seaver tseaver@zope.com
Thu, 21 Mar 2002 07:27:35 -0500


Update of /cvs-repository/CMF/CMFCore/tests
In directory cvs.zope.org:/tmp/cvs-serv32416/CMFCore/tests

Modified Files:
	test_FSPageTemplate.py test_all.py 
Added Files:
	test_CachingPolicyManager.py 
Log Message:


  - Added CachingPolicyManager tool, which manages caching policies
    for skin methods, and updated FSPageTemplate to use a CPM if
    found.


=== CMF/CMFCore/tests/test_CachingPolicyManager.py 1.1 => 1.2 ===
+import unittest
+
+from DateTime.DateTime import DateTime
+
+EPOCH = DateTime( '1970/01/01' )
+ACCLARK = DateTime( '2001/01/01' )
+
+class DummyContent:
+
+    __allow_access_to_unprotected_subobjects__ = 1
+
+    def Type( self ):
+        return 'Dummy'
+
+    def modified( self ):
+        return EPOCH
+
+
+class CachingPolicyTests( unittest.TestCase ):
+
+    def _makePolicy( self, policy_id, **kw ):
+
+        from Products.CMFCore.CachingPolicyManager import CachingPolicy
+        return CachingPolicy( policy_id, **kw )
+
+    def _makeContext( self, **kw ):
+
+        from Products.CMFCore.CachingPolicyManager import createCPContext
+        return createCPContext( DummyContent(), 'foo_view', kw )
+        
+    def test_empty( self ):
+
+        policy = self._makePolicy( 'empty' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 1 )
+        self.assertEqual( headers[0][0] , 'Last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+
+    def test_noPassPredicate( self ):
+
+        policy = self._makePolicy( 'noPassPredicate', predicate='nothing' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 0 )
+
+    def test_typePredicate( self ):
+
+        policy = self._makePolicy( 'typePredicate'
+                           , predicate='python:content.Type() == "Dummy"' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 1 )
+        self.assertEqual( headers[0][0] , 'Last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+
+    def test_typePredicateMiss( self ):
+
+        policy = self._makePolicy( 'typePredicate'
+                           , predicate='python:content.Type() == "Foolish"' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 0 )
+
+    def test_viewPredicate( self ):
+
+        policy = self._makePolicy( 'viewPredicate'
+                           , predicate='python:view == "foo_view"' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 1 )
+        self.assertEqual( headers[0][0] , 'Last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+
+    def test_viewPredicateMiss( self ):
+
+        policy = self._makePolicy( 'viewPredicateMiss'
+                           , predicate='python:view == "bar_view"' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 0 )
+
+    def test_kwPredicate( self ):
+
+        policy = self._makePolicy( 'kwPredicate'
+                           , predicate='python:"foo" in keywords.keys()' )
+        context = self._makeContext( foo=1 )
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 1 )
+        self.assertEqual( headers[0][0] , 'Last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+
+    def test_kwPredicateMiss( self ):
+
+        policy = self._makePolicy( 'kwPredicateMiss'
+                           , predicate='python:"foo" in keywords.keys()' )
+        context = self._makeContext( bar=1 )
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 0 )
+
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 0 )
+        
+    def test_mtimeFunc( self ):
+
+        policy = self._makePolicy( 'mtimeFunc', mtime_func='string:2001/01/01' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 1 )
+        self.assertEqual( headers[0][0] , 'Last-modified' )
+        self.assertEqual( headers[0][1] , ACCLARK.rfc822() )
+        
+    def test_mtimeFuncNone( self ):
+
+        policy = self._makePolicy( 'mtimeFuncNone'
+                                 , mtime_func='nothing' )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 0 )
+        
+    def test_maxAge( self ):
+
+        policy = self._makePolicy( 'aged', max_age_secs=86400 )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 3 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'expires' )
+        self.assertEqual( headers[1][1] , (EPOCH+1).rfc822() )
+        self.assertEqual( headers[2][0].lower() , 'cache-control' )
+        self.assertEqual( headers[2][1] , 'max-age=86400' )
+        
+    def test_noCache( self ):
+
+        policy = self._makePolicy( 'noCache', no_cache=1 )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 2 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'cache-control' )
+        self.assertEqual( headers[1][1] , 'no-cache' )
+        
+    def test_noStore( self ):
+
+        policy = self._makePolicy( 'noStore', no_store=1 )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 2 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'cache-control' )
+        self.assertEqual( headers[1][1] , 'no-store' )
+        
+    def test_mustRevalidate( self ):
+
+        policy = self._makePolicy( 'mustRevalidate', must_revalidate=1 )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 2 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'cache-control' )
+        self.assertEqual( headers[1][1] , 'must-revalidate' )
+        
+    def test_combined( self ):
+
+        policy = self._makePolicy( 'noStore', no_cache=1, no_store=1 )
+        context = self._makeContext()
+        headers = policy.getHeaders( context )
+
+        self.assertEqual( len( headers ), 2 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'cache-control' )
+        self.assertEqual( headers[1][1] , 'no-cache, no-store' )
+
+
+class CachingPolicyManagerTests( unittest.TestCase ):
+
+    def _makeOne( self ):
+        from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
+        return CachingPolicyManager()
+
+    def test_interface( self ):
+        from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
+        from Products.CMFCore.interfaces.CachingPolicyManager \
+                import CachingPolicyManager as ICachingPolicyManager
+
+        from Interface import verify_class_implementation
+
+        verify_class_implementation( ICachingPolicyManager
+                                   , CachingPolicyManager )
+
+    def test_empty( self ):
+
+        mgr = self._makeOne()
+
+        self.assertEqual( len( mgr.listPolicies() ), 0 )
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent()
+                                           , view_method='foo_view'
+                                           , keywords={}
+                                           )
+        self.assertEqual( len( headers ), 0 )
+
+        self.assertRaises( KeyError, mgr._updatePolicy
+                         , 'xyzzy', None, None, None, None, None, None )
+        self.assertRaises( KeyError, mgr._removePolicy, 'xyzzy' )
+        self.assertRaises( KeyError, mgr._reorderPolicy, 'xyzzy', -1 )
+    
+    def test_addPolicy( self ):
+
+        mgr = self._makeOne()
+        mgr._addPolicy( 'first', 'python:1', None, 0, 0, 0, 0 )
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent()
+                                           , view_method='foo_view'
+                                           , keywords={}
+                                           )
+        self.assertEqual( len( headers ), 3 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'expires' )
+        self.assertEqual( headers[1][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[2][0].lower() , 'cache-control' )
+        self.assertEqual( headers[2][1] , 'max-age=0' )
+
+    def test_reorder( self ):
+
+        mgr = self._makeOne()
+
+        policy_ids = ( 'foo', 'bar', 'baz', 'qux' )
+
+        for policy_id in policy_ids:
+            mgr._addPolicy( policy_id
+                          , 'python:"%s" in keywords.keys()' % policy_id
+                          , None, 0, 0, 0, 0 )
+
+        ids = tuple( map( lambda x: x[0], mgr.listPolicies() ) )
+        self.assertEqual( ids, policy_ids )
+
+        mgr._reorderPolicy( 'bar', 3 )
+
+        ids = tuple( map( lambda x: x[0], mgr.listPolicies() ) )
+        self.assertEqual( ids, ( 'foo', 'baz', 'qux', 'bar' ) )
+
+    def _makeOneWithPolicies( self ):
+
+        mgr = self._makeOne()
+
+        policy_tuples = ( ( 'foo', None  )
+                        , ( 'bar', 0     )
+                        , ( 'baz', 3600  )
+                        , ( 'qux', 86400 )
+                        )
+
+        for policy_id, max_age_secs in policy_tuples:
+            mgr._addPolicy( policy_id
+                          , 'python:"%s" in keywords.keys()' % policy_id
+                          , None, max_age_secs, 0, 0, 0 )
+
+        return mgr
+
+    def test_lookupNoMatch( self ):
+
+        mgr = self._makeOneWithPolicies()
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent()
+                                           , view_method='foo_view'
+                                           , keywords={}
+                                           )
+        self.assertEqual( len( headers ), 0 )
+
+    def test_lookupMatchFoo( self ):
+
+        mgr = self._makeOneWithPolicies()
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent()
+                                           , view_method='foo_view'
+                                           , keywords={ 'foo' : 1 }
+                                           )
+        self.assertEqual( len( headers ), 1 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+
+
+    def test_lookupMatchBar( self ):
+
+        mgr = self._makeOneWithPolicies()
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent()
+                                           , view_method='foo_view'
+                                           , keywords={ 'bar' : 1 }
+                                           )
+        self.assertEqual( len( headers ), 3 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'expires' )
+        self.assertEqual( headers[1][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[2][0].lower() , 'cache-control' )
+        self.assertEqual( headers[2][1] , 'max-age=0' )
+
+
+    def test_lookupMatchBaz( self ):
+
+        mgr = self._makeOneWithPolicies()
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent()
+                                           , view_method='foo_view'
+                                           , keywords={ 'baz' : 1 }
+                                           )
+        self.assertEqual( len( headers ), 3 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'expires' )
+        self.assertEqual( headers[1][1] , (EPOCH+(1.0/24.0)).rfc822() )
+        self.assertEqual( headers[2][0].lower() , 'cache-control' )
+        self.assertEqual( headers[2][1] , 'max-age=3600' )
+
+
+    def test_lookupMatchQux( self ):
+
+        mgr = self._makeOneWithPolicies()
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent()
+                                           , view_method='foo_view'
+                                           , keywords={ 'qux' : 1 }
+                                           )
+        self.assertEqual( len( headers ), 3 )
+        self.assertEqual( headers[0][0].lower() , 'last-modified' )
+        self.assertEqual( headers[0][1] , EPOCH.rfc822() )
+        self.assertEqual( headers[1][0].lower() , 'expires' )
+        self.assertEqual( headers[1][1] , (EPOCH+1).rfc822() )
+        self.assertEqual( headers[2][0].lower() , 'cache-control' )
+        self.assertEqual( headers[2][1] , 'max-age=86400' )
+
+def test_suite():
+    return unittest.TestSuite((
+                                unittest.makeSuite( CachingPolicyTests ),
+                                unittest.makeSuite( CachingPolicyManagerTests ),
+                             ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


=== CMF/CMFCore/tests/test_FSPageTemplate.py 1.3 => 1.4 ===
 import Zope
 
+class DummyCachingManager:
+    def getHTTPCachingHeaders( self, content, view_name, keywords ):
+        return ( ( 'foo', 'Foo' ), ( 'bar', 'Bar' ) )
+
 from Products.CMFCore.tests.base.testcase import RequestTest, SecurityTest
 
 class FSPTMaker:
@@ -28,6 +32,19 @@
         script = script.__of__(self.root)
         self.assertRaises(Undefined,script)
 
+    def test_caching( self ):
+        """
+            Test HTTP caching headers.
+        """
+        self.root.caching_policy_manager = DummyCachingManager()
+        original_len = len( self.RESPONSE.headers )
+        script = self._makeOne('testPT', 'testPT.pt')
+        script = script.__of__(self.root)
+        script()
+        self.failUnless( len( self.RESPONSE.headers ) >= original_len + 2 )
+        self.failUnless( 'foo' in self.RESPONSE.headers.keys() )
+        self.failUnless( 'bar' in self.RESPONSE.headers.keys() )
+
 class FSPageTemplateCustomizationTests( SecurityTest, FSPTMaker ):
 
     def setUp( self ):
@@ -73,7 +90,4 @@
 
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')
-
-
-
 


=== CMF/CMFCore/tests/test_all.py 1.14 => 1.15 ===
         'test_DirectoryView',
         'test_FSPythonScript',
-        'test_FSPageTemplate'
+        'test_FSPageTemplate',
+        'test_CachingPolicyManager',
         ])
 
 if __name__ == '__main__':