Back to index

plone3  3.1.7
test_exportimport.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 """ Unit tests for GenericSetup export / import support for topics / criteria.
00014 
00015 $Id: test_exportimport.py 71240 2006-11-21 13:14:15Z yuppie $
00016 """
00017 
00018 import unittest
00019 
00020 from DateTime.DateTime import DateTime
00021 
00022 from Products.GenericSetup.testing import ExportImportZCMLLayer
00023 from Products.GenericSetup.tests.conformance \
00024         import ConformsToIFilesystemExporter
00025 from Products.GenericSetup.tests.conformance \
00026         import ConformsToIFilesystemImporter
00027 from Products.GenericSetup.tests.common import BaseRegistryTests
00028 from Products.GenericSetup.tests.common import DummyExportContext
00029 from Products.GenericSetup.tests.common import DummyImportContext
00030 
00031 _DATE_STR = '2005-11-20T12:00:00Z'
00032 _CRITERIA_DATA = (
00033     ('a', 'String Criterion', {'value': 'A'}),
00034     ('b', 'Integer Criterion', {'value': 3, 'direction': 'min'}),
00035     ('c', 'Friendly Date Criterion', {'value': DateTime(_DATE_STR),
00036                                       'operation': 'min',
00037                                       'daterange': 'old',
00038                                      }),
00039     ('d', 'List Criterion', {'value': ('D', 'd'), 'operator': 'or'}),
00040     ('e', 'Sort Criterion', {'reversed': 0}),
00041 )
00042 
00043 
00044 class TopicExportImportTests(BaseRegistryTests,
00045                              ConformsToIFilesystemExporter,
00046                              ConformsToIFilesystemImporter,
00047                             ):
00048 
00049     layer = ExportImportZCMLLayer
00050 
00051     def _getTargetClass(self):
00052         from Products.CMFTopic.exportimport import TopicExportImport
00053         return TopicExportImport
00054 
00055     def _makeOne(self, context, *args, **kw):
00056         return self._getTargetClass()(context, *args, **kw)
00057 
00058     def _makeTopic(self, id, with_criteria=False):
00059         from Products.CMFTopic.Topic import Topic
00060         topic = Topic(id)
00061 
00062         if with_criteria:
00063             for field, c_type, attrs in _CRITERIA_DATA:
00064                 topic.addCriterion(field, c_type)
00065                 criterion = topic.getCriterion(field)
00066                 criterion.edit(**attrs)
00067 
00068         return topic
00069 
00070     def test_listExportableItems(self):
00071         topic = self._makeTopic('lEI', False).__of__(self.root)
00072         adapter = self._makeOne(topic)
00073 
00074         self.assertEqual(len(adapter.listExportableItems()), 0)
00075         topic.addCriterion('field_a', 'String Criterion')
00076         self.assertEqual(len(adapter.listExportableItems()), 0)
00077 
00078     def test__getExportInfo_empty(self):
00079         topic = self._makeTopic('empty', False).__of__(self.root)
00080         adapter = self._makeOne(topic)
00081 
00082         info = adapter._getExportInfo()
00083         self.assertEqual(len(info['criteria']), 0)
00084 
00085     def test_export_empty(self):
00086         topic = self._makeTopic('empty', False).__of__(self.root)
00087         adapter = self._makeOne(topic)
00088 
00089         context = DummyExportContext(topic)
00090         adapter.export(context, 'test', False)
00091 
00092         self.assertEqual( len( context._wrote ), 2 )
00093         filename, text, content_type = context._wrote[ 0 ]
00094         self.assertEqual( filename, 'test/empty/.objects' )
00095         self.assertEqual( text, '' )
00096         self.assertEqual( content_type, 'text/comma-separated-values' )
00097 
00098         filename, text, content_type = context._wrote[ 1 ]
00099         self.assertEqual( filename, 'test/empty/criteria.xml' )
00100         self._compareDOM( text, _EMPTY_TOPIC_CRITERIA )
00101         self.assertEqual( content_type, 'text/xml' )
00102 
00103     def test__getExportInfo_with_criteria(self):
00104         topic = self._makeTopic('with_criteria', True).__of__(self.root)
00105         adapter = self._makeOne(topic)
00106 
00107         info = adapter._getExportInfo()
00108         self.assertEqual(len(info['criteria']), len(_CRITERIA_DATA))
00109 
00110         for found, expected in zip(info['criteria'], _CRITERIA_DATA):
00111             attributes = expected[2]
00112             for k, v in attributes.items():
00113                 if type(v) in (list, tuple):
00114                     attributes[k] = ','.join(v)
00115 
00116             self.assertEqual(found['criterion_id'], 'crit__%s' % expected[0])
00117             self.assertEqual(found['type'], expected[1])
00118             self.assertEqual(found['field'], expected[0])
00119             self.assertEqual(dict(found['attributes']), attributes)
00120 
00121     def test_export_with_string_criterion(self):
00122         topic = self._makeTopic('with_string', False).__of__(self.root)
00123         data = _CRITERIA_DATA[0]
00124         topic.addCriterion(data[0], data[1])
00125         topic.getCriterion(data[0]).edit(**data[2])
00126         adapter = self._makeOne(topic)
00127 
00128         context = DummyExportContext(topic)
00129         adapter.export(context, 'test', False)
00130 
00131         self.assertEqual( len( context._wrote ), 2 )
00132         filename, text, content_type = context._wrote[ 0 ]
00133         self.assertEqual( filename, 'test/with_string/.objects' )
00134         self.assertEqual( text, '' )
00135         self.assertEqual( content_type, 'text/comma-separated-values' )
00136 
00137         filename, text, content_type = context._wrote[ 1 ]
00138         self.assertEqual( filename, 'test/with_string/criteria.xml' )
00139         self._compareDOM( text, _STRING_TOPIC_CRITERIA )
00140         self.assertEqual( content_type, 'text/xml' )
00141 
00142     def test_export_with_integer_criterion(self):
00143         topic = self._makeTopic('with_integer', False).__of__(self.root)
00144         data = _CRITERIA_DATA[1]
00145         topic.addCriterion(data[0], data[1])
00146         topic.getCriterion(data[0]).edit(**data[2])
00147         adapter = self._makeOne(topic)
00148 
00149         context = DummyExportContext(topic)
00150         adapter.export(context, 'test', False)
00151 
00152         self.assertEqual( len( context._wrote ), 2 )
00153         filename, text, content_type = context._wrote[ 0 ]
00154         self.assertEqual( filename, 'test/with_integer/.objects' )
00155         self.assertEqual( text, '' )
00156         self.assertEqual( content_type, 'text/comma-separated-values' )
00157 
00158         filename, text, content_type = context._wrote[ 1 ]
00159         self.assertEqual( filename, 'test/with_integer/criteria.xml' )
00160         self._compareDOM( text, _INTEGER_TOPIC_CRITERIA )
00161         self.assertEqual( content_type, 'text/xml' )
00162 
00163     def test_export_with_date_criterion(self):
00164         topic = self._makeTopic('with_date', False).__of__(self.root)
00165         data = _CRITERIA_DATA[2]
00166         topic.addCriterion(data[0], data[1])
00167         topic.getCriterion(data[0]).edit(**data[2])
00168         adapter = self._makeOne(topic)
00169 
00170         context = DummyExportContext(topic)
00171         adapter.export(context, 'test', False)
00172 
00173         self.assertEqual( len( context._wrote ), 2 )
00174         filename, text, content_type = context._wrote[ 0 ]
00175         self.assertEqual( filename, 'test/with_date/.objects' )
00176         self.assertEqual( text, '' )
00177         self.assertEqual( content_type, 'text/comma-separated-values' )
00178 
00179         filename, text, content_type = context._wrote[ 1 ]
00180         self.assertEqual( filename, 'test/with_date/criteria.xml' )
00181         self._compareDOM( text, _DATE_TOPIC_CRITERIA )
00182         self.assertEqual( content_type, 'text/xml' )
00183 
00184     def test_export_with_list_criterion(self):
00185         topic = self._makeTopic('with_list', False).__of__(self.root)
00186         data = _CRITERIA_DATA[3]
00187         topic.addCriterion(data[0], data[1])
00188         topic.getCriterion(data[0]).edit(**data[2])
00189         adapter = self._makeOne(topic)
00190 
00191         context = DummyExportContext(topic)
00192         adapter.export(context, 'test', False)
00193 
00194         self.assertEqual( len( context._wrote ), 2 )
00195         filename, text, content_type = context._wrote[ 0 ]
00196         self.assertEqual( filename, 'test/with_list/.objects' )
00197         self.assertEqual( text, '' )
00198         self.assertEqual( content_type, 'text/comma-separated-values' )
00199 
00200         filename, text, content_type = context._wrote[ 1 ]
00201         self.assertEqual( filename, 'test/with_list/criteria.xml' )
00202         self._compareDOM( text, _LIST_TOPIC_CRITERIA )
00203         self.assertEqual( content_type, 'text/xml' )
00204 
00205     def test_export_with_sort_criterion(self):
00206         topic = self._makeTopic('with_sort', False).__of__(self.root)
00207         data = _CRITERIA_DATA[4]
00208         topic.addCriterion(data[0], data[1])
00209         topic.getCriterion(data[0]).edit(**data[2])
00210         adapter = self._makeOne(topic)
00211 
00212         context = DummyExportContext(topic)
00213         adapter.export(context, 'test', False)
00214 
00215         self.assertEqual( len( context._wrote ), 2 )
00216         filename, text, content_type = context._wrote[ 0 ]
00217         self.assertEqual( filename, 'test/with_sort/.objects' )
00218         self.assertEqual( text, '' )
00219         self.assertEqual( content_type, 'text/comma-separated-values' )
00220 
00221         filename, text, content_type = context._wrote[ 1 ]
00222         self.assertEqual( filename, 'test/with_sort/criteria.xml' )
00223         self._compareDOM( text, _SORT_TOPIC_CRITERIA )
00224         self.assertEqual( content_type, 'text/xml' )
00225 
00226     def test_export_with_mixed_criteria(self):
00227         topic = self._makeTopic('with_mixed', False).__of__(self.root)
00228         for index in 0, 2, 4:
00229             data = _CRITERIA_DATA[index]
00230             topic.addCriterion(data[0], data[1])
00231             topic.getCriterion(data[0]).edit(**data[2])
00232         adapter = self._makeOne(topic)
00233 
00234         context = DummyExportContext(topic)
00235         adapter.export(context, 'test', False)
00236 
00237         self.assertEqual( len( context._wrote ), 2 )
00238         filename, text, content_type = context._wrote[ 0 ]
00239         self.assertEqual( filename, 'test/with_mixed/.objects' )
00240         self.assertEqual( text, '' )
00241         self.assertEqual( content_type, 'text/comma-separated-values' )
00242 
00243         filename, text, content_type = context._wrote[ 1 ]
00244         self.assertEqual( filename, 'test/with_mixed/criteria.xml' )
00245         self._compareDOM( text, _MIXED_TOPIC_CRITERIA )
00246         self.assertEqual( content_type, 'text/xml' )
00247 
00248     def test_import_empty_with_string_criterion(self):
00249         topic = self._makeTopic('empty', False).__of__(self.root)
00250         adapter = self._makeOne(topic)
00251 
00252         context = DummyImportContext(topic, encoding='ascii')
00253         context._files['test/empty/criteria.xml'] = _STRING_TOPIC_CRITERIA
00254 
00255         adapter.import_(context, 'test', False)
00256 
00257         expected = _CRITERIA_DATA[0]
00258         found = topic.listCriteria()
00259         self.assertEqual(len(found), 1)
00260 
00261         criterion = found[0]
00262 
00263         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00264         self.assertEqual(criterion.Type(), expected[1])
00265         self.assertEqual(criterion.Field(), expected[0])
00266         self.assertEqual(criterion.value, expected[2]['value'])
00267 
00268     def test_import_empty_with_integer_criterion(self):
00269         topic = self._makeTopic('empty', False).__of__(self.root)
00270         adapter = self._makeOne(topic)
00271 
00272         context = DummyImportContext(topic, encoding='ascii')
00273         context._files['test/empty/criteria.xml'] = _INTEGER_TOPIC_CRITERIA
00274 
00275         adapter.import_(context, 'test', False)
00276 
00277         expected = _CRITERIA_DATA[1]
00278         found = topic.listCriteria()
00279         self.assertEqual(len(found), 1)
00280 
00281         criterion = found[0]
00282 
00283         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00284         self.assertEqual(criterion.Type(), expected[1])
00285         self.assertEqual(criterion.Field(), expected[0])
00286         self.assertEqual(criterion.value, expected[2]['value'])
00287         self.assertEqual(criterion.direction, expected[2]['direction'])
00288 
00289     def test_import_empty_with_date_criterion(self):
00290         topic = self._makeTopic('empty', False).__of__(self.root)
00291         adapter = self._makeOne(topic)
00292 
00293         context = DummyImportContext(topic, encoding='ascii')
00294         context._files['test/empty/criteria.xml'] = _DATE_TOPIC_CRITERIA
00295 
00296         adapter.import_(context, 'test', False)
00297 
00298         expected = _CRITERIA_DATA[2]
00299         found = topic.listCriteria()
00300         self.assertEqual(len(found), 1)
00301 
00302         criterion = found[0]
00303 
00304         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00305         self.assertEqual(criterion.Type(), expected[1])
00306         self.assertEqual(criterion.Field(), expected[0])
00307         self.assertEqual(criterion.value, expected[2]['value'])
00308         self.assertEqual(criterion.operation, expected[2]['operation'])
00309         self.assertEqual(criterion.daterange, expected[2]['daterange'])
00310 
00311     def test_import_empty_with_list_criterion(self):
00312         topic = self._makeTopic('empty', False).__of__(self.root)
00313         adapter = self._makeOne(topic)
00314 
00315         context = DummyImportContext(topic, encoding='ascii')
00316         context._files['test/empty/criteria.xml'] = _LIST_TOPIC_CRITERIA
00317 
00318         adapter.import_(context, 'test', False)
00319 
00320         expected = _CRITERIA_DATA[3]
00321         found = topic.listCriteria()
00322         self.assertEqual(len(found), 1)
00323 
00324         criterion = found[0]
00325 
00326         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00327         self.assertEqual(criterion.Type(), expected[1])
00328         self.assertEqual(criterion.Field(), expected[0])
00329         self.assertEqual(','.join(criterion.value), expected[2]['value'])
00330         self.assertEqual(criterion.operator, expected[2]['operator'])
00331 
00332     def test_import_empty_with_sort_criterion(self):
00333         topic = self._makeTopic('empty', False).__of__(self.root)
00334         adapter = self._makeOne(topic)
00335 
00336         context = DummyImportContext(topic, encoding='ascii')
00337         context._files['test/empty/criteria.xml'] = _SORT_TOPIC_CRITERIA
00338 
00339         adapter.import_(context, 'test', False)
00340 
00341         expected = _CRITERIA_DATA[4]
00342         found = topic.listCriteria()
00343         self.assertEqual(len(found), 1)
00344 
00345         criterion = found[0]
00346 
00347         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00348         self.assertEqual(criterion.Type(), expected[1])
00349         self.assertEqual(criterion.field, None)
00350         self.assertEqual(criterion.index, expected[0])
00351         self.assertEqual(criterion.reversed, bool(expected[2]['reversed']))
00352 
00353     def test_import_empty_with_mixed_criterion(self):
00354         topic = self._makeTopic('empty', False).__of__(self.root)
00355         adapter = self._makeOne(topic)
00356 
00357         context = DummyImportContext(topic, encoding='ascii')
00358         context._files['test/empty/criteria.xml'] = _MIXED_TOPIC_CRITERIA
00359 
00360         adapter.import_(context, 'test', False)
00361 
00362         found = topic.listCriteria()
00363         self.assertEqual(len(found), 3)
00364 
00365         criterion = found[0]
00366         expected = _CRITERIA_DATA[0]
00367 
00368         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00369         self.assertEqual(criterion.Type(), expected[1])
00370         self.assertEqual(criterion.Field(), expected[0])
00371         self.assertEqual(criterion.value, expected[2]['value'])
00372 
00373         criterion = found[1]
00374         expected = _CRITERIA_DATA[2]
00375 
00376         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00377         self.assertEqual(criterion.Type(), expected[1])
00378         self.assertEqual(criterion.Field(), expected[0])
00379         self.assertEqual(criterion.value, expected[2]['value'])
00380         self.assertEqual(criterion.operation, expected[2]['operation'])
00381         self.assertEqual(criterion.daterange, expected[2]['daterange'])
00382 
00383         criterion = found[2]
00384         expected = _CRITERIA_DATA[4]
00385 
00386         self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
00387         self.assertEqual(criterion.Type(), expected[1])
00388         self.assertEqual(criterion.field, None)
00389         self.assertEqual(criterion.index, expected[0])
00390         self.assertEqual(criterion.reversed, bool(expected[2]['reversed']))
00391 
00392     def test_import_without_purge_leaves_existing_criteria(self):
00393 
00394         topic = self._makeTopic('with_criteria', True).__of__(self.root)
00395         adapter = self._makeOne(topic)
00396 
00397         context = DummyImportContext(topic, purge=False)
00398         context._files['test/with_criteria/criteria.xml'
00399                       ] = _EMPTY_TOPIC_CRITERIA
00400 
00401         self.assertEqual(len(topic.listCriteria()), len(_CRITERIA_DATA))
00402         adapter.import_(context, 'test', False)
00403         self.assertEqual(len(topic.listCriteria()), len(_CRITERIA_DATA))
00404 
00405     def test_import_with_purge_removes_existing_criteria(self):
00406 
00407         topic = self._makeTopic('with_criteria', True).__of__(self.root)
00408         adapter = self._makeOne(topic)
00409 
00410         context = DummyImportContext(topic, purge=True)
00411         context._files['test/with_criteria/criteria.xml'
00412                       ] = _EMPTY_TOPIC_CRITERIA
00413 
00414         self.assertEqual(len(topic.listCriteria()), len(_CRITERIA_DATA))
00415         adapter.import_(context, 'test', False)
00416         self.assertEqual(len(topic.listCriteria()), 0)
00417 
00418 _EMPTY_TOPIC_CRITERIA = """\
00419 <?xml version="1.0" ?>
00420 <criteria>
00421 </criteria>
00422 """
00423 
00424 _STRING_TOPIC_CRITERIA = """\
00425 <?xml version="1.0" ?>
00426 <criteria>
00427  <criterion
00428     criterion_id="crit__a"
00429     type="String Criterion"
00430     field="a">
00431   <attribute name="value" value="A" />
00432  </criterion>
00433 </criteria>
00434 """
00435 
00436 _INTEGER_TOPIC_CRITERIA = """\
00437 <?xml version="1.0" ?>
00438 <criteria>
00439  <criterion
00440     criterion_id="crit__b"
00441     type="Integer Criterion"
00442     field="b">
00443   <attribute name="value" value="3" />
00444   <attribute name="direction" value="min" />
00445  </criterion>
00446 </criteria>
00447 """
00448 
00449 _DATE_TOPIC_CRITERIA = """\
00450 <?xml version="1.0" ?>
00451 <criteria>
00452  <criterion
00453     criterion_id="crit__c"
00454     type="Friendly Date Criterion"
00455     field="c">
00456   <attribute name="value" value="%s" />
00457   <attribute name="operation" value="min" />
00458   <attribute name="daterange" value="old" />
00459  </criterion>
00460 </criteria>
00461 """ % int(DateTime(_DATE_STR))
00462 
00463 _LIST_TOPIC_CRITERIA = """\
00464 <?xml version="1.0" ?>
00465 <criteria>
00466  <criterion
00467     criterion_id="crit__d"
00468     type="List Criterion"
00469     field="d">
00470   <attribute name="value" value="D,d" />
00471   <attribute name="operator" value="or" />
00472  </criterion>
00473 </criteria>
00474 """
00475 
00476 _SORT_TOPIC_CRITERIA = """\
00477 <?xml version="1.0" ?>
00478 <criteria>
00479  <criterion
00480     criterion_id="crit__e"
00481     type="Sort Criterion"
00482     field="e">
00483   <attribute name="reversed" value="False" />
00484  </criterion>
00485 </criteria>
00486 """
00487 
00488 _MIXED_TOPIC_CRITERIA = """\
00489 <?xml version="1.0" ?>
00490 <criteria>
00491  <criterion
00492     criterion_id="crit__a"
00493     type="String Criterion"
00494     field="a">
00495   <attribute name="value" value="A" />
00496  </criterion>
00497  <criterion
00498     criterion_id="crit__c"
00499     type="Friendly Date Criterion"
00500     field="c">
00501   <attribute name="value" value="%s" />
00502   <attribute name="operation" value="min" />
00503   <attribute name="daterange" value="old" />
00504  </criterion>
00505  <criterion
00506     criterion_id="crit__e"
00507     type="Sort Criterion"
00508     field="e">
00509   <attribute name="reversed" value="False" />
00510  </criterion>
00511 </criteria>
00512 """ % int(DateTime(_DATE_STR))
00513 
00514 
00515 def test_suite():
00516     return unittest.TestSuite((
00517         unittest.makeSuite(TopicExportImportTests),
00518         ))
00519 
00520 if __name__ == '__main__':
00521     from Products.GenericSetup.testing import run
00522     run(test_suite())