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

Casey Duncan casey@zope.com
Mon, 9 Dec 2002 17:37:01 -0500


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

Modified Files:
	testCatalog.py 
Log Message:
More sorting improvements:

* Changed logic for activating first sort algorithm to elminate bad performance with large result sets (20k+). The full sort is now faster for a larger proportion of cases. This algorithm is also skipped now if a sort limit value is passed.

* Full sort now handles sort limits where the limit is 25% or greater of the total result where N-Best performance degrades. This allows the application to always apply a sort limit up to and beyond the result set length.

* Added an "N-worst" sort handler to deal with forward sort limits (previously only reverse limits worked properly).

* Small optimizations to N-best/worst to wring out a few more CPU cycles.



=== Zope/lib/python/Products/ZCatalog/tests/testCatalog.py 1.19 => 1.20 ===
--- Zope/lib/python/Products/ZCatalog/tests/testCatalog.py:1.19	Thu Dec  5 16:17:08 2002
+++ Zope/lib/python/Products/ZCatalog/tests/testCatalog.py	Mon Dec  9 17:37:00 2002
@@ -17,7 +17,7 @@
 from Products.PluginIndexes.TextIndex.Lexicon import  Lexicon
 from Products.PluginIndexes.KeywordIndex.KeywordIndex import KeywordIndex
 
-import whrandom,string, unittest
+import whrandom,string, unittest, random
 
 
 def createDatabase():
@@ -166,6 +166,16 @@
         self.assertEqual(len(sr), 3)
 
 class TestCatalogObject(unittest.TestCase):
+    
+    upper = 1000
+    
+    nums = range(upper)
+    for i in range(upper):
+        j = random.randint(0, upper-1)
+        tmp = nums[i]
+        nums[i] = nums[j]
+        nums[j] = tmp
+        
     def setUp(self):
         self._vocabulary = Vocabulary.Vocabulary('Vocabulary','Vocabulary',
                                                  globbing=1)
@@ -196,7 +206,6 @@
         self._catalog.addColumn('att3')
         self._catalog.addColumn('num')
 
-        self.upper = 1000
         class dummy(ExtensionClass.Base):
             att1 = 'att1'
             att2 = 'att2'
@@ -213,9 +222,9 @@
             def col3(self):
                 return ['col3']
 
-
+        
         for x in range(0, self.upper):
-            self._catalog.catalogObject(dummy(x), `x`)
+            self._catalog.catalogObject(dummy(self.nums[x]), `x`)
         self._catalog.aq_parent = dummy('foo') # fake out acquisition
 
     def tearDown(self):
@@ -353,11 +362,33 @@
         # set is much larger than the sort index.
         a = self._catalog(sort_on='att1')
         self.assertEqual(len(a), self.upper)
+        
+    def testBadSortLimits(self):
+        self.assertRaises(
+            AssertionError, self._catalog, sort_on='num', sort_limit=0)
+        self.assertRaises(
+            AssertionError, self._catalog, sort_on='num', sort_limit=-10)
     
     def testSortLimit(self):
+        full = self._catalog(sort_on='num')
         a = self._catalog(sort_on='num', sort_limit=10)
-        self.assertEqual(a[0].num, self.upper - 1)
+        self.assertEqual([r.num for r in a], [r.num for r in full[:10]])
         self.assertEqual(a.actual_result_count, self.upper)
+        a = self._catalog(sort_on='num', sort_limit=10, sort_order='reverse')
+        rev = [r.num for r in full[-10:]]
+        rev.reverse()
+        self.assertEqual([r.num for r in a], rev)
+        self.assertEqual(a.actual_result_count, self.upper)
+        
+    def testBigSortLimit(self):
+        a = self._catalog(sort_on='num', sort_limit=self.upper*3)
+        self.assertEqual(a.actual_result_count, self.upper)
+        self.assertEqual(a[0].num, 0)
+        a = self._catalog(
+            sort_on='num', sort_limit=self.upper*3, sort_order='reverse')
+        self.assertEqual(a.actual_result_count, self.upper)
+        self.assertEqual(a[0].num, self.upper - 1)
+        
 
 class objRS(ExtensionClass.Base):