[Checkins] SVN: Zope3/trunk/src/zope/app/catalog/ If a callable is used to retrieve the index value it is allways indexed, even

Jürgen Kartnaller juergen at kartnaller.at
Wed Jan 17 08:08:54 EST 2007


Log message for revision 72070:
  If a callable is used to retrieve the index value it is allways indexed, even
  if it is None.
  Changed this and added tests for it.
  
  A value of None is never indexed now.
  

Changed:
  U   Zope3/trunk/src/zope/app/catalog/attribute.py
  U   Zope3/trunk/src/zope/app/catalog/tests.py

-=-
Modified: Zope3/trunk/src/zope/app/catalog/attribute.py
===================================================================
--- Zope3/trunk/src/zope/app/catalog/attribute.py	2007-01-17 12:44:19 UTC (rev 72069)
+++ Zope3/trunk/src/zope/app/catalog/attribute.py	2007-01-17 13:08:54 UTC (rev 72070)
@@ -131,13 +131,14 @@
                 return None
 
         value = getattr(object, self.field_name, None)
+
+        if value is not None and self.field_callable:
+            #do not eat the exception raised below
+            value = value()
+
         if value is None:
             #unindex the previous value!
             super(AttributeIndex, self).unindex_doc(docid)
             return None
 
-        if self.field_callable:
-            #do not eat the exception raised below
-            value = value()
-
         return super(AttributeIndex, self).index_doc(docid, value)

Modified: Zope3/trunk/src/zope/app/catalog/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/catalog/tests.py	2007-01-17 12:44:19 UTC (rev 72069)
+++ Zope3/trunk/src/zope/app/catalog/tests.py	2007-01-17 13:08:54 UTC (rev 72070)
@@ -371,10 +371,35 @@
         ob1.author = None
         catalog.index_doc(ob1id, ob1)
         
+        #the index must be empty now because None values are never indexed
+        res = catalog.searchResults(author=(None, None))
+        self.assertEqual(len(res), 0)
+    
+    def test_updateIndexFromCallableWithNone(self):
+        uidutil = IntIdsStub()
+        ztapi.provideUtility(IIntIds, uidutil)
+        
+        catalog = Catalog()
+        index = FieldIndex('getAuthor', None, field_callable=True)
+        catalog['author'] = index
+        
+        ob1 = stoopidCallable(author = "joe")
+        
+        ob1id = uidutil.register(ob1)
+        catalog.index_doc(ob1id, ob1)
+        
         res = catalog.searchResults(author=('joe','joe'))
         names = [x.author for x in res]
-        #joe must not be here anymore
-        self.assertEqual(len(names), 0)
+        names.sort()
+        self.assertEqual(len(names), 1)
+        self.assertEqual(names, ['joe'])
+        
+        ob1.author = None
+        catalog.index_doc(ob1id, ob1)
+        
+        #the index must be empty now because None values are never indexed
+        res = catalog.searchResults(author=(None, None))
+        self.assertEqual(len(res), 0)
 
 class stoopidCallable(object):
     def __init__(self, **kw):



More information about the Checkins mailing list