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 """ZCatalog export / import support unit tests.
00014 
00015 $Id: test_exportimport.py 76398 2007-06-06 11:28:46Z jens $
00016 """
00017 
00018 import unittest
00019 from Testing import ZopeTestCase
00020 ZopeTestCase.installProduct('ZCTextIndex', 1)
00021 
00022 from zope.component import getMultiAdapter
00023 
00024 from Products.GenericSetup.interfaces import IBody
00025 from Products.GenericSetup.testing import BodyAdapterTestCase
00026 from Products.GenericSetup.testing import DummySetupEnviron
00027 from Products.GenericSetup.testing import ExportImportZCMLLayer
00028 
00029 
00030 class _extra:
00031 
00032     pass
00033 
00034 
00035 _CATALOG_BODY = """\
00036 <?xml version="1.0"?>
00037 <object name="foo_catalog" meta_type="ZCatalog">
00038  <property name="title"></property>
00039  <object name="foo_plexicon" meta_type="ZCTextIndex Lexicon">
00040   <element name="Whitespace splitter" group="Word Splitter"/>
00041   <element name="Case Normalizer" group="Case Normalizer"/>
00042   <element name="Remove listed stop words only" group="Stop Words"/>
00043  </object>
00044 %s <index name="foo_date" meta_type="DateIndex">
00045   <property name="index_naive_time_as_local">True</property>
00046  </index>
00047  <index name="foo_daterange" meta_type="DateRangeIndex" since_field="bar"
00048     until_field="baz"/>
00049  <index name="foo_field" meta_type="FieldIndex">
00050   <indexed_attr value="bar"/>
00051  </index>
00052  <index name="foo_keyword" meta_type="KeywordIndex">
00053   <indexed_attr value="bar"/>
00054  </index>
00055  <index name="foo_path" meta_type="PathIndex"/>
00056 %s <index name="foo_topic" meta_type="TopicIndex">
00057   <filtered_set name="bar" meta_type="PythonFilteredSet" expression="True"/>
00058   <filtered_set name="baz" meta_type="PythonFilteredSet" expression="False"/>
00059  </index>
00060  <index name="foo_zctext" meta_type="ZCTextIndex">
00061   <indexed_attr value="foo_zctext"/>
00062   <extra name="index_type" value="Okapi BM25 Rank"/>
00063   <extra name="lexicon_id" value="foo_plexicon"/>
00064  </index>
00065 %s <column value="eggs"/>
00066  <column value="spam"/>
00067 </object>
00068 """
00069 
00070 _CATALOG_UPDATE_BODY = """\
00071 <?xml version="1.0"?>
00072 <object name="foo_catalog">
00073  <object name="foo_vocabulary" remove="True"/>
00074  <index name="foo_text" remove="True"/>
00075  <index name="foo_text" meta_type="ZCTextIndex">
00076   <indexed_attr value="foo_text"/>
00077   <extra name="index_type" value="Okapi BM25 Rank"/>
00078   <extra name="lexicon_id" value="foo_plexicon"/>
00079  </index>
00080  <index name="non_existing" remove="True"/>
00081  <column value="non_existing" remove="True"/>
00082  <column value="bacon" remove="True"/>
00083 </object>
00084 """
00085 
00086 # START SITUATION
00087 #
00088 # The catalog starts out as the _CATALOG_BODY above with the following
00089 # xml snippets inserted.
00090 
00091 _VOCABULARY_XML = """\
00092  <object name="foo_vocabulary" meta_type="Vocabulary" deprecated="True"/>
00093 """
00094 
00095 _TEXT_XML = """\
00096  <index name="foo_text" meta_type="TextIndex" deprecated="True"/>
00097 """
00098 
00099 _COLUMN_XML = """\
00100  <column value="bacon"/>
00101 """
00102 
00103 # END SITUATION
00104 #
00105 # The catalog ends as the _CATALOG_BODY above with the following
00106 # xml snippets and some empty strings inserted.
00107 
00108 _ZCTEXT_XML = """\
00109  <index name="foo_text" meta_type="ZCTextIndex">
00110   <indexed_attr value="foo_text"/>
00111   <extra name="index_type" value="Okapi BM25 Rank"/>
00112   <extra name="lexicon_id" value="foo_plexicon"/>
00113  </index>
00114 """
00115 
00116 class ZCatalogXMLAdapterTests(BodyAdapterTestCase):
00117 
00118     layer = ExportImportZCMLLayer
00119 
00120     def _getTargetClass(self):
00121         from Products.GenericSetup.ZCatalog.exportimport \
00122                 import ZCatalogXMLAdapter
00123 
00124         return ZCatalogXMLAdapter
00125 
00126     def _populate(self, obj):
00127         from Products.ZCTextIndex.Lexicon import CaseNormalizer
00128         from Products.ZCTextIndex.Lexicon import Splitter
00129         from Products.ZCTextIndex.Lexicon import StopWordRemover
00130         from Products.ZCTextIndex.ZCTextIndex import PLexicon
00131 
00132         obj._setObject('foo_plexicon', PLexicon('foo_plexicon'))
00133         lex = obj.foo_plexicon
00134         lex._pipeline = (Splitter(), CaseNormalizer(), StopWordRemover())
00135 
00136         obj.addIndex('foo_date', 'DateIndex')
00137 
00138         obj.addIndex('foo_daterange', 'DateRangeIndex')
00139         idx = obj._catalog.getIndex('foo_daterange')
00140         idx._edit('bar', 'baz')
00141 
00142         obj.addIndex('foo_field', 'FieldIndex')
00143         idx = obj._catalog.getIndex('foo_field')
00144         idx.indexed_attrs = ('bar',)
00145 
00146         obj.addIndex('foo_keyword', 'KeywordIndex')
00147         idx = obj._catalog.getIndex('foo_keyword')
00148         idx.indexed_attrs = ('bar',)
00149 
00150         obj.addIndex('foo_path', 'PathIndex')
00151 
00152         obj.addIndex('foo_topic', 'TopicIndex')
00153         idx = obj._catalog.getIndex('foo_topic')
00154         idx.addFilteredSet('bar', 'PythonFilteredSet', 'True')
00155         idx.addFilteredSet('baz', 'PythonFilteredSet', 'False')
00156 
00157         extra = _extra()
00158         extra.lexicon_id = 'foo_plexicon'
00159         extra.index_type = 'Okapi BM25 Rank'
00160         obj.addIndex('foo_zctext', 'ZCTextIndex', extra)
00161 
00162         obj.addColumn('spam')
00163         obj.addColumn('eggs')
00164 
00165     def _populate_special(self, obj):
00166         from Products.PluginIndexes.TextIndex.Vocabulary import Vocabulary
00167 
00168         self._populate(self._obj)
00169         obj._setObject('foo_vocabulary', Vocabulary('foo_vocabulary'))
00170         obj.addIndex('foo_text', 'TextIndex')
00171         obj.addColumn('bacon')
00172 
00173     def setUp(self):
00174         from Products.ZCatalog.ZCatalog import ZCatalog
00175 
00176         BodyAdapterTestCase.setUp(self)
00177         self._obj = ZCatalog('foo_catalog')
00178         self._BODY = _CATALOG_BODY % ('', '', '')
00179 
00180     def test_body_get_special(self):
00181         # Assert that the catalog starts out the way we expect it to.
00182         self._populate_special(self._obj)
00183         context = DummySetupEnviron()
00184         adapted = getMultiAdapter((self._obj, context), IBody)
00185         self.assertEqual(adapted.body,
00186                          _CATALOG_BODY % (_VOCABULARY_XML, _TEXT_XML, _COLUMN_XML))
00187 
00188     def test_body_set_update(self):
00189         # Assert that the catalog ends up the way we expect it to.
00190         self._populate_special(self._obj)
00191         context = DummySetupEnviron()
00192         context._should_purge = False
00193         adapted = getMultiAdapter((self._obj, context), IBody)
00194         adapted.body = _CATALOG_UPDATE_BODY
00195         self.assertEqual(adapted.body, _CATALOG_BODY % ('', _ZCTEXT_XML, ''))
00196 
00197 
00198 def test_suite():
00199     return unittest.TestSuite((
00200         unittest.makeSuite(ZCatalogXMLAdapterTests),
00201         ))
00202 
00203 if __name__ == '__main__':
00204     from Products.GenericSetup.testing import run
00205     run(test_suite())