Back to index

plone3  3.1.7
testIndexedAttrs.py
Go to the documentation of this file.
00001 #
00002 # IndexedAttrs tests
00003 #
00004 
00005 import os, sys
00006 if __name__ == '__main__':
00007     execfile(os.path.join(sys.path[0], 'framework.py'))
00008 
00009 from Testing import ZopeTestCase
00010 
00011 ZopeTestCase.installProduct('ExtendedPathIndex')
00012 
00013 from Products.ZCatalog.ZCatalog import ZCatalog
00014 from OFS.SimpleItem import SimpleItem
00015 
00016 
00017 class Record:
00018     def __init__(self, **kw):
00019         self.__dict__.update(kw)
00020 
00021 class Dummy(SimpleItem):
00022     def __init__(self, id):
00023         self.id = id
00024     def getCustomPath(self):
00025         return ('', 'custom', 'path')
00026     def getStringPath(self):
00027         return '/string/path'
00028 
00029 
00030 class TestIndexedAttrs(ZopeTestCase.ZopeTestCase):
00031 
00032     def afterSetUp(self):
00033         self.catalog = ZCatalog('catalog')
00034         self.folder._setObject('dummy', Dummy('dummy'))
00035         self.dummy = self.folder.dummy
00036         self.physical_path = '/'.join(self.dummy.getPhysicalPath())
00037         self.custom_path = '/'.join(self.dummy.getCustomPath())
00038         self.string_path = self.dummy.getStringPath()
00039 
00040     def addIndex(self, id='path', extra=None):
00041         self.catalog.addIndex(id, 'ExtendedPathIndex', extra)
00042         return self.catalog.Indexes[id]
00043 
00044     def testAddIndex(self):
00045         self.catalog.addIndex('path', 'ExtendedPathIndex')
00046         try:
00047             self.catalog.Indexes['path']
00048         except KeyError:
00049             self.fail('Failed to create index')
00050 
00051     def testDefaultIndexedAttrs(self):
00052         # By default we don't have indexed_attrs at all
00053         idx = self.addIndex()
00054         self.failIf(hasattr(idx, 'indexed_attrs'))
00055 
00056     def testDefaultIndexSourceNames(self):
00057         # However, getIndexSourceName returns 'getPhysicalPath'
00058         idx = self.addIndex()
00059         self.assertEqual(idx.getIndexSourceNames(), ('getPhysicalPath',))
00060 
00061     def testDefaultIndexObject(self):
00062         # By default PathIndex indexes getPhysicalPath
00063         idx = self.addIndex()
00064         idx.index_object(123, self.dummy)
00065         self.assertEqual(idx.getEntryForObject(123), self.physical_path)
00066 
00067     def testDefaultSearchObject(self):
00068         # We can find the object in the catalog by physical path
00069         self.addIndex()
00070         self.catalog.catalog_object(self.dummy)
00071         self.assertEqual(len(self.catalog(path=self.physical_path)), 1)
00072 
00073     def testDefaultSearchDictSyntax(self):
00074         # PathIndex supports dictionary syntax for queries
00075         self.addIndex()
00076         self.catalog.catalog_object(self.dummy)
00077         self.assertEqual(len(self.catalog(path={'query': self.physical_path})), 1)
00078 
00079     def testExtraAsRecord(self):
00080         # 'extra' can be a record type object
00081         idx = self.addIndex(extra=Record(indexed_attrs='getCustomPath'))
00082         self.assertEqual(idx.indexed_attrs, ('getCustomPath',))
00083 
00084     def testExtraAsMapping(self):
00085         # or a dictionary
00086         idx = self.addIndex(extra={'indexed_attrs': 'getCustomPath'})
00087         self.assertEqual(idx.indexed_attrs, ('getCustomPath',))
00088 
00089     def testCustomIndexSourceNames(self):
00090         # getIndexSourceName returns the indexed_attrs
00091         idx = self.addIndex(extra={'indexed_attrs': 'getCustomPath'})
00092         self.assertEqual(idx.getIndexSourceNames(), ('getCustomPath',))
00093 
00094     def testCustomIndexObject(self):
00095         # PathIndex indexes getCustomPath
00096         idx = self.addIndex(extra={'indexed_attrs': 'getCustomPath'})
00097         idx.index_object(123, self.dummy)
00098         self.assertEqual(idx.getEntryForObject(123), self.custom_path)
00099 
00100     def testCustomSearchObject(self):
00101         # We can find the object in the catalog by custom path
00102         self.addIndex(extra={'indexed_attrs': 'getCustomPath'})
00103         self.catalog.catalog_object(self.dummy)
00104         self.assertEqual(len(self.catalog(path=self.custom_path)), 1)
00105 
00106     def testStringIndexObject(self):
00107         # PathIndex accepts a path as tuple or string
00108         idx = self.addIndex(extra={'indexed_attrs': 'getStringPath'})
00109         idx.index_object(123, self.dummy)
00110         self.assertEqual(idx.getEntryForObject(123), self.string_path)
00111 
00112     def testStringSearchObject(self):
00113         # And we can find the object in the catalog again
00114         self.addIndex(extra={'indexed_attrs': 'getStringPath'})
00115         self.catalog.catalog_object(self.dummy)
00116         self.assertEqual(len(self.catalog(path=self.string_path)), 1)
00117 
00118     def testIdIndexObject(self):
00119         # PathIndex prefers an attribute matching its id over getPhysicalPath
00120         idx = self.addIndex(id='getId')
00121         idx.index_object(123, self.dummy)
00122         self.assertEqual(idx.getEntryForObject(123), 'dummy')
00123 
00124     def testIdIndexObject(self):
00125         # Using indexed_attr overrides this behavior
00126         idx = self.addIndex(id='getId', extra={'indexed_attrs': 'getCustomPath'})
00127         idx.index_object(123, self.dummy)
00128         self.assertEqual(idx.getEntryForObject(123), self.custom_path)
00129 
00130     def testListIndexedAttr(self):
00131         # indexed_attrs can be a list
00132         idx = self.addIndex(id='getId', extra={'indexed_attrs': ['getCustomPath', 'foo']})
00133         # only the first attribute is used
00134         self.assertEqual(idx.getIndexSourceNames(), ('getCustomPath',))
00135 
00136     def testStringIndexedAttr(self):
00137         # indexed_attrs can also be a comma separated string
00138         idx = self.addIndex(id='getId', extra={'indexed_attrs': 'getCustomPath, foo'})
00139         # only the first attribute is used
00140         self.assertEqual(idx.getIndexSourceNames(), ('getCustomPath',))
00141 
00142     def testEmtpyListAttr(self):
00143         # Empty indexed_attrs falls back to defaults
00144         idx = self.addIndex(extra={'indexed_attrs': []})
00145         self.assertEqual(idx.getIndexSourceNames(), ('getPhysicalPath',))
00146 
00147     def testEmtpyStringAttr(self):
00148         # Empty indexed_attrs falls back to defaults
00149         idx = self.addIndex(extra={'indexed_attrs': ''})
00150         self.assertEqual(idx.getIndexSourceNames(), ('getPhysicalPath',))
00151 
00152 
00153 def test_suite():
00154     from unittest import TestSuite, makeSuite
00155     suite = TestSuite()
00156     suite.addTest(makeSuite(TestIndexedAttrs))
00157     return suite
00158 
00159 if __name__ == '__main__':
00160     framework()