Back to index

plone3  3.1.7
common.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 # 
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.0 (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 testing utilities
00014 
00015 $Id: common.py 72373 2007-02-05 16:18:41Z tseaver $
00016 """
00017 try:
00018     import transaction
00019 except ImportError:  # Zope < 2.8
00020     get_transaction = get_transaction
00021 else:
00022     def get_transaction():
00023         return transaction
00024 
00025 def common_setUp(self):
00026     # Install a hack to make SimpleItem version aware, so that the
00027     # tests work. In normal development, you would mix in the
00028     # VersionSupport class on an as-needed basis.
00029     import cStringIO
00030     from AccessControl.SecurityManagement import newSecurityManager
00031     from OFS.Application import Application
00032     from OFS.DTMLDocument import addDTMLDocument
00033     from OFS.Folder import manage_addFolder
00034     from Testing.makerequest import makerequest
00035 
00036     import Products.ZopeVersionControl
00037     Products.ZopeVersionControl.install_hack()
00038 
00039     from Products.ZopeVersionControl.ZopeRepository import addRepository
00040     from ZODB import DB
00041     from ZODB.DemoStorage import DemoStorage
00042     s = DemoStorage(quota=(1<<20))
00043     self.connection = DB( s ).open()
00044     try:
00045         r = self.connection.root()
00046         a = Application()
00047         r['Application'] = a
00048         self.root = a
00049         responseOut = self.responseOut = cStringIO.StringIO()
00050         self.app = makerequest( self.root, stdout=responseOut )
00051         self.app.acl_users.userFolderAddUser('UnitTester', '123', (), ())
00052         manage_addFolder(self.app, 'folder1')
00053         self.folder1 = getattr(self.app, 'folder1')
00054         manage_addFolder(self.folder1, 'folder2')
00055         self.folder2 = getattr(self.folder1, 'folder2')
00056         addDTMLDocument(self.folder2, 'document1', file='some text')
00057         self.document1 = getattr(self.folder2, 'document1')
00058         addDTMLDocument(self.folder2, 'document2', file='some text')
00059         self.document2 = getattr(self.folder2, 'document2')
00060         addDTMLDocument(self.folder2, 'document_nonversion', file='some?')
00061         self.document_nonversion = getattr(self.folder2,
00062                                             'document_nonversion')
00063         self.document_nonversion.__non_versionable__ = 1
00064         addRepository(self.folder1, 'repository')
00065         self.repository = getattr(self.folder1, 'repository')
00066         get_transaction().commit()            
00067     except:
00068         self.connection.close()
00069         raise
00070     get_transaction().begin()
00071     user = self.app.acl_users.getUser('UnitTester')
00072     user = user.__of__(self.app.acl_users)
00073     newSecurityManager( None, user)
00074 
00075 def common_tearDown(self):
00076     from AccessControl.SecurityManagement import noSecurityManager
00077     noSecurityManager()
00078     del self.folder1
00079     del self.folder2
00080     del self.document1
00081     del self.document2
00082     get_transaction().abort()
00083     self.app._p_jar.sync()
00084     self.connection.close()
00085     del self.app
00086     del self.responseOut
00087     del self.root
00088     del self.connection
00089 
00090 def common_commit(self):
00091     if self.do_commits:
00092         get_transaction().commit()
00093