[Zope-Checkins] CVS: Zope/lib/python/Products/ZCatalog/tests - testCatalog.py:1.22.12.6

Casey Duncan casey at zope.com
Tue May 18 10:48:50 EDT 2004


Update of /cvs-repository/Zope/lib/python/Products/ZCatalog/tests
In directory cvs.zope.org:/tmp/cvs-serv25626/lib/python/Products/ZCatalog/tests

Modified Files:
      Tag: Zope-2_7-branch
	testCatalog.py 
Log Message:
Fix KeyError in mergeResults() when merging results sorted using a small index. 
Add mergeResults unit tests.


=== Zope/lib/python/Products/ZCatalog/tests/testCatalog.py 1.22.12.5 => 1.22.12.6 ===
--- Zope/lib/python/Products/ZCatalog/tests/testCatalog.py:1.22.12.5	Tue Mar 23 15:27:23 2004
+++ Zope/lib/python/Products/ZCatalog/tests/testCatalog.py	Tue May 18 10:48:49 2004
@@ -17,6 +17,7 @@
 import os
 import random
 import unittest
+from sets import Set
 
 import ZODB, OFS.Application
 from ZODB.DemoStorage import DemoStorage
@@ -71,6 +72,10 @@
 ##     def getUserName( self ):
 ##         return self._name
 
+def sort(iterable):
+    L = list(iterable)
+    L.sort()
+    return L
 
 class CatalogBase:
     def setUp(self):
@@ -457,7 +462,7 @@
     def __init__(self,num):
         self.number = num
 
-class testRS(unittest.TestCase):
+class TestRS(unittest.TestCase):
 
     def setUp(self):
         self._vocabulary = Vocabulary.Vocabulary('Vocabulary','Vocabulary'
@@ -484,14 +489,110 @@
                 self.assert_(m<=size and size<=n,
                              "%d vs [%d,%d]" % (r.number,m,n))
 
-
+class TestMerge(unittest.TestCase):
+    # Test merging results from multiple catalogs
+    
+    def setUp(self):
+        vocabulary = Vocabulary.Vocabulary(
+            'Vocabulary','Vocabulary', globbing=1)
+        self.catalogs = []
+        for i in range(3):
+            cat = Catalog()
+            cat.addIndex('num', FieldIndex('num'))
+            cat.addIndex('big', FieldIndex('big'))
+            cat.addIndex('title', TextIndex('title'))
+            cat.vocabulary = vocabulary
+            cat.aq_parent = zdummy(16336)
+            for i in range(10):
+                obj = zdummy(i)
+                obj.big = i > 5
+                cat.catalogObject(obj, str(i))
+            self.catalogs.append(cat)
+    
+    def testNoFilterOrSort(self):
+        from Products.ZCatalog.Catalog import mergeResults
+        results = [cat.searchResults(_merge=0) for cat in self.catalogs]
+        merged = mergeResults(results, has_sort_keys=False, reverse=False)
+        expected = []
+        for r in results:
+            expected.extend(r)
+        self.assertEqual(sort(merged), sort(expected))
+    
+    def testSortedOnly(self):
+        from Products.ZCatalog.Catalog import mergeResults
+        results = [cat.searchResults(sort_on='num', _merge=0) 
+                   for cat in self.catalogs]
+        merged_rids = [r.getRID() for r in mergeResults(
+            results, has_sort_keys=True, reverse=False)]
+        expected = []
+        for r in results:
+            expected.extend(r)
+        expected.sort()
+        expected = [rid for sortkey, rid, getitem in expected]
+        self.assertEqual(merged_rids, expected)
+    
+    def testSortReverse(self):
+        from Products.ZCatalog.Catalog import mergeResults
+        results = [cat.searchResults(sort_on='num', _merge=0) 
+                   for cat in self.catalogs]
+        merged_rids = [r.getRID() for r in mergeResults(
+            results, has_sort_keys=True, reverse=True)]
+        expected = []
+        for r in results:
+            expected.extend(r)
+        expected.sort()
+        expected.reverse()
+        expected = [rid for sortkey, rid, getitem in expected]
+        self.assertEqual(merged_rids, expected)
+    
+    def testLimitSort(self):
+        from Products.ZCatalog.Catalog import mergeResults
+        results = [cat.searchResults(sort_on='num', sort_limit=2, _merge=0) 
+                   for cat in self.catalogs]
+        merged_rids = [r.getRID() for r in mergeResults(
+            results, has_sort_keys=True, reverse=False)]
+        expected = []
+        for r in results:
+            expected.extend(r)
+        expected.sort()
+        expected = [rid for sortkey, rid, getitem in expected]
+        self.assertEqual(merged_rids, expected)
+    
+    def testScored(self):
+        from Products.ZCatalog.Catalog import mergeResults
+        results = [cat.searchResults(title='4 or 5 or 6', _merge=0) 
+                   for cat in self.catalogs]
+        merged_rids = [r.getRID() for r in mergeResults(
+            results, has_sort_keys=True, reverse=False)]
+        expected = []
+        for r in results:
+            expected.extend(r)
+        expected.sort()
+        expected = [rid for sortkey, (nscore, score, rid), getitem in expected]
+        self.assertEqual(merged_rids, expected)
+        
+    def testSmallIndexSort(self):
+        # Test that small index sort optimization is not used for merging
+        from Products.ZCatalog.Catalog import mergeResults
+        results = [cat.searchResults(sort_on='big', _merge=0) 
+                   for cat in self.catalogs]
+        merged_rids = [r.getRID() for r in mergeResults(
+            results, has_sort_keys=True, reverse=False)]
+        expected = []
+        for r in results:
+            expected.extend(r)
+        expected.sort()
+        expected = [rid for sortkey, rid, getitem in expected]
+        self.assertEqual(merged_rids, expected)
+    
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest( unittest.makeSuite( TestAddDelColumn ) )
     suite.addTest( unittest.makeSuite( TestAddDelIndexes ) )
     suite.addTest( unittest.makeSuite( TestZCatalog ) )
     suite.addTest( unittest.makeSuite( TestCatalogObject ) )
-    suite.addTest( unittest.makeSuite( testRS ) )
+    suite.addTest( unittest.makeSuite( TestRS ) )
+    suite.addTest( unittest.makeSuite( TestMerge ) )
     return suite
 
 if __name__ == '__main__':




More information about the Zope-Checkins mailing list