[CMF-checkins] CVS: CMF/CMFSetup/tests - common.py:1.3
test_context.py:1.3 test_tool.py:1.6
Tres Seaver
tseaver at zope.com
Sun May 23 21:52:44 EDT 2004
Update of /cvs-repository/CMF/CMFSetup/tests
In directory cvs.zope.org:/tmp/cvs-serv15342/tests
Modified Files:
common.py test_context.py test_tool.py
Log Message:
- context.py:
o Add a new export context type, TarballExportContext, which creates
an on-the-fly tarball from the calls to 'writeDataFile'.
- interfaces.py:
o Document extended interface for 'runExportStep' and 'runAllExportSteps'.
- tool.py:
o Set up always-on export step for the two step registries.
o Implement 'runExportStep' and 'runAllExportSteps' using
TarballExportContext.
=== CMF/CMFSetup/tests/common.py 1.2 => 1.3 ===
--- CMF/CMFSetup/tests/common.py:1.2 Sun May 23 16:31:43 2004
+++ CMF/CMFSetup/tests/common.py Sun May 23 21:52:43 2004
@@ -2,16 +2,12 @@
"""
import os
import shutil
+from tarfile import TarFile
from Products.CMFCore.tests.base.testcase import SecurityRequestTest
-class BaseRegistryTests( SecurityRequestTest ):
-
- def _makeOne( self, *args, **kw ):
-
- # Derived classes must implement _getTargetClass
- return self._getTargetClass()( *args, **kw )
+class DOMComparator:
def _compareDOM( self, found_text, expected_text ):
@@ -20,6 +16,13 @@
expected = parseString( expected_text )
self.assertEqual( found.toxml(), expected.toxml() )
+class BaseRegistryTests( SecurityRequestTest, DOMComparator ):
+
+ def _makeOne( self, *args, **kw ):
+
+ # Derived classes must implement _getTargetClass
+ return self._getTargetClass()( *args, **kw )
+
def _clearTestDirectory( root_path ):
if os.path.exists( root_path ):
@@ -41,7 +44,7 @@
file.close()
return fqpath
-class TestBase( SecurityRequestTest ):
+class FilesystemTestBase( SecurityRequestTest ):
def _makeOne( self, *args, **kw ):
@@ -64,6 +67,37 @@
def _makeFile( self, filename, contents ):
return _makeTestFile( filename, self._PROFILE_PATH, contents )
+
+
+class TarballTester( DOMComparator ):
+
+ def _verifyTarballContents( self, fileish, toc_list ):
+
+ fileish.seek( 0L )
+ tarfile = TarFile.open( 'foo.tar.gz', fileobj=fileish, mode='r:gz' )
+ items = tarfile.getnames()
+ items.sort()
+ toc_list.sort()
+
+ self.assertEqual( len( items ), len( toc_list ) )
+ for i in range( len( items ) ):
+ self.assertEqual( items[ i ], toc_list[ i ] )
+
+ def _verifyTarballEntry( self, fileish, entry_name, data ):
+
+ fileish.seek( 0L )
+ tarfile = TarFile.open( 'foo.tar.gz', fileobj=fileish, mode='r:gz' )
+ extract = tarfile.extractfile( entry_name )
+ found = extract.read()
+ self.assertEqual( found, data )
+
+ def _verifyTarballEntryXML( self, fileish, entry_name, data ):
+
+ fileish.seek( 0L )
+ tarfile = TarFile.open( 'foo.tar.gz', fileobj=fileish, mode='r:gz' )
+ extract = tarfile.extractfile( entry_name )
+ found = extract.read()
+ self._compareDOM( found, data )
def dummy_handler( context ):
=== CMF/CMFSetup/tests/test_context.py 1.2 => 1.3 ===
--- CMF/CMFSetup/tests/test_context.py:1.2 Sun May 23 16:31:43 2004
+++ CMF/CMFSetup/tests/test_context.py Sun May 23 21:52:43 2004
@@ -5,12 +5,15 @@
import unittest
import os
+from StringIO import StringIO
from OFS.Folder import Folder
from Products.CMFCore.tests.base.testcase import SecurityRequestTest
-from common import TestBase
+from common import FilesystemTestBase
+from common import TarballTester
+from common import _makeTestFile
from conformance import ConformsToISetupContext
from conformance import ConformsToIImportContext
from conformance import ConformsToIExportContext
@@ -20,7 +23,7 @@
pass
-class ImportContextTests( TestBase
+class ImportContextTests( FilesystemTestBase
, ConformsToISetupContext
, ConformsToIImportContext
):
@@ -254,7 +257,7 @@
self.failUnless( 'CVS' in names )
-class ExportContextTests( TestBase
+class ExportContextTests( FilesystemTestBase
, ConformsToISetupContext
, ConformsToIExportContext
):
@@ -324,11 +327,73 @@
self.assertEqual( open( fqname, 'rb' ).read(), digits )
+class TarballExportContextTests( FilesystemTestBase
+ , TarballTester
+ , ConformsToISetupContext
+ , ConformsToIExportContext
+ ):
+
+ _PROFILE_PATH = '/tmp/TECT_tests'
+
+ def _getTargetClass( self ):
+
+ from Products.CMFSetup.context import TarballExportContext
+ return TarballExportContext
+
+ def test_writeDataFile_simple( self ):
+
+ from string import printable
+
+ site = DummySite( 'site' ).__of__( self.root )
+ ctx = self._getTargetClass()( site )
+
+ ctx.writeDataFile( 'foo.txt', printable, 'text/plain' )
+
+ fileish = StringIO( ctx.getArchive() )
+
+ self._verifyTarballContents( fileish, [ 'foo.txt' ] )
+ self._verifyTarballEntry( fileish, 'foo.txt', printable )
+
+ def test_writeDataFile_multiple( self ):
+
+ from string import printable
+ from string import digits
+
+ site = DummySite( 'site' ).__of__( self.root )
+ ctx = self._getTargetClass()( site )
+
+ ctx.writeDataFile( 'foo.txt', printable, 'text/plain' )
+ ctx.writeDataFile( 'bar.txt', digits, 'text/plain' )
+
+ fileish = StringIO( ctx.getArchive() )
+
+ self._verifyTarballContents( fileish, [ 'foo.txt', 'bar.txt' ] )
+ self._verifyTarballEntry( fileish, 'foo.txt', printable )
+ self._verifyTarballEntry( fileish, 'bar.txt', digits )
+
+ def test_writeDataFile_subdir( self ):
+
+ from string import printable
+ from string import digits
+
+ site = DummySite( 'site' ).__of__( self.root )
+ ctx = self._getTargetClass()( site )
+
+ ctx.writeDataFile( 'foo.txt', printable, 'text/plain' )
+ ctx.writeDataFile( 'bar/baz.txt', digits, 'text/plain' )
+
+ fileish = StringIO( ctx.getArchive() )
+
+ self._verifyTarballContents( fileish, [ 'foo.txt', 'bar/baz.txt' ] )
+ self._verifyTarballEntry( fileish, 'foo.txt', printable )
+ self._verifyTarballEntry( fileish, 'bar/baz.txt', digits )
+
def test_suite():
return unittest.TestSuite((
unittest.makeSuite( ImportContextTests ),
unittest.makeSuite( ExportContextTests ),
+ unittest.makeSuite( TarballExportContextTests ),
))
if __name__ == '__main__':
=== CMF/CMFSetup/tests/test_tool.py 1.5 => 1.6 ===
--- CMF/CMFSetup/tests/test_tool.py:1.5 Sun May 23 17:59:15 2004
+++ CMF/CMFSetup/tests/test_tool.py Sun May 23 21:52:43 2004
@@ -5,11 +5,14 @@
import unittest
import os
+from StringIO import StringIO
-from common import TestBase
+from common import FilesystemTestBase
+from common import TarballTester
from conformance import ConformsToISetupTool
-class SetupToolTests( TestBase
+class SetupToolTests( FilesystemTestBase
+ , TarballTester
, ConformsToISetupTool
):
@@ -24,7 +27,7 @@
return self._getTargetClass()( *args, **kw )
- def _makeSite( self, title ):
+ def _makeSite( self, title="Don't care" ):
from OFS.Folder import Folder
@@ -46,7 +49,9 @@
self.assertEqual( len( import_registry.listSteps() ), 0 )
export_registry = tool.getExportStepRegistry()
- self.assertEqual( len( export_registry.listSteps() ), 0 )
+ export_steps = export_registry.listSteps()
+ self.assertEqual( len( export_steps ), 1 )
+ self.assertEqual( export_steps[ 0 ], 'step_registries' )
def test_getProfileDirectory_relative_no_product( self ):
@@ -189,8 +194,7 @@
def test_runImportStep_nonesuch( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
@@ -265,8 +269,7 @@
def test_runImportStep_default_purge( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
registry = tool.getImportStepRegistry()
@@ -281,8 +284,7 @@
def test_runImportStep_explicit_purge( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
registry = tool.getImportStepRegistry()
@@ -297,8 +299,7 @@
def test_runImportStep_skip_purge( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
registry = tool.getImportStepRegistry()
@@ -313,8 +314,7 @@
def test_runImportStep_consistent_context( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
@@ -328,8 +328,7 @@
def test_runAllImportSteps_empty( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
result = tool.runAllImportSteps()
@@ -371,8 +370,7 @@
def test_runAllImportSteps_sorted_explicit_purge( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
registry = tool.getImportStepRegistry()
@@ -397,8 +395,7 @@
def test_runAllImportSteps_sorted_skip_purge( self ):
- TITLE = 'original title'
- site = self._makeSite( TITLE )
+ site = self._makeSite()
tool = self._makeOne().__of__( site )
registry = tool.getImportStepRegistry()
@@ -421,6 +418,169 @@
self.assertEqual( result[ 'steps' ][ 2 ], 'dependent' )
self.failIf( site.purged )
+ def test_runExportStep_nonesuch( self ):
+
+ site = self._makeSite()
+ tool = self._makeOne().__of__( site )
+
+ self.assertRaises( ValueError, tool.runExportStep, 'nonesuch' )
+
+ def test_runExportStep_step_registry( self ):
+
+ from test_registry import _EMPTY_IMPORT_XML
+
+ site = self._makeSite()
+ site.portal_setup = self._makeOne()
+ tool = site.portal_setup
+
+ result = tool.runExportStep( 'step_registries' )
+
+ self.assertEqual( len( result[ 'steps' ] ), 1 )
+ self.assertEqual( result[ 'steps' ][ 0 ], 'step_registries' )
+ self.assertEqual( result[ 'messages' ][ 'step_registries' ]
+ , 'Step registries exported'
+ )
+ fileish = StringIO( result[ 'tarball' ] )
+
+ self._verifyTarballContents( fileish, [ 'import_steps.xml'
+ , 'export_steps.xml'
+ ] )
+ self._verifyTarballEntryXML( fileish, 'import_steps.xml'
+ , _EMPTY_IMPORT_XML )
+ self._verifyTarballEntryXML( fileish, 'export_steps.xml'
+ , _DEFAULT_STEP_REGISTRIES_EXPORT_XML )
+
+ def test_runAllExportSteps_default( self ):
+
+ from test_registry import _EMPTY_IMPORT_XML
+
+ site = self._makeSite()
+ site.portal_setup = self._makeOne()
+ tool = site.portal_setup
+
+ result = tool.runAllExportSteps()
+
+ self.assertEqual( len( result[ 'steps' ] ), 1 )
+ self.assertEqual( result[ 'steps' ][ 0 ], 'step_registries' )
+ self.assertEqual( result[ 'messages' ][ 'step_registries' ]
+ , 'Step registries exported'
+ )
+ fileish = StringIO( result[ 'tarball' ] )
+
+ self._verifyTarballContents( fileish, [ 'import_steps.xml'
+ , 'export_steps.xml'
+ ] )
+ self._verifyTarballEntryXML( fileish, 'import_steps.xml'
+ , _EMPTY_IMPORT_XML )
+ self._verifyTarballEntryXML( fileish, 'export_steps.xml'
+ , _DEFAULT_STEP_REGISTRIES_EXPORT_XML )
+
+ def test_runAllExportSteps_extras( self ):
+
+ from test_registry import _EMPTY_IMPORT_XML
+
+ site = self._makeSite()
+ site.portal_setup = self._makeOne()
+ tool = site.portal_setup
+
+ import_reg = tool.getImportStepRegistry()
+ import_reg.registerStep( 'dependable', '1'
+ , _underscoreSiteTitle, ( 'purging', ) )
+ import_reg.registerStep( 'dependent', '1'
+ , _uppercaseSiteTitle, ( 'dependable', ) )
+ import_reg.registerStep( 'purging', '1'
+ , _purgeIfRequired )
+
+ export_reg = tool.getExportStepRegistry()
+ export_reg.registerStep( 'properties'
+ , _exportPropertiesINI )
+
+ result = tool.runAllExportSteps()
+
+ self.assertEqual( len( result[ 'steps' ] ), 2 )
+
+ self.failUnless( 'properties' in result[ 'steps' ] )
+ self.assertEqual( result[ 'messages' ][ 'properties' ]
+ , 'Exported properties'
+ )
+
+ self.failUnless( 'step_registries' in result[ 'steps' ] )
+ self.assertEqual( result[ 'messages' ][ 'step_registries' ]
+ , 'Step registries exported'
+ )
+
+ fileish = StringIO( result[ 'tarball' ] )
+
+ self._verifyTarballContents( fileish, [ 'import_steps.xml'
+ , 'export_steps.xml'
+ , 'properties.ini'
+ ] )
+ self._verifyTarballEntryXML( fileish, 'import_steps.xml'
+ , _EXTRAS_STEP_REGISTRIES_IMPORT_XML )
+ self._verifyTarballEntryXML( fileish, 'export_steps.xml'
+ , _EXTRAS_STEP_REGISTRIES_EXPORT_XML )
+ self._verifyTarballEntry( fileish, 'properties.ini'
+ , _PROPERTIES_INI % site.title )
+
+
+_DEFAULT_STEP_REGISTRIES_EXPORT_XML = """\
+<?xml version="1.0"?>
+<export-steps>
+ <export-step id="step_registries"
+ handler="Products.CMFSetup.tool.exportStepRegistries"
+ title="Export import / export steps.">
+
+ </export-step>
+</export-steps>
+"""
+
+_EXTRAS_STEP_REGISTRIES_EXPORT_XML = """\
+<?xml version="1.0"?>
+<export-steps>
+ <export-step id="properties"
+ handler="Products.CMFSetup.tests.test_tool._exportPropertiesINI"
+ title="properties">
+
+ </export-step>
+ <export-step id="step_registries"
+ handler="Products.CMFSetup.tool.exportStepRegistries"
+ title="Export import / export steps.">
+
+ </export-step>
+</export-steps>
+"""
+
+_EXTRAS_STEP_REGISTRIES_IMPORT_XML = """\
+<?xml version="1.0"?>
+<import-steps>
+ <import-step id="dependable"
+ version="1"
+ handler="Products.CMFSetup.tests.test_tool._underscoreSiteTitle"
+ title="dependable">
+ <dependency step="purging" />
+
+ </import-step>
+ <import-step id="dependent"
+ version="1"
+ handler="Products.CMFSetup.tests.test_tool._uppercaseSiteTitle"
+ title="dependent">
+ <dependency step="dependable" />
+
+ </import-step>
+ <import-step id="purging"
+ version="1"
+ handler="Products.CMFSetup.tests.test_tool._purgeIfRequired"
+ title="purging">
+
+ </import-step>
+</import-steps>
+"""
+
+_PROPERTIES_INI = """\
+[Default]
+Title=%s
+"""
+
def _underscoreSiteTitle( context ):
site = context.getSite()
@@ -438,6 +598,15 @@
site = context.getSite()
purged = site.purged = context.shouldPurge()
return purged and 'Purged' or 'Unpurged'
+
+def _exportPropertiesINI( context ):
+
+ site = context.getSite()
+ text = _PROPERTIES_INI % site.title
+
+ context.writeDataFile( 'properties.ini', text, 'text/plain' )
+
+ return 'Exported properties'
def test_suite():
return unittest.TestSuite((
More information about the CMF-checkins
mailing list