[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/index/text/tests - test_index.py:1.2
Guido van Rossum
guido@python.org
Wed, 4 Dec 2002 07:09:28 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/index/text/tests
In directory cvs.zope.org:/tmp/cvs-serv15680
Modified Files:
test_index.py
Log Message:
Add a functional test of the hub machinery for indexing and
unindexing.
=== Zope3/lib/python/Zope/App/index/text/tests/test_index.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/index/text/tests/test_index.py:1.1 Wed Dec 4 06:10:24 2002
+++ Zope3/lib/python/Zope/App/index/text/tests/test_index.py Wed Dec 4 07:09:28 2002
@@ -18,16 +18,25 @@
from unittest import TestCase, TestSuite, main, makeSuite
-from Zope.App.index.text.interfaces import ISearchableText
-from Zope.App.index.text.index import TextIndex
+from Zope.ComponentArchitecture.GlobalAdapterService import provideAdapter
+from Zope.Event.ObjectEvent import ObjectModifiedEvent
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup import \
PlacefulSetup
+from Zope.App.Traversing.ITraverser import ITraverser
+from Zope.App.Traversing import locationAsUnicode
+
+from Zope.App.OFS.Services.ObjectHub.IHubEvent import \
+ IRegistrationHubEvent, IObjectModifiedHubEvent
from Zope.App.OFS.Services.ObjectHub.HubEvent import \
ObjectRegisteredHubEvent, \
ObjectUnregisteredHubEvent, \
ObjectModifiedHubEvent
+from Zope.App.OFS.Services.ObjectHub.ObjectHub import ObjectHub
+
+from Zope.App.index.text.interfaces import ISearchableText
+from Zope.App.index.text.index import TextIndex
class FakeSearchableObject:
__implements__ = ISearchableText
@@ -36,6 +45,17 @@
def getSearchableText(self):
return self.texts
+class FakeTraverser:
+ __implements__ = ITraverser
+ def __init__(self, object, location):
+ self.__object = object
+ self.__location = location
+ def traverse(self, path):
+ canonical_path = locationAsUnicode(path)
+ if canonical_path == self.__location:
+ return self.__object
+ raise KeyError, (path, canonical_path)
+
class Test(PlacefulSetup, TestCase):
def setUp(self):
@@ -43,7 +63,7 @@
self.index = TextIndex()
self.object = FakeSearchableObject()
- def testEverything(self):
+ def testNotification(self):
event = ObjectRegisteredHubEvent(None, 1000, object=self.object)
self.index.notify(event)
results, total = self.index.query(u"Bruce")
@@ -62,6 +82,32 @@
location="fake",
object=self.object)
self.index.notify(event)
+ self.assertEqual(self.index.query(u"Bruce"), ([], 0))
+ self.assertEqual(self.index.query(u"Sheila"), ([], 0))
+
+ def testHubMachinery(self):
+ # Technically this is a functional test
+ hub = ObjectHub()
+ hub.subscribe(self.index, IRegistrationHubEvent)
+ hub.subscribe(self.index, IObjectModifiedHubEvent)
+ location = "/bruce"
+ traverser = FakeTraverser(self.object, location)
+ provideAdapter(None, ITraverser, lambda dummy: traverser)
+
+ hubid = hub.register(location)
+ results, total = self.index.query(u"Bruce")
+ self.assertEqual(total, 1)
+ self.assertEqual(results[0][0], hubid)
+
+ self.object.texts = [u"Sheila"]
+ event = ObjectModifiedEvent(self.object, location)
+ hub.notify(event)
+ self.assertEqual(self.index.query(u"Bruce"), ([], 0))
+ results, total = self.index.query(u"Sheila")
+ self.assertEqual(total, 1)
+ self.assertEqual(results[0][0], hubid)
+
+ hub.unregister(location)
self.assertEqual(self.index.query(u"Bruce"), ([], 0))
self.assertEqual(self.index.query(u"Sheila"), ([], 0))