Back to index

plone3  3.1.7
testing.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 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 """Node adapter testing utils.
00014 
00015 $Id: testing.py 84353 2008-02-27 20:24:03Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from xml.dom.minidom import parseString
00022 
00023 import Products.Five
00024 from OFS.interfaces import IItem
00025 from Products.Five import zcml
00026 from zope.component import getMultiAdapter
00027 from zope.interface import implements
00028 from zope.interface.verify import verifyClass
00029 from zope.testing import testrunner
00030 from zope.testing.cleanup import cleanUp
00031 
00032 from interfaces import IBody
00033 from interfaces import INode
00034 from interfaces import ISetupEnviron
00035 
00036 
00037 class DummyLogger:
00038 
00039     def __init__(self, id, messages):
00040         self._id = id
00041         self._messages = messages
00042 
00043     def info(self, msg, *args, **kwargs):
00044         self._messages.append((20, self._id, msg))
00045 
00046     def warning(self, msg, *args, **kwargs):
00047         self._messages.append((30, self._id, msg))
00048 
00049 
00050 class DummySetupEnviron(object):
00051 
00052     """Context for body im- and exporter.
00053     """
00054 
00055     implements(ISetupEnviron)
00056 
00057     def __init__(self):
00058         self._notes = []
00059         self._should_purge = True
00060 
00061     def getLogger(self, name):
00062         return DummyLogger(name, self._notes)
00063 
00064     def shouldPurge(self):
00065         return self._should_purge
00066 
00067 
00068 class IDummyMarker(IItem):
00069 
00070     pass
00071 
00072 
00073 class _AdapterTestCaseBase(unittest.TestCase):
00074 
00075     def _populate(self, obj):
00076         pass
00077 
00078     def _verifyImport(self, obj):
00079         pass
00080 
00081 
00082 class BodyAdapterTestCase(_AdapterTestCaseBase):
00083 
00084     def test_z3interfaces(self):
00085         verifyClass(IBody, self._getTargetClass())
00086 
00087     def test_body_get(self):
00088         self._populate(self._obj)
00089         context = DummySetupEnviron()
00090         adapted = getMultiAdapter((self._obj, context), IBody)
00091         self.assertEqual(adapted.body, self._BODY)
00092 
00093     def test_body_set(self):
00094         context = DummySetupEnviron()
00095         adapted = getMultiAdapter((self._obj, context), IBody)
00096         adapted.body = self._BODY
00097         self._verifyImport(self._obj)
00098         self.assertEqual(adapted.body, self._BODY)
00099 
00100         # now in update mode
00101         context._should_purge = False
00102         adapted = getMultiAdapter((self._obj, context), IBody)
00103         adapted.body = self._BODY
00104         self._verifyImport(self._obj)
00105         self.assertEqual(adapted.body, self._BODY)
00106 
00107         # and again in update mode
00108         adapted = getMultiAdapter((self._obj, context), IBody)
00109         adapted.body = self._BODY
00110         self._verifyImport(self._obj)
00111         self.assertEqual(adapted.body, self._BODY)
00112 
00113 
00114 class NodeAdapterTestCase(_AdapterTestCaseBase):
00115 
00116     def test_z3interfaces(self):
00117         verifyClass(INode, self._getTargetClass())
00118 
00119     def test_node_get(self):
00120         self._populate(self._obj)
00121         context = DummySetupEnviron()
00122         adapted = getMultiAdapter((self._obj, context), INode)
00123         self.assertEqual(adapted.node.toprettyxml(' '), self._XML)
00124 
00125     def test_node_set(self):
00126         context = DummySetupEnviron()
00127         adapted = getMultiAdapter((self._obj, context), INode)
00128         adapted.node = parseString(self._XML).documentElement
00129         self._verifyImport(self._obj)
00130         self.assertEqual(adapted.node.toprettyxml(' '), self._XML)
00131 
00132         # now in update mode
00133         context._should_purge = False
00134         adapted = getMultiAdapter((self._obj, context), INode)
00135         adapted.node = parseString(self._XML).documentElement
00136         self._verifyImport(self._obj)
00137         self.assertEqual(adapted.node.toprettyxml(' '), self._XML)
00138 
00139         # and again in update mode
00140         adapted = getMultiAdapter((self._obj, context), INode)
00141         adapted.node = parseString(self._XML).documentElement
00142         self._verifyImport(self._obj)
00143         self.assertEqual(adapted.node.toprettyxml(' '), self._XML)
00144 
00145 
00146 class ExportImportZCMLLayer:
00147 
00148     @classmethod
00149     def setUp(cls):
00150         import Products.Five
00151         import Products.GenericSetup
00152 
00153         zcml.load_config('meta.zcml', Products.Five)
00154         zcml.load_config('meta.zcml', Products.GenericSetup)
00155         zcml.load_config('permissions.zcml', Products.Five)
00156         zcml.load_config('traversing.zcml', Products.Five)
00157         zcml.load_config('configure.zcml', Products.GenericSetup)
00158 
00159     @classmethod
00160     def tearDown(cls):
00161         cleanUp()
00162 
00163 
00164 # Derive from ZopeLite layer if available
00165 try:
00166     from Testing.ZopeTestCase.layer import ZopeLite
00167 except ImportError:
00168     pass # Zope < 2.11
00169 else:
00170     ExportImportZCMLLayer.__bases__ = (ZopeLite,)
00171 
00172 
00173 def run(test_suite):
00174     options = testrunner.get_options()
00175     options.resume_layer = None
00176     options.resume_number = 0
00177     testrunner.run_with_options(options, test_suite)