[Zope3-checkins] CVS: Zope3/src/zope/app/index/field/tests - test_index.py:1.3
Marius Gedminas
mgedmin@codeworks.lt
Mon, 23 Jun 2003 12:45:09 -0400
Update of /cvs-repository/Zope3/src/zope/app/index/field/tests
In directory cvs.zope.org:/tmp/cvs-serv9136/src/zope/app/index/field/tests
Modified Files:
test_index.py
Log Message:
FieldIndex now can adapt objects to a specific interface before indexing them.
For example, now you can create an index that adapts objects to IZopeDublinCore
and indexes their 'title' attributes.
=== Zope3/src/zope/app/index/field/tests/test_index.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/index/field/tests/test_index.py:1.2 Thu May 1 15:35:19 2003
+++ Zope3/src/zope/app/index/field/tests/test_index.py Mon Jun 23 12:44:38 2003
@@ -18,21 +18,42 @@
import unittest
+from zope.interface import Interface, Attribute, implements
+from zope.interface.verify import verifyObject
from zope.app.event.objectevent import ObjectModifiedEvent
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.traversing import traverse
from zope.component import getService
+from zope.component.adapter import provideAdapter
from zope.app.services.servicenames import HubIds
from zope.app.interfaces.services.hub import \
IRegistrationHubEvent, IObjectModifiedHubEvent
from zope.app.services.hub import ObjectRegisteredHubEvent
from zope.app.index.field.index import FieldIndex
+from zope.app.interfaces.index.field import IUIFieldIndex
+
+class ISomeInterface(Interface):
+ someField = Attribute("")
+
+class ISomeOtherInterface(Interface):
+ someField = Attribute("")
class FakeSearchableObject:
def __init__(self):
self.zope3 = 42
+ def someMethod(self):
+ return 24
+
+class SomeAdapter:
+
+ implements(ISomeInterface)
+
+ def __init__(self, context):
+ self.context = context
+ self.someField = context.zope3 + context.someMethod()
+
Bruce = 42
Sheila = u"Sheila"
@@ -40,6 +61,7 @@
def setUp(self):
PlacefulSetup.setUp(self)
+ provideAdapter(None, ISomeInterface, SomeAdapter)
self.buildFolders()
self.index = FieldIndex('zope3')
self.rootFolder.setObject('myIndex', self.index)
@@ -54,6 +76,21 @@
def assertAbsent(self, value):
self.assertEqual(len(self.index.search(value)), 0)
+
+ def testInterface(self):
+ verifyObject(IUIFieldIndex, self.index)
+
+ def testGetValue(self):
+ idx = FieldIndex('zope3')
+ self.assertEqual(idx._getValue(self.object), 42)
+ idx = FieldIndex('someMethod')
+ self.assertEqual(idx._getValue(self.object), 24)
+ idx = FieldIndex('someField', ISomeInterface)
+ self.assertEqual(idx._getValue(self.object), 42 + 24)
+ idx = FieldIndex('nonexistent')
+ self.assertEqual(idx._getValue(self.object), None)
+ idx = FieldIndex('someField', ISomeOtherInterface)
+ self.assertEqual(idx._getValue(self.object), None)
def testNotIndexing(self):
docid = 1000