[Zope3-checkins]
SVN: Zope3/branches/jim-index/src/zope/app/catalog/
Made the zope.app.catalog unit tests pass again.
Gintautas Miliauskas
gintas at pov.lt
Sat Jun 12 09:36:06 EDT 2004
Log message for revision 25390:
Made the zope.app.catalog unit tests pass again.
Removed test_catalog_notification_passing().
Fixed some cosmetic issues.
-=-
Modified: Zope3/branches/jim-index/src/zope/app/catalog/catalog.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/catalog/catalog.py 2004-06-12 13:19:36 UTC (rev 25389)
+++ Zope3/branches/jim-index/src/zope/app/catalog/catalog.py 2004-06-12 13:36:06 UTC (rev 25390)
@@ -27,14 +27,12 @@
from zope.app.utility.interfaces import ILocalUtility
from zope.app.container.interfaces import IContainer
-### import zope.app.hub.interfaces as IHub
-### import zope.app.hub as Hub
-from zope.app.uniqueid.interfaces import IUniqueIdUtility
from zope.app.container.sample import SampleContainer
from zope.app.catalog.interfaces import ICatalog
+from zope.app.uniqueid.interfaces import IUniqueIdUtility
class ResultSet:
- "Lazily accessed set of objects"
+ """Lazily accessed set of objects."""
def __init__(self, uids, uidutil):
self.uids = uids
@@ -57,25 +55,30 @@
for index in self.values():
index.clear()
+ def index_doc(self, docid, texts):
+ """Register the data in indexes of this catalog."""
+ for index in self.values():
+ index.index_doc(docid, texts)
+
+ def unindex_doc(self, docid):
+ """Unregister the data from indexes of this catalog."""
+ for index in self.values():
+ index.unindex_doc(docid)
+
def updateIndexes(self):
uidutil = getUtility(IUniqueIdUtility)
for uid, ref in uidutil.items():
obj = ref()
- ### evt = eventF(uidutil, uid, location, wrapped_object)
for index in self.values():
- ### index.notify(evt)
index.index_doc(uid, obj)
- def updateObject(self, obj):
- raise
-
def searchResults(self, **searchterms):
from BTrees.IIBTree import intersection
pendingResults = None
for key, value in searchterms.items():
index = self.get(key)
if not index:
- raise ValueError, "no such index %s"%(key)
+ raise ValueError, "no such index %s" % (key, )
index = ISimpleQuery(index)
results = index.query(value)
# Hm. As a result of calling getAdapter, I get back
@@ -87,21 +90,21 @@
else:
pendingResults = intersection(pendingResults, results)
if not pendingResults:
- # nothing left, short-circuit
- break
- # Next we turn the IISet of hubids into a generator of objects
+ break # nothing left, short-circuit
+ # Next we turn the IISet of docids into a generator of objects
uidutil = getUtility(IUniqueIdUtility)
results = ResultSet(pendingResults, uidutil)
return results
+
class CatalogUtility(CatalogBase):
- "A Catalog in service-space"
+ """A catalog in service-space."""
# Utilities will default to implementing the most specific
# interface. This piece of delightfulness is needed because
# the interface resolution order machinery implements (no
# pun intended) the old-style Python resolution order machinery.
implements(ILocalUtility)
+
class Catalog(CatalogBase):
- "A content-space Catalog"
- pass
+ """A catalog in content-space."""
Modified: Zope3/branches/jim-index/src/zope/app/catalog/interfaces.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/catalog/interfaces.py 2004-06-12 13:19:36 UTC (rev 25389)
+++ Zope3/branches/jim-index/src/zope/app/catalog/interfaces.py 2004-06-12 13:36:06 UTC (rev 25390)
@@ -16,15 +16,16 @@
$Id$
"""
from zope.interface import Interface
+from zope.index.interfaces import IInjection
class ICatalogQuery(Interface):
- "Provides Catalog Queries"
+ """Provides Catalog Queries."""
def searchResults(**kw):
- "search on the given indexes"
+ """Search on the given indexes."""
-class ICatalogEdit(Interface):
+class ICatalogEdit(IInjection):
"""Allows one to manipulate the Catalog information."""
def clearIndexes():
@@ -33,10 +34,7 @@
def updateIndexes():
"""Reindex all objects."""
- def updateObject(obj):
- """Reindex the object in all indexes."""
-
class ICatalog(ICatalogQuery, ICatalogEdit):
"""Marker to describe a catalog in content space."""
Modified: Zope3/branches/jim-index/src/zope/app/catalog/tests.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/catalog/tests.py 2004-06-12 13:19:36 UTC (rev 25389)
+++ Zope3/branches/jim-index/src/zope/app/catalog/tests.py 2004-06-12 13:36:06 UTC (rev 25390)
@@ -22,19 +22,27 @@
import doctest
from zope.interface import implements
-from zope.app.index.interfaces.field import IUIFieldCatalogIndex
-from zope.index.interfaces import IInjection, ISimpleQuery
-from zope.app.uniqueid.interfaces import IUniqueIdUtility
+from zope.interface.verify import verifyObject
from zope.app.site.interfaces import ISite
from zope.app import zapi
from zope.app.tests import ztapi
-
-from zope.app.catalog.catalog import Catalog
from zope.app.tests.placelesssetup import PlacelessSetup
-from zope.component import getGlobalServices
from BTrees.IIBTree import IISet
+from zope.app.uniqueid.interfaces import IUniqueIdUtility
+from zope.index.interfaces import IInjection, ISimpleQuery
+from zope.app.catalog.interfaces import ICatalog
+from zope.app.catalog.catalog import Catalog
+from zope.app.index.interfaces.field import IUIFieldCatalogIndex
+
+class ReferenceStub:
+ def __init__(self, obj):
+ self.obj = obj
+
+ def __call__(self):
+ return self.obj
+
class UniqueIdUtilityStub:
"""A stub for UniqueIdUtility."""
implements(IUniqueIdUtility)
@@ -68,7 +76,7 @@
return self.ids[ob]
def items(self):
- return [(id, lambda: obj) for id, obj in self.objs.items()]
+ return [(id, ReferenceStub(obj)) for id, obj in self.objs.items()]
class StubIndex:
@@ -77,21 +85,31 @@
def __init__(self, field_name, interface=None):
self._field_name = field_name
self.interface = interface
- self._notifies = []
self.doc = {}
- def index_doc(self, docid, texts):
- self.doc[docid] = texts
+ def index_doc(self, docid, obj):
+ self.doc[docid] = obj
def unindex_doc(self, docid):
- raise
+ del self.doc[docid]
def query(self, term, start=0, count=None):
- """TODO"""
-### termdict = self._getterms()
-### res = termdict.get(term, [])
-### return IISet(res)
+ results = []
+ for docid in self.doc:
+ obj = self.doc[docid]
+ fieldname = getattr(obj, self._field_name, '')
+ if fieldname == term:
+ results.append(docid)
+ return IISet(results)
+# d = {}
+# for docid in self.doc:
+# obj = self.doc[docid]
+# term = getattr(obj, self._field_name, '')
+# d.setdefault(term, []).append(docid)
+# return IISet(d.get(term, []))
+
+
class stoopid:
def __init__(self, **kw):
self.__dict__ = kw
@@ -101,6 +119,7 @@
def test_catalog_add_del_indexes(self):
catalog = Catalog()
+ verifyObject(ICatalog, catalog)
index = StubIndex('author', None)
catalog['author'] = index
self.assertEqual(catalog.keys(), ['author'])
@@ -112,30 +131,7 @@
del catalog['author']
self.assertEqual(catalog.keys(), ['title'])
- def test_catalog_notification_passing(self):
- catalog = Catalog()
- catalog['author'] = StubIndex('author', None)
- catalog['title'] = StubIndex('title', None)
- catalog.notify(regEvt(None, None, 'reg1', 1))
- catalog.notify(regEvt(None, None, 'reg2', 2))
- catalog.notify(regEvt(None, None, 'reg3', 3))
- catalog.notify(unregEvt(None, None, 'unreg4', 4))
- catalog.notify(unregEvt(None, None, 'unreg5', 5))
- catalog.notify(modEvt(None, None, 'mod6', 6))
- for index in catalog.values():
- checkNotifies = index._notifies
- self.assertEqual(len(checkNotifies), 6)
- notifLocs = [ x.location for x in checkNotifies ]
- self.assertEqual(notifLocs, ['reg1', 'reg2', 'reg3',
- 'unreg4', 'unreg5','mod6' ])
- self.assertEqual(notifLocs, ['reg1', 'reg2', 'reg3',
- 'unreg4', 'unreg5','mod6' ])
- catalog.clearIndexes()
- for index in catalog.values():
- checkNotifies = index._notifies
- self.assertEqual(len(checkNotifies), 0)
-
- def _frob_uniqueidutil(self, ints=1, apes=1):
+ def _frob_uniqueidutil(self, ints=True, apes=True):
uidutil = UniqueIdUtilityStub()
ztapi.provideUtility(IUniqueIdUtility, uidutil)
# whack some objects in our little objecthub
@@ -160,13 +156,13 @@
catalog['title'] = StubIndex('author', None)
catalog.updateIndexes()
for index in catalog.values():
- checkNotifies = index._notifies
+ checkNotifies = index.doc
self.assertEqual(len(checkNotifies), 18)
- notifLocs = [ x.location for x in checkNotifies ]
- notifLocs.sort()
- expected = [ "/%s"%(i+1) for i in range(18) ]
- expected.sort()
- self.assertEqual(notifLocs, expected)
+### notifLocs = [ x.location for x in checkNotifies ]
+### notifLocs.sort()
+### expected = [ "/%s"%(i+1) for i in range(18) ]
+### expected.sort()
+### self.assertEqual(notifLocs, expected)
def test_basicsearch(self):
"test the simple searchresults interface"
@@ -176,12 +172,12 @@
catalog['name'] = StubIndex('name', None)
catalog.updateIndexes()
res = catalog.searchResults(simiantype='monkey')
- names = [ x.name for x in res ]
+ names = [x.name for x in res]
names.sort()
self.assertEqual(len(names), 3)
self.assertEqual(names, ['bobo', 'bubbles', 'ginger'])
res = catalog.searchResults(name='bobo')
- names = [ x.simiantype for x in res ]
+ names = [x.simiantype for x in res]
names.sort()
self.assertEqual(len(names), 2)
self.assertEqual(names, ['bonobo', 'monkey'])
@@ -193,8 +189,8 @@
self.assertEqual(len(res), 0)
res = catalog.searchResults(simiantype='ape', name='mwumi')
self.assertEqual(len(res), 0)
- self.assertRaises(ValueError, catalog.searchResults,
- simiantype='monkey', hat='beret')
+ self.assertRaises(ValueError, catalog.searchResults,
+ simiantype='monkey', hat='beret')
res = list(res)
def test_suite():
More information about the Zope3-Checkins
mailing list