Back to index

plone3  3.1.7
test_zcml.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006-2007 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Unit tests for zcml module.
00014 
00015 $Id: test_zcml.py 82937 2008-01-17 21:55:32Z hannosch $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 from zope.testing import doctest
00021 from zope.testing.doctest import ELLIPSIS
00022 from zope.testing.cleanup import cleanUp
00023 
00024 from Products.GenericSetup.registry import _import_step_registry
00025 from Products.GenericSetup.registry import _export_step_registry
00026 from Products.Five import zcml
00027 
00028 def dummy_importstep_handler(context):
00029     pass
00030 
00031 def dummy_exportstep_handler(context):
00032     pass
00033 
00034 def dummy_upgrade_handler(context):
00035     pass
00036 
00037 def b_dummy_upgrade_handler(context):
00038     pass
00039 
00040 def c_dummy_upgrade_handler(context):
00041     pass
00042 
00043 def test_simpleRegisterProfile():
00044     """
00045     Use the genericsetup:registerProfile directive::
00046 
00047       >>> import Products.GenericSetup
00048       >>> from Products.Five import zcml
00049       >>> configure_zcml = '''
00050       ... <configure
00051       ...     xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
00052       ...     i18n_domain="foo">
00053       ...   <genericsetup:registerProfile
00054       ...       provides="Products.GenericSetup.interfaces.EXTENSION"
00055       ...       />
00056       ... </configure>'''
00057       >>> zcml.load_config('meta.zcml', Products.GenericSetup)
00058       >>> zcml.load_string(configure_zcml)
00059 
00060     Make sure the profile is registered correctly::
00061 
00062       >>> from Products.GenericSetup.registry import _profile_registry
00063       >>> profile_id = 'Products.GenericSetup:default'
00064       >>> profile_id in _profile_registry._profile_ids
00065       True
00066       >>> info = _profile_registry._profile_info[profile_id]
00067       >>> info['id']
00068       u'Products.GenericSetup:default'
00069       >>> info['title']
00070       u"Profile 'default' from 'Products.GenericSetup'"
00071       >>> info['description']
00072       u''
00073       >>> info['path']
00074       u'profiles/default'
00075       >>> info['product']
00076       'Products.GenericSetup'
00077       >>> from Products.GenericSetup.interfaces import EXTENSION
00078       >>> info['type'] is EXTENSION
00079       True
00080       >>> info['for'] is None
00081       True
00082 
00083     Clean up and make sure the cleanup works::
00084 
00085       >>> from zope.testing.cleanup import cleanUp
00086       >>> cleanUp()
00087       >>> profile_id in _profile_registry._profile_ids
00088       False
00089       >>> profile_id in _profile_registry._profile_info
00090       False
00091     """
00092 
00093 def test_registerProfile():
00094     """
00095     Use the genericsetup:registerProfile directive::
00096 
00097       >>> import Products.GenericSetup
00098       >>> from Products.Five import zcml
00099       >>> configure_zcml = '''
00100       ... <configure
00101       ...     xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
00102       ...     i18n_domain="foo">
00103       ...   <genericsetup:registerProfile
00104       ...       name="default"
00105       ...       title="Install Foo Extension"
00106       ...       description="Adds foo support."
00107       ...       provides="Products.GenericSetup.interfaces.EXTENSION"
00108       ...       />
00109       ... </configure>'''
00110       >>> zcml.load_config('meta.zcml', Products.GenericSetup)
00111       >>> zcml.load_string(configure_zcml)
00112 
00113     Make sure the profile is registered correctly::
00114 
00115       >>> from Products.GenericSetup.registry import _profile_registry
00116       >>> profile_id = 'Products.GenericSetup:default'
00117       >>> profile_id in _profile_registry._profile_ids
00118       True
00119       >>> info = _profile_registry._profile_info[profile_id]
00120       >>> info['id']
00121       u'Products.GenericSetup:default'
00122       >>> info['title']
00123       u'Install Foo Extension'
00124       >>> info['description']
00125       u'Adds foo support.'
00126       >>> info['path']
00127       u'profiles/default'
00128       >>> info['product']
00129       'Products.GenericSetup'
00130       >>> from Products.GenericSetup.interfaces import EXTENSION
00131       >>> info['type'] is EXTENSION
00132       True
00133       >>> info['for'] is None
00134       True
00135 
00136     Clean up and make sure the cleanup works::
00137 
00138       >>> from zope.testing.cleanup import cleanUp
00139       >>> cleanUp()
00140       >>> profile_id in _profile_registry._profile_ids
00141       False
00142       >>> profile_id in _profile_registry._profile_info
00143       False
00144     """
00145 
00146 def test_registerUpgradeStep(self):
00147     """
00148     Use the genericsetup:upgradeStep directive::
00149 
00150       >>> import Products.GenericSetup
00151       >>> from Products.Five import zcml
00152       >>> configure_zcml = '''
00153       ... <configure
00154       ...     xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
00155       ...     i18n_domain="foo">
00156       ...   <genericsetup:upgradeStep
00157       ...       title="Upgrade Foo Product"
00158       ...       description="Upgrades Foo from 1.0 to 1.1."
00159       ...       source="1.0"
00160       ...       destination="1.1"
00161       ...       handler="Products.GenericSetup.tests.test_zcml.dummy_upgrade_handler"
00162       ...       sortkey="1"
00163       ...       profile="default"
00164       ...       />
00165       ... </configure>'''
00166       >>> zcml.load_config('meta.zcml', Products.GenericSetup)
00167       >>> zcml.load_string(configure_zcml)
00168 
00169     Make sure the upgrade step is registered correctly::
00170 
00171       >>> from Products.GenericSetup.upgrade import _upgrade_registry
00172       >>> profile_steps = _upgrade_registry.getUpgradeStepsForProfile('default')
00173       >>> keys = profile_steps.keys()
00174       >>> len(keys)
00175       1
00176       >>> step = profile_steps[keys[0]]
00177       >>> step.source
00178       ('1', '0')
00179       >>> step.dest
00180       ('1', '1')
00181       >>> step.handler
00182       <function dummy_upgrade_handler at ...>
00183 
00184     Clean up and make sure the cleanup works::
00185 
00186       >>> from zope.testing.cleanup import cleanUp
00187       >>> cleanUp()
00188     """
00189 
00190 
00191 def test_registerUpgradeSteps(self):
00192     """
00193     Use the nested genericsetup:upgradeSteps directive::
00194 
00195       >>> import Products.GenericSetup
00196       >>> from Products.Five import zcml
00197       >>> configure_zcml = '''
00198       ... <configure
00199       ...     xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
00200       ...     i18n_domain="foo">
00201       ...   <genericsetup:upgradeSteps
00202       ...       profile="default"
00203       ...       source="1.0"
00204       ...       destination="1.1"
00205       ...       sortkey="2"
00206       ...       >
00207       ...       <genericsetup:upgradeStep
00208       ...           title="Foo Upgrade Step 1"
00209       ...           description="Does some Foo upgrade thing."
00210       ...           handler="Products.GenericSetup.tests.test_zcml.b_dummy_upgrade_handler"
00211       ...           />
00212       ...       <genericsetup:upgradeStep
00213       ...           title="Foo Upgrade Step 2"
00214       ...           description="Does another Foo upgrade thing."
00215       ...           handler="Products.GenericSetup.tests.test_zcml.c_dummy_upgrade_handler"
00216       ...           />
00217       ...   </genericsetup:upgradeSteps>
00218       ...   <genericsetup:upgradeSteps
00219       ...       profile="default"
00220       ...       source="1.0"
00221       ...       destination="1.1"
00222       ...       sortkey="1"
00223       ...       >
00224       ...       <genericsetup:upgradeStep
00225       ...           title="Bar Upgrade Step 1"
00226       ...           description="Does some Bar upgrade thing."
00227       ...           handler="Products.GenericSetup.tests.test_zcml.b_dummy_upgrade_handler"
00228       ...           />
00229       ...       <genericsetup:upgradeStep
00230       ...           title="Bar Upgrade Step 2"
00231       ...           description="Does another Bar upgrade thing."
00232       ...           handler="Products.GenericSetup.tests.test_zcml.c_dummy_upgrade_handler"
00233       ...           />
00234       ...   </genericsetup:upgradeSteps>
00235       ... </configure>'''
00236       >>> zcml.load_config('meta.zcml', Products.GenericSetup)
00237       >>> zcml.load_string(configure_zcml)
00238 
00239     Make sure the upgrade steps are registered correctly::
00240 
00241       >>> from Products.GenericSetup.upgrade import _upgrade_registry
00242       >>> from Products.GenericSetup.upgrade import listUpgradeSteps
00243       >>> from Products.GenericSetup.tool import SetupTool
00244       >>> tool = SetupTool('setup_tool')
00245       >>> profile_steps = listUpgradeSteps(tool, 'default', '1.0')
00246       >>> len(profile_steps)
00247       2
00248       >>> steps = profile_steps[0]
00249       >>> type(steps)
00250       <type 'list'>
00251       >>> len(steps)
00252       2
00253       >>> step1, step2 = steps
00254       >>> step1['source'] == step2['source'] == ('1', '0')
00255       True
00256       >>> step1['dest'] == step2['dest'] == ('1', '1')
00257       True
00258       >>> step1['step'].handler
00259       <function b_dummy_upgrade_handler at ...>
00260       >>> step1['title']
00261       u'Bar Upgrade Step 1'
00262       >>> step2['step'].handler
00263       <function c_dummy_upgrade_handler at ...>
00264       >>> step2['title']
00265       u'Bar Upgrade Step 2'
00266       
00267     First one listed should be second in the registry due to sortkey:
00268 
00269       >>> steps = profile_steps[1]
00270       >>> type(steps)
00271       <type 'list'>
00272       >>> len(steps)
00273       2
00274       >>> step1, step2 = steps
00275       >>> step1['source'] == step2['source'] == ('1', '0')
00276       True
00277       >>> step1['dest'] == step2['dest'] == ('1', '1')
00278       True
00279       >>> step1['step'].handler
00280       <function b_dummy_upgrade_handler at ...>
00281       >>> step1['title']
00282       u'Foo Upgrade Step 1'
00283       >>> step2['step'].handler
00284       <function c_dummy_upgrade_handler at ...>
00285       >>> step2['title']
00286       u'Foo Upgrade Step 2'
00287 
00288     Clean up and make sure the cleanup works::
00289 
00290       >>> from zope.testing.cleanup import cleanUp
00291       >>> cleanUp()
00292     """
00293 
00294 
00295 class ImportStepTests(unittest.TestCase):
00296 
00297     def setUp(self):
00298         import Products.GenericSetup
00299         zcml.load_config('meta.zcml', Products.GenericSetup)
00300 
00301     def tearDown(self):
00302         cleanUp()
00303 
00304     def testNoDependencies(self):
00305         zcml.load_string("""<configure
00306                               xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
00307                               i18n_domain="genericsetup">
00308                              <genericsetup:importStep
00309                                  name="name"
00310                                  title="title"
00311                                  description="description"
00312                                  handler="Products.GenericSetup.tests.test_zcml.dummy_importstep_handler">
00313                              </genericsetup:importStep>
00314                             </configure>""")
00315         from Products.GenericSetup.zcml import _import_step_regs
00316         self.assertEqual(_import_step_regs, [u'name'])
00317         self.assertEqual( _import_step_registry.listSteps(), [u'name'])
00318         data=_import_step_registry.getStepMetadata(u'name')
00319         self.assertEqual(data["handler"],
00320                 'Products.GenericSetup.tests.test_zcml.dummy_importstep_handler')
00321         self.assertEqual(data["description"], u"description")
00322         self.assertEqual(data["title"], u"title")
00323         self.assertEqual(data["dependencies"], ())
00324         self.assertEqual(data["id"], u"name")
00325 
00326 
00327     def testWithDependency(self):
00328         zcml.load_string("""<configure
00329                               xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
00330                               i18n_domain="genericsetup">
00331                              <genericsetup:importStep
00332                                  name="name"
00333                                  title="title"
00334                                  description="description"
00335                                  handler="Products.GenericSetup.tests.test_zcml.dummy_importstep_handler">
00336                                 <depends name="something.else"/>
00337                              </genericsetup:importStep>
00338                             </configure>""")
00339         data=_import_step_registry.getStepMetadata(u'name')
00340         self.assertEqual(data["dependencies"], (u"something.else",))
00341 
00342 
00343 
00344 class ExportStepTests(unittest.TestCase):
00345 
00346     def setUp(self):
00347         import Products.GenericSetup
00348         zcml.load_config('meta.zcml', Products.GenericSetup)
00349 
00350     def tearDown(self):
00351         cleanUp()
00352 
00353     def testRegistration(self):
00354         zcml.load_string("""<configure
00355                               xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
00356                               i18n_domain="genericsetup">
00357                              <genericsetup:exportStep
00358                                  name="name"
00359                                  title="title"
00360                                  description="description"
00361                                  handler="Products.GenericSetup.tests.test_zcml.dummy_exportstep_handler"
00362                                  />
00363                               </configure>
00364                               """)
00365         from Products.GenericSetup.zcml import _export_step_regs
00366         self.assertEqual(_export_step_regs, [u'name'])
00367         self.assertEqual( _export_step_registry.listSteps(), [u'name'])
00368         data=_export_step_registry.getStepMetadata(u'name')
00369         self.assertEqual(data["handler"],
00370                 'Products.GenericSetup.tests.test_zcml.dummy_exportstep_handler')
00371         self.assertEqual(data["description"], u"description")
00372         self.assertEqual(data["title"], u"title")
00373         self.assertEqual(data["id"], u"name")
00374 
00375 
00376 def test_suite():
00377     suite = unittest.TestSuite()
00378     suite.addTest(doctest.DocTestSuite(optionflags=ELLIPSIS))
00379     suite.addTest(unittest.makeSuite(ImportStepTests))
00380     suite.addTest(unittest.makeSuite(ExportStepTests))
00381     return suite
00382 
00383 if __name__ == '__main__':
00384     unittest.main(defaultTest='test_suite')