Back to index

plone3  3.1.7
TestBase.py
Go to the documentation of this file.
00001 # Copyright (C) 2004 by Dr. Dieter Maurer, Eichendorffstr. 23, D-66386 St. Ingbert, Germany
00002 # see "LICENSE.txt" for details
00003 #       $Id: TestBase.py,v 1.3 2004/08/04 19:29:12 dieter Exp $
00004 '''Test base class.
00005 
00006 The 'AdvancedQuery' tests use 'ZopeTestCase'.
00007 You must install this package separately (under 'lib/python/Testing').
00008 You can get 'ZopeTestCase' from Zope.org.
00009 '''
00010 
00011 from unittest import TestSuite, makeSuite
00012 
00013 from Acquisition import Implicit
00014 
00015 from Testing.ZopeTestCase import ZopeTestCase, installProduct
00016 
00017 installProduct('ZCatalog', 1)
00018 installProduct('ManagableIndex', 1)
00019 installProduct('PluginIndexes', 1)
00020 
00021 class TestCase(ZopeTestCase):
00022   _indexType = 'Managable FieldIndex'
00023   _ReverseOrder = 0
00024   _stringType = 0
00025 
00026   def afterSetUp(self):
00027     folder = self.folder
00028     folder.manage_addProduct['ZCatalog'].manage_addZCatalog('Catalog','')
00029     catalog = self.catalog = folder.Catalog
00030     catalog.manage_addIndex('I1', self._indexType)
00031     catalog.manage_addIndex('I2', self._indexType)
00032     if self._ReverseOrder:
00033       indexes = catalog._catalog.indexes
00034       idx = indexes['I1']; idx.ReverseOrder = 1; idx.clear()
00035       idx = indexes['I2']; idx.ReverseOrder = 1; idx.clear()
00036     if self._stringType:
00037       indexes = catalog._catalog.indexes
00038       idx = indexes['I1']; idx.TermType = 'string'; idx.clear()
00039       idx = indexes['I2']; idx.TermType = 'string'; idx.clear()
00040     self._addObject(folder, 1, 'a', 'A')
00041     self._addObject(folder, 2, 'b', 'A')
00042     self._addObject(folder, 3, 'a', 'B')
00043     self._addObject(folder, 4, 'b', 'B')
00044     self._addObject(folder, 5, None, 'A')
00045     self._addObject(folder, 6, 'c', None)
00046 
00047   def _addObject(self, dest, id, a1, a2):
00048     id = `id`
00049     setattr(dest, id, _Object(id, a1, a2)); obj = getattr(dest, id)
00050     obj.indexObject()
00051 
00052   def _checkQuery(self, query, should):
00053     '''check that the result *query* equals *should*.
00054 
00055     *should* is a sequence of digits (representing ids).
00056     '''
00057     C = self.catalog
00058     return self._check(C.evalAdvancedQuery(query), should)
00059 
00060   def _check(self, result, should, order=True):
00061     c = self.catalog._catalog
00062     ids = [c.paths[r.data_record_id_] for r in result]
00063     if order: ids.sort()
00064     self.assertEqual(''.join(ids), should)
00065 
00066 
00067 
00068 class _Object(Implicit):
00069   def __init__(self, id, a1, a2):
00070     self.id = id
00071     if a1 is not None: self.I1 = a1
00072     if a2 is not None: self.I2 = a2
00073 
00074   def indexObject(self):
00075     self.Catalog.catalog_object(self, uid=self.id)
00076 
00077 
00078 def getSuite(*testClasses, **kw):
00079   prefix= kw.get('prefix','test')
00080   return TestSuite([makeSuite(cl,prefix) for cl in testClasses])
00081