Back to index

plone3  3.1.7
test_catalog.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 """Catalog tool setup handler unit tests.
00014 
00015 $Id: test_catalog.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 import unittest
00019 from Testing import ZopeTestCase
00020 ZopeTestCase.installProduct('ZCTextIndex', 1)
00021 
00022 from OFS.Folder import Folder
00023 from Products.ZCTextIndex.Lexicon import CaseNormalizer
00024 from Products.ZCTextIndex.Lexicon import Splitter
00025 from Products.ZCTextIndex.Lexicon import StopWordRemover
00026 from Products.ZCTextIndex.ZCTextIndex import PLexicon
00027 
00028 from Products.GenericSetup.tests.common import BaseRegistryTests
00029 from Products.GenericSetup.tests.common import DummyExportContext
00030 from Products.GenericSetup.tests.common import DummyImportContext
00031 
00032 from Products.CMFCore.CatalogTool import CatalogTool
00033 from Products.CMFCore.testing import ExportImportZCMLLayer
00034 
00035 _EMPTY_EXPORT = """\
00036 <?xml version="1.0"?>
00037 <object meta_type="CMF Catalog" name="portal_catalog">
00038  <property name="title"/>
00039 </object>
00040 """
00041 
00042 _NORMAL_EXPORT = """\
00043 <?xml version="1.0"?>
00044 <object meta_type="CMF Catalog" name="portal_catalog">
00045  <property name="title"/>
00046  <object name="foo_plexicon" meta_type="ZCTextIndex Lexicon">
00047   <element name="Whitespace splitter" group="Word Splitter"/>
00048   <element name="Case Normalizer" group="Case Normalizer"/>
00049   <element name="Remove listed stop words only" group="Stop Words"/>
00050  </object>
00051  <index name="foo_zctext" meta_type="ZCTextIndex">
00052   <indexed_attr value="foo_zctext"/>
00053   <extra name="index_type" value="Okapi BM25 Rank"/>
00054   <extra name="lexicon_id" value="foo_plexicon"/>
00055  </index>
00056  <column value="foo_zctext"/>
00057 </object>
00058 """
00059 
00060 _UPDATE_IMPORT = """\
00061 <?xml version="1.0"?>
00062 <object meta_type="CMF Catalog" name="portal_catalog">
00063  <index name="foo_date" meta_type="DateIndex">
00064   <property name="index_naive_time_as_local">True</property>
00065  </index>
00066  <column value="foo_date"/>
00067 </object>
00068 """
00069 
00070 
00071 class _extra:
00072 
00073     pass
00074 
00075 
00076 class _CatalogToolSetup(BaseRegistryTests):
00077 
00078     def _initSite(self, foo=2):
00079         site = self.root.site = Folder(id='site')
00080         ctool = site.portal_catalog = CatalogTool()
00081 
00082         for obj_id in ctool.objectIds():
00083             ctool._delObject(obj_id)
00084         for idx_id in ctool.indexes():
00085             ctool.delIndex(idx_id)
00086         for col in ctool.schema()[:]:
00087             ctool.delColumn(col)
00088 
00089         if foo > 0:
00090             ctool._setObject('foo_plexicon', PLexicon('foo_plexicon'))
00091             lex = ctool.foo_plexicon
00092             lex._pipeline = (Splitter(), CaseNormalizer(), StopWordRemover())
00093 
00094             extra = _extra()
00095             extra.lexicon_id = 'foo_plexicon'
00096             extra.index_type = 'Okapi BM25 Rank'
00097             ctool.addIndex('foo_zctext', 'ZCTextIndex', extra)
00098 
00099             ctool.addColumn('foo_zctext')
00100 
00101         return site
00102 
00103 
00104 class exportCatalogToolTests(_CatalogToolSetup):
00105 
00106     layer = ExportImportZCMLLayer
00107 
00108     def test_unchanged(self):
00109         from Products.CMFCore.exportimport.catalog import exportCatalogTool
00110 
00111         site = self._initSite(0)
00112         context = DummyExportContext(site)
00113         exportCatalogTool(context)
00114 
00115         self.assertEqual(len(context._wrote), 1)
00116         filename, text, content_type = context._wrote[0]
00117         self.assertEqual(filename, 'catalog.xml')
00118         self._compareDOM(text, _EMPTY_EXPORT)
00119         self.assertEqual(content_type, 'text/xml')
00120 
00121     def test_normal(self):
00122         from Products.CMFCore.exportimport.catalog import exportCatalogTool
00123 
00124         site = self._initSite(2)
00125         context = DummyExportContext(site)
00126         exportCatalogTool(context)
00127 
00128         self.assertEqual(len(context._wrote), 1)
00129         filename, text, content_type = context._wrote[0]
00130         self.assertEqual(filename, 'catalog.xml')
00131         self._compareDOM(text, _NORMAL_EXPORT)
00132         self.assertEqual(content_type, 'text/xml')
00133 
00134 
00135 class importCatalogToolTests(_CatalogToolSetup):
00136 
00137     layer = ExportImportZCMLLayer
00138 
00139     def test_empty_purge(self):
00140         from Products.CMFCore.exportimport.catalog import importCatalogTool
00141 
00142         site = self._initSite(2)
00143         ctool = site.portal_catalog
00144 
00145         self.assertEqual(len(ctool.objectIds()), 1)
00146         self.assertEqual(len(ctool.indexes()), 1)
00147         self.assertEqual(len(ctool.schema()), 1)
00148 
00149         context = DummyImportContext(site, True)
00150         context._files['catalog.xml'] = _EMPTY_EXPORT
00151         importCatalogTool(context)
00152 
00153         self.assertEqual(len(ctool.objectIds()), 0)
00154         self.assertEqual(len(ctool.indexes()), 0)
00155         self.assertEqual(len(ctool.schema()), 0)
00156 
00157     def test_empty_update(self):
00158         from Products.CMFCore.exportimport.catalog import importCatalogTool
00159 
00160         site = self._initSite(2)
00161         ctool = site.portal_catalog
00162 
00163         self.assertEqual(len(ctool.objectIds()), 1)
00164         self.assertEqual(len(ctool.indexes()), 1)
00165         self.assertEqual(len(ctool.schema()), 1)
00166 
00167         context = DummyImportContext(site, False)
00168         context._files['catalog.xml'] = _EMPTY_EXPORT
00169         importCatalogTool(context)
00170 
00171         self.assertEqual(len(ctool.objectIds()), 1)
00172         self.assertEqual(len(ctool.indexes()), 1)
00173         self.assertEqual(len(ctool.schema()), 1)
00174 
00175     def test_normal_purge(self):
00176         from Products.CMFCore.exportimport.catalog import exportCatalogTool
00177         from Products.CMFCore.exportimport.catalog import importCatalogTool
00178 
00179         site = self._initSite(2)
00180         ctool = site.portal_catalog
00181 
00182         self.assertEqual(len(ctool.objectIds()), 1)
00183         self.assertEqual(len(ctool.indexes()), 1)
00184         self.assertEqual(len(ctool.schema()), 1)
00185 
00186         context = DummyImportContext(site, True)
00187         context._files['catalog.xml'] = _NORMAL_EXPORT
00188         importCatalogTool(context)
00189 
00190         self.assertEqual(len(ctool.objectIds()), 1)
00191         self.assertEqual(len(ctool.indexes()), 1)
00192         self.assertEqual(len(ctool.schema()), 1)
00193 
00194         # complete the roundtrip
00195         context = DummyExportContext(site)
00196         exportCatalogTool(context)
00197 
00198         self.assertEqual(len(context._wrote), 1)
00199         filename, text, content_type = context._wrote[0]
00200         self.assertEqual(filename, 'catalog.xml')
00201         self._compareDOM(text, _NORMAL_EXPORT)
00202         self.assertEqual(content_type, 'text/xml')
00203 
00204     def test_normal_update(self):
00205         from Products.CMFCore.exportimport.catalog import importCatalogTool
00206 
00207         site = self._initSite(2)
00208         ctool = site.portal_catalog
00209 
00210         self.assertEqual(len(ctool.objectIds()), 1)
00211         self.assertEqual(len(ctool.indexes()), 1)
00212         self.assertEqual(len(ctool.schema()), 1)
00213 
00214         context = DummyImportContext(site, False)
00215         context._files['catalog.xml'] = _UPDATE_IMPORT
00216         importCatalogTool(context)
00217 
00218         self.assertEqual(len(ctool.objectIds()), 1)
00219         self.assertEqual(len(ctool.indexes()), 2)
00220         self.assertEqual(len(ctool.schema()), 2)
00221 
00222 
00223 def test_suite():
00224     return unittest.TestSuite((
00225         unittest.makeSuite(exportCatalogToolTests),
00226         unittest.makeSuite(importCatalogToolTests),
00227         ))
00228 
00229 if __name__ == '__main__':
00230     from Products.CMFCore.testing import run
00231     run(test_suite())