vendor/CMF/1.6.3/CMFCore

view exportimport/tests/test_cachingpolicymgr.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """Caching policy manager xml adapter and setup handler unit tests.
15 $Id$
16 """
18 import unittest
19 import Testing
21 import Products
22 from OFS.Folder import Folder
23 from Products.Five import zcml
25 from Products.GenericSetup.testing import BodyAdapterTestCase
26 from Products.GenericSetup.testing import NodeAdapterTestCase
27 from Products.GenericSetup.tests.common import BaseRegistryTests
28 from Products.GenericSetup.tests.common import DummyExportContext
29 from Products.GenericSetup.tests.common import DummyImportContext
31 from Products.CMFCore.tests.base.testcase import PlacelessSetup
33 _CP_XML = """\
34 <caching-policy name="foo_policy" enable_304s="False" etag_func=""
35 last_modified="True" max_age_secs="0" mtime_func="object/modified"
36 must_revalidate="False" no_cache="False" no_store="False"
37 no_transform="False" predicate="python:1" private="False"
38 proxy_revalidate="False" public="False" vary=""/>
39 """
41 _CPM_BODY = """\
42 <?xml version="1.0"?>
43 <object name="caching_policy_manager" meta_type="CMF Caching Policy Manager">
44 <caching-policy name="foo_policy" enable_304s="False" etag_func=""
45 last_modified="True" max_age_secs="600" mtime_func="object/modified"
46 must_revalidate="False" no_cache="False" no_store="False"
47 no_transform="False"
48 predicate="python:object.getPortalTypeName() == 'Foo'" private="False"
49 proxy_revalidate="False" public="False" vary=""/>
50 </object>
51 """
54 class CachingPolicyNodeAdapterTests(NodeAdapterTestCase):
56 def _getTargetClass(self):
57 from Products.CMFCore.exportimport.cachingpolicymgr \
58 import CachingPolicyNodeAdapter
60 return CachingPolicyNodeAdapter
62 def setUp(self):
63 import Products.CMFCore.exportimport
64 from Products.CMFCore.CachingPolicyManager import CachingPolicy
66 NodeAdapterTestCase.setUp(self)
67 zcml.load_config('configure.zcml', Products.CMFCore.exportimport)
69 self._obj = CachingPolicy('foo_policy', max_age_secs=0)
70 self._XML = _CP_XML
73 class CachingPolicyManagerXMLAdapterTests(BodyAdapterTestCase):
75 def _getTargetClass(self):
76 from Products.CMFCore.exportimport.cachingpolicymgr \
77 import CachingPolicyManagerXMLAdapter
79 return CachingPolicyManagerXMLAdapter
81 def _populate(self, obj):
82 obj.addPolicy('foo_policy',
83 'python:object.getPortalTypeName() == \'Foo\'',
84 'object/modified', 600, 0, 0, 0, '', '')
86 def setUp(self):
87 import Products.CMFCore.exportimport
88 from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
90 BodyAdapterTestCase.setUp(self)
91 zcml.load_config('configure.zcml', Products.CMFCore.exportimport)
93 self._obj = CachingPolicyManager()
94 self._BODY = _CPM_BODY
97 class _CachingPolicyManagerSetup(PlacelessSetup, BaseRegistryTests):
99 POLICY_ID = 'policy_id'
100 PREDICATE = "python:object.getId() == 'foo'"
101 MTIME_FUNC = "object/modified"
102 MAX_AGE_SECS = 60
103 VARY = "Test"
104 ETAG_FUNC = "object/getETag"
105 S_MAX_AGE_SECS = 120
106 PRE_CHECK = 42
107 POST_CHECK = 43
109 _EMPTY_EXPORT = """\
110 <?xml version="1.0"?>
111 <object name="caching_policy_manager" meta_type="CMF Caching Policy Manager"/>
112 """
114 _WITH_POLICY_EXPORT = """\
115 <?xml version="1.0"?>
116 <object name="caching_policy_manager" meta_type="CMF Caching Policy Manager">
117 <caching-policy name="%s" enable_304s="True"
118 etag_func="%s" last_modified="False" max_age_secs="%d"
119 mtime_func="%s" must_revalidate="True" no_cache="True"
120 no_store="True" no_transform="True" post_check="%d" pre_check="%d"
121 predicate="%s" private="True"
122 proxy_revalidate="True" public="True" s_max_age_secs="%d" vary="%s"/>
123 </object>
124 """ % (POLICY_ID, ETAG_FUNC, MAX_AGE_SECS, MTIME_FUNC, POST_CHECK, PRE_CHECK,
125 PREDICATE, S_MAX_AGE_SECS, VARY)
127 def _initSite(self, with_policy=False):
128 from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
130 self.root.site = Folder(id='site')
131 site = self.root.site
132 mgr = CachingPolicyManager()
133 site._setObject( mgr.getId(), mgr )
135 if with_policy:
136 mgr.addPolicy( policy_id=self.POLICY_ID
137 , predicate=self.PREDICATE
138 , mtime_func=self.MTIME_FUNC
139 , max_age_secs=self.MAX_AGE_SECS
140 , no_cache=True
141 , no_store=True
142 , must_revalidate=True
143 , vary=self.VARY
144 , etag_func=self.ETAG_FUNC
145 , s_max_age_secs=self.S_MAX_AGE_SECS
146 , proxy_revalidate=True
147 , public=True
148 , private=True
149 , no_transform=True
150 , enable_304s=True
151 , last_modified=False
152 , pre_check=self.PRE_CHECK
153 , post_check=self.POST_CHECK
154 )
156 return site
158 def setUp(self):
159 PlacelessSetup.setUp(self)
160 BaseRegistryTests.setUp(self)
161 zcml.load_config('meta.zcml', Products.Five)
162 zcml.load_config('configure.zcml', Products.CMFCore.exportimport)
164 def tearDown(self):
165 BaseRegistryTests.tearDown(self)
166 PlacelessSetup.tearDown(self)
169 class exportCachingPolicyManagerTests(_CachingPolicyManagerSetup):
171 def test_empty(self):
172 from Products.CMFCore.exportimport.cachingpolicymgr \
173 import exportCachingPolicyManager
175 site = self._initSite(with_policy=False)
176 context = DummyExportContext(site)
177 exportCachingPolicyManager(context)
179 self.assertEqual(len(context._wrote), 1)
180 filename, text, content_type = context._wrote[0]
181 self.assertEqual(filename, 'cachingpolicymgr.xml')
182 self._compareDOM(text, self._EMPTY_EXPORT)
183 self.assertEqual(content_type, 'text/xml')
185 def test_with_policy(self):
186 from Products.CMFCore.exportimport.cachingpolicymgr \
187 import exportCachingPolicyManager
189 site = self._initSite(with_policy=True)
190 context = DummyExportContext(site)
191 exportCachingPolicyManager(context)
193 self.assertEqual(len(context._wrote), 1)
194 filename, text, content_type = context._wrote[0]
195 self.assertEqual(filename, 'cachingpolicymgr.xml')
196 self._compareDOM(text, self._WITH_POLICY_EXPORT)
197 self.assertEqual(content_type, 'text/xml')
200 class importCachingPolicyManagerTests(_CachingPolicyManagerSetup):
202 def test_normal(self):
203 from Products.CMFCore.exportimport.cachingpolicymgr \
204 import importCachingPolicyManager
206 site = self._initSite(with_policy=False)
207 cpm = site.caching_policy_manager
208 self.assertEqual(len(cpm.listPolicies()), 0)
210 context = DummyImportContext(site)
211 context._files['cachingpolicymgr.xml'] = self._WITH_POLICY_EXPORT
212 importCachingPolicyManager(context)
214 self.assertEqual(len(cpm.listPolicies()), 1)
215 policy_id, policy = cpm.listPolicies()[0]
216 self.assertEqual(policy.getPolicyId(), self.POLICY_ID)
217 self.assertEqual(policy.getPredicate(), self.PREDICATE)
218 self.assertEqual(policy.getMTimeFunc(), self.MTIME_FUNC)
219 self.assertEqual(policy.getVary(), self.VARY)
220 self.assertEqual(policy.getETagFunc(), self.ETAG_FUNC)
221 self.assertEqual(policy.getMaxAgeSecs(), self.MAX_AGE_SECS)
222 self.assertEqual(policy.getSMaxAgeSecs(), self.S_MAX_AGE_SECS)
223 self.assertEqual(policy.getPreCheck(), self.PRE_CHECK)
224 self.assertEqual(policy.getPostCheck(), self.POST_CHECK)
225 self.assertEqual(policy.getLastModified(), False)
226 self.assertEqual(policy.getNoCache(), True)
227 self.assertEqual(policy.getNoStore(), True)
228 self.assertEqual(policy.getMustRevalidate(), True)
229 self.assertEqual(policy.getProxyRevalidate(), True)
230 self.assertEqual(policy.getNoTransform(), True)
231 self.assertEqual(policy.getPublic(), True)
232 self.assertEqual(policy.getPrivate(), True)
233 self.assertEqual(policy.getEnable304s(), True)
236 def test_suite():
237 return unittest.TestSuite((
238 unittest.makeSuite(CachingPolicyNodeAdapterTests),
239 unittest.makeSuite(CachingPolicyManagerXMLAdapterTests),
240 unittest.makeSuite(exportCachingPolicyManagerTests),
241 unittest.makeSuite(importCachingPolicyManagerTests),
242 ))
244 if __name__ == '__main__':
245 unittest.main(defaultTest='test_suite')