Back to index

plone3  3.1.7
test_topictool.py
Go to the documentation of this file.
00001 
00002 from Testing import ZopeTestCase # side effect import. leave it here.
00003 from Products.ATContentTypes.tests import atcttestcase
00004 
00005 from Products.CMFCore.utils import getToolByName
00006 from Products.CMFPlone.CatalogTool import CatalogTool
00007 from Products.ATContentTypes.config import TOOLNAME
00008 from Products.ATContentTypes.interfaces import IATCTTopicsTool
00009 from Interface.Verify import verifyObject
00010 
00011 # z3 imports
00012 from Products.ATContentTypes.interface import IATCTTopicsTool as Z3IATCTTopicsTool
00013 from zope.interface.verify import verifyObject as Z3verifyObject
00014 
00015 tests = []
00016 index_def = {'index'        : 'end',
00017              'friendlyName' : 'End Date For Test',
00018              'description'  : 'This is an end Date',
00019              'criteria'     : ['ATDateCriteria','ATDateRangeCriteria']
00020             }
00021 meta_def =  {'metadata'        : 'ModificationDate',
00022              'friendlyName' : 'Modification Date For Test',
00023              'description'  : ''
00024             }
00025 
00026 class TestTool(atcttestcase.ATCTSiteTestCase):
00027 
00028     def afterSetUp(self):
00029         self.tool = getattr(self.portal.aq_explicit, TOOLNAME)
00030 
00031     def test_interface(self):
00032         self.failUnless(IATCTTopicsTool.isImplementedBy(self.tool))
00033         self.failUnless(verifyObject(IATCTTopicsTool, self.tool))
00034 
00035     def test_Z3interface(self):
00036         iface = Z3IATCTTopicsTool
00037         self.failUnless(Z3verifyObject(iface,self.tool))
00038  
00039     #Index tests
00040     def test_add_index(self):
00041         t = self.tool
00042         t.addIndex(enabled = True, **index_def)
00043         index = t.getIndex(index_def['index'])
00044         self.failUnlessEqual(index.index, index_def['index'])
00045         self.failUnlessEqual(index.friendlyName, index_def['friendlyName'])
00046         self.failUnlessEqual(index.description, index_def['description'])
00047         # Only need to test truth not actual value
00048         self.failUnless(index.enabled)
00049         self.failUnlessEqual(index.criteria, tuple(index_def['criteria']))
00050 
00051         self.failUnless(index in t.getEnabledIndexes())
00052         self.failUnless(index_def['index'] in [a[0] for a in t.getEnabledFields()])
00053         self.failUnless(index_def['index'] in t.getIndexDisplay(True).keys())
00054         self.failUnless(index_def['friendlyName'] in t.getIndexDisplay(True).values())
00055         self.failUnless(index_def['index'] in t.getIndexes(1))
00056         
00057     def test_disable_index(self):
00058         t = self.tool
00059         t.addIndex(enabled = False, **index_def)
00060         index = t.getIndex(index_def['index'])
00061         self.failUnlessEqual(index.index, index_def['index'])
00062         self.failUnlessEqual(index.friendlyName, index_def['friendlyName'])
00063         self.failUnlessEqual(index.description, index_def['description'])
00064         # Only need to test truth not actual value
00065         self.failIf(index.enabled)
00066         self.failUnlessEqual(index.criteria, tuple(index_def['criteria']))
00067 
00068         self.failIf(index in t.getEnabledIndexes())
00069         self.failIf(index_def['index'] in [a[0] for a in t.getEnabledFields()])
00070         self.failIf(index_def['index'] in t.getIndexes(1))
00071         self.failIf(index_def['index'] in t.getIndexDisplay(True).keys())
00072         self.failUnless(index_def['friendlyName'] not in t.getIndexDisplay(True).values())
00073         # Make sure it's still in the un-limited list
00074         self.failUnless(index_def['index'] in t.getIndexDisplay(False).keys())
00075         self.failUnless(index_def['friendlyName'] in t.getIndexDisplay(False).values())
00076         self.failUnless(index_def['index'] in t.getIndexes())
00077 
00078     def test_add_bogus_index(self):
00079         # You can add metadata that's not in the catalog
00080         t = self.tool
00081         t.addIndex('bogosity', enabled = True)
00082         self.failUnless(t.getIndex('bogosity'))
00083         
00084         #Add
00085         t.addIndex('bogosity', enabled = True)
00086         self.failUnless('bogosity' in [a[0] for a in t.getEnabledFields()])
00087         #Add
00088         t.addIndex('bogosity', enabled = True)
00089         self.failUnless('bogosity' in t.getIndexDisplay(True).keys())
00090         #Add
00091         t.addIndex('bogosity', enabled = True)
00092         self.failUnless('bogosity' in t.getIndexes(1))
00093         #Add
00094         t.addIndex('bogosity', enabled = True)
00095         self.failUnless('bogosity' in [i.index for i in t.getEnabledIndexes()])
00096         
00097     def test_remove_index(self):
00098         t = self.tool
00099         t.addIndex(**index_def)
00100         t.removeIndex(index_def['index'])
00101         error = None
00102         try:
00103             index = t.topic_indexes[index_def['index']]
00104         except KeyError:
00105             error = True
00106         self.failUnless(error)
00107 
00108         error = True
00109         try:
00110             index = t.getIndex(index_def['index'])
00111         except AttributeError:
00112             error = False
00113         self.failIf(error)
00114         
00115     def test_update_index(self):
00116         # An index with no criteria set should set all available criteria,
00117         # also changes made using updateIndex should not reset already set
00118         # values
00119         t = self.tool
00120         t.addIndex(enabled = True, **index_def)
00121         t.updateIndex(index_def['index'], criteria = None,
00122                       description = 'New Description')
00123         index = t.getIndex(index_def['index'])
00124         self.failUnless(index.criteria)
00125         self.failUnless(index.criteria != index_def['criteria'])
00126         self.failUnless(index.description == 'New Description')
00127         self.failUnless(index.friendlyName == index_def['friendlyName'])
00128         self.failUnless(index.enabled)
00129 
00130     def test_all_indexes(self):
00131         # Ensure that the tool includes all indexes in the catalog
00132         t = self.tool
00133         cat = getToolByName(self.tool, CatalogTool.id)
00134         indexes = [field for field in cat.indexes()]
00135         init_indexes = list(t.getIndexes())
00136         unique_indexes = [i for i in indexes if i not in init_indexes]
00137         unique_indexes = unique_indexes + [i for i in init_indexes if i not in indexes]
00138         self.failIf(unique_indexes)
00139 
00140     def test_change_catalog_index(self):
00141         t = self.tool
00142         cat = getToolByName(self.tool, CatalogTool.id)
00143         #add
00144         error = True
00145         cat.manage_addIndex('nonsense', 'FieldIndex')
00146         try:
00147             t.getIndex('nonsense')
00148         except AttributeError:
00149             error = False
00150         self.failIf(error)
00151         #remove
00152         error = False
00153         cat.delIndex('nonsense')
00154         try:
00155             t.getIndex('nonsense')
00156         except AttributeError:
00157             error = True
00158         self.failUnless(error)
00159 
00160     #Metadata tests
00161     def test_add_metadata(self):
00162         t = self.tool
00163         t.addMetadata(enabled = True, **meta_def)
00164         meta = t.getMetadata(meta_def['metadata'])
00165         self.failUnlessEqual(meta.index, meta_def['metadata'])
00166         self.failUnlessEqual(meta.friendlyName, meta_def['friendlyName'])
00167         self.failUnlessEqual(meta.description, meta_def['description'])
00168         # Only need to test truth not actual value
00169         self.failUnless(meta.enabled)
00170 
00171         self.failUnless(meta in t.getEnabledMetadata())
00172         self.failUnless(meta_def['metadata'] in t.getMetadataDisplay(True).keys())
00173         self.failUnless(meta_def['friendlyName'] in t.getMetadataDisplay(True).values())
00174         self.failUnless(meta_def['metadata'] in t.getAllMetadata(1))
00175         
00176     def test_disable_metadata(self):
00177         t = self.tool
00178         t.addMetadata(enabled = False, **meta_def)
00179         meta = t.getMetadata(meta_def['metadata'])
00180         self.failUnlessEqual(meta.index, meta_def['metadata'])
00181         self.failUnlessEqual(meta.friendlyName, meta_def['friendlyName'])
00182         self.failUnlessEqual(meta.description, meta_def['description'])
00183         # Only need to test truth not actual value
00184         self.failIf(meta.enabled)
00185 
00186         self.failUnless(meta not in t.getEnabledMetadata())
00187         self.failIf(meta_def['metadata'] in t.getAllMetadata(1))
00188         self.failIf(meta_def['metadata'] in t.getMetadataDisplay(True).keys())
00189         self.failIf(meta_def['friendlyName'] in t.getMetadataDisplay(True).values())
00190         # Make sure it's still in the un-limited list
00191         self.failUnless(meta_def['metadata'] in t.getMetadataDisplay(False).keys())
00192         self.failUnless(meta_def['friendlyName'] in t.getMetadataDisplay(False).values())
00193         self.failUnless(meta_def['metadata'] in t.getAllMetadata())
00194 
00195     def test_add_bogus_metadata(self):
00196         # You can add metdata that's not in the catalog
00197         t = self.tool
00198         t.addMetadata('bogosity', enabled = True)
00199         self.failUnless(t.getMetadata('bogosity'))
00200 
00201         #Add
00202         t.addMetadata('bogosity', enabled = True)
00203         self.failUnless('bogosity' in t.getMetadataDisplay(True).keys())
00204         #Add
00205         t.addMetadata('bogosity', enabled = True)
00206         self.failUnless('bogosity' in t.getAllMetadata(1))
00207         #Add
00208         t.addMetadata('bogosity', enabled = True)
00209         self.failUnless('bogosity' in [i.index for i in t.getEnabledMetadata()])
00210 
00211     def test_remove_metadata(self):
00212         t = self.tool
00213         t.addMetadata(**meta_def)
00214         t.removeMetadata(meta_def['metadata'])
00215         error = None
00216         try:
00217             meta = t.topic_metadata[meta_def['metadata']]
00218         except KeyError:
00219             error = True
00220         self.failUnless(error)
00221 
00222         error = True
00223         try:
00224             meta = t.getMetadata(meta_def['metadata'])
00225         except AttributeError:
00226             error = False
00227         self.failIf(error)
00228         
00229     def test_update_metadata(self):
00230         # Changes made using updateMetadata should not reset already set
00231         # values
00232         t = self.tool
00233         t.addMetadata(enabled = True, **meta_def)
00234         t.updateMetadata(meta_def['metadata'], friendlyName = 'New Name')
00235         meta = t.getMetadata(meta_def['metadata'])
00236         self.failUnless(meta.friendlyName == 'New Name')
00237         self.failUnless(meta.enabled)
00238 
00239     def test_all_metadata(self):
00240         # Ensure that the tool includes all metadata in the catalog
00241         t = self.tool
00242         cat = getToolByName(self.tool, CatalogTool.id)
00243         metadata = [field for field in cat.schema()]
00244         init_metadata = list(t.getAllMetadata())
00245         unique_metadata = [i for i in metadata if i not in init_metadata]
00246         unique_metadata = unique_metadata + [i for i in init_metadata if i not in metadata]
00247         self.failIf(unique_metadata)
00248 
00249     def test_change_catalog_schema(self):
00250         t = self.tool
00251         cat = getToolByName(self.tool, CatalogTool.id)
00252         #add
00253         error = True
00254         cat.manage_addColumn('nonsense')
00255         try:
00256             t.getMetadata('nonsense')
00257         except AttributeError:
00258             error = False
00259         self.failIf(error)
00260         #remove
00261         error = False
00262         cat.delColumn('nonsense')
00263         try:
00264             t.getMetadata('nonsense')
00265         except AttributeError:
00266             error = True
00267         self.failUnless(error)
00268 
00269 tests.append(TestTool)
00270 
00271 import unittest
00272 def test_suite():
00273     suite = unittest.TestSuite()
00274     for test in tests:
00275         suite.addTest(unittest.makeSuite(test))
00276     return suite