[Zope3-checkins] CVS: Zope3/src/zope/app/index/text/tests - __init__.py:1.2 test_batchedrankedprocessor.py:1.2 test_index.py:1.2

Jim Fulton jim@zope.com
Wed, 25 Dec 2002 09:13:57 -0500


Update of /cvs-repository/Zope3/src/zope/app/index/text/tests
In directory cvs.zope.org:/tmp/cvs-serv15352/src/zope/app/index/text/tests

Added Files:
	__init__.py test_batchedrankedprocessor.py test_index.py 
Log Message:
Grand renaming:

- Renamed most files (especially python modules) to lower case.

- Moved views and interfaces into separate hierarchies within each
  project, where each top-level directory under the zope package
  is a separate project.

- Moved everything to src from lib/python.

  lib/python will eventually go away. I need access to the cvs
  repository to make this happen, however.

There are probably some bits that are broken. All tests pass
and zope runs, but I haven't tried everything. There are a number
of cleanups I'll work on tomorrow.



=== Zope3/src/zope/app/index/text/tests/__init__.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:56 2002
+++ Zope3/src/zope/app/index/text/tests/__init__.py	Wed Dec 25 09:12:56 2002
@@ -0,0 +1,2 @@
+#
+# This file is necessary to make this directory a package.


=== Zope3/src/zope/app/index/text/tests/test_batchedrankedprocessor.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:56 2002
+++ Zope3/src/zope/app/index/text/tests/test_batchedrankedprocessor.py	Wed Dec 25 09:12:56 2002
@@ -0,0 +1,96 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+##############################################################################
+"""
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from zope.app.tests.placelesssetup import PlacelessSetup
+
+from zope.interface.verify import verifyObject
+
+from zope.component import getAdapter
+
+from zope.textindex.textindexinterfaces import IQuerying
+from zope.app.interfaces.index.interfaces import IBatchedQuery, ITextIndexQuery, \
+    IBatchedResult, IRankedHubIdList
+
+from zope.app.index.text.processors import \
+    BatchedRankedProcessor, IBatchedRankedProcessor
+from zope.app.index.queries import BatchedRankedResult
+from zope.app.index.text.queries import BatchedTextIndexQuery
+
+class StupidTextIndex:
+
+    __implements__ = IQuerying
+
+    def __init__(self, returnvalue):
+        self.returnvalue = returnvalue
+
+    def query(self, querytext, start, count):
+        """a stub query processor"""
+        self.args = (querytext, start, count)
+
+        return self.returnvalue
+
+#############################################################################
+# If your tests change any global registries, then uncomment the
+# following import and include CleanUp as a base class of your
+# test. It provides a setUp and tearDown that clear global data that
+# has registered with the test cleanup framework.  Don't use this
+# tests outside the Zope package.
+
+# from zope.testing.cleanup import CleanUp # Base class w registry cleanup
+
+#############################################################################
+
+class Test(PlacelessSetup, TestCase):
+
+    def _Test__new(self):
+        return BatchedRankedProcessor()
+
+    ############################################################
+    # Interface-driven tests:
+
+    def test_IVerify(self):
+        processor = BatchedRankedProcessor(StupidTextIndex(None))
+        verifyObject(IBatchedRankedProcessor, processor)
+
+    def test_parameter(self):
+        query = BatchedTextIndexQuery(u"test AND foo OR bar", 0, 20)
+        index = StupidTextIndex(([], 0))
+
+        processor = BatchedRankedProcessor(index)
+        result = processor(query)
+
+        # Do introspection for parameterpassing on the index
+        self.failUnlessEqual(index.args, (u"test AND foo OR bar", 0, 20))
+
+        # Do introspection on the result
+
+        # BatchedResult
+        batch = getAdapter(result, IBatchedResult)
+        self.failUnlessEqual(0, batch.totalSize)
+        self.failUnlessEqual(0, batch.startPosition)
+        self.failUnlessEqual(20, batch.batchSize)
+
+        # RankedHubIdList
+        list = getAdapter(result, IRankedHubIdList)
+        self.failUnlessRaises(IndexError, list.__getitem__, 0)
+
+def test_suite():
+    return makeSuite(Test)
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Zope3/src/zope/app/index/text/tests/test_index.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:13:56 2002
+++ Zope3/src/zope/app/index/text/tests/test_index.py	Wed Dec 25 09:12:56 2002
@@ -0,0 +1,155 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tests for text index.
+
+$Id$
+"""
+
+import unittest
+
+from zope.component.adapter import provideAdapter
+from zope.app.event.objectevent import ObjectModifiedEvent
+
+from zope.app.services.tests.placefulsetup import \
+     PlacefulSetup
+
+from zope.app.interfaces.traversing.traverser import ITraverser
+from zope.app.traversing import locationAsUnicode
+
+from zope.app.interfaces.services.hub import \
+     IRegistrationHubEvent, IObjectModifiedHubEvent
+from zope.app.services.hub import \
+     ObjectRegisteredHubEvent, \
+     ObjectUnregisteredHubEvent, \
+     ObjectModifiedHubEvent
+from zope.app.services.hub import ObjectHub
+
+from zope.app.interfaces.index.text.interfaces import ISearchableText
+from zope.app.index.text.index import TextIndex
+
+
+class FakeSearchableObject:
+    __implements__ = ISearchableText
+    def __init__(self):
+        self.texts = [u"Bruce"]
+    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)
+
+Bruce = u"Bruce"
+Sheila = u"Sheila"
+
+class Test(PlacefulSetup, unittest.TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        self.index = TextIndex()
+        self.object = FakeSearchableObject()
+
+    def assertPresent(self, word, docid):
+        results, total = self.index.query(word)
+        self.assertEqual(total, 1)
+        self.assertEqual(results[0][0], docid)
+
+    def assertAbsent(self, word):
+        self.assertEqual(self.index.query(word), ([], 0))
+
+    def testNotification(self):
+        docid = 1000
+        event = ObjectRegisteredHubEvent(None, docid, object=self.object)
+        self.index.notify(event)
+        self.assertPresent(Bruce, docid)
+
+        self.object.texts = [Sheila]
+        event = ObjectModifiedHubEvent(None, docid, object=self.object)
+        self.index.notify(event)
+        self.assertPresent(Sheila, docid)
+        self.assertAbsent(Bruce)
+
+        event = ObjectUnregisteredHubEvent(None, docid,
+                                           location="fake",
+                                           object=self.object)
+        self.index.notify(event)
+        self.assertAbsent(Bruce)
+        self.assertAbsent(Sheila)
+
+    def testNotIndexing(self):
+        docid = 1000
+        self.object.texts = None
+        event = ObjectRegisteredHubEvent(None, docid, object=self.object)
+        self.index.notify(event)
+        self.assertEqual(self.index.documentCount(), 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)
+        self.assertPresent(Bruce, hubid)
+
+        self.object.texts = [Sheila]
+        event = ObjectModifiedEvent(self.object, location)
+        hub.notify(event)
+        self.assertPresent(Sheila, hubid)
+        self.assertAbsent(Bruce)
+
+        hub.unregister(location)
+        self.assertAbsent(Bruce)
+        self.assertAbsent(Sheila)
+
+    def testBootstrap(self):
+        self.assertEqual(self.index.isSubscribed(), False)
+        self.assertAbsent(Bruce)
+        self.assertAbsent(Sheila)
+
+        hub = ObjectHub()
+        location = "/bruce"
+        traverser = FakeTraverser(self.object, location)
+        provideAdapter(None, ITraverser, lambda dummy: traverser)
+        hubid = hub.register(location)
+        self.index.subscribe(hub)
+        self.assertEqual(self.index.isSubscribed(), True)
+        self.assertPresent(Bruce, hubid)
+
+        self.index.unsubscribe(hub)
+        self.assertEqual(self.index.isSubscribed(), False)
+        self.assertPresent(Bruce, hubid)
+
+        self.object.texts = [Sheila]
+        event = ObjectModifiedEvent(self.object, location)
+        hub.notify(event)
+        self.assertPresent(Bruce, hubid)
+        self.assertAbsent(Sheila)
+
+
+def test_suite():
+    return unittest.makeSuite(Test)
+
+if __name__=='__main__':
+    unittest.main(defaultTest='test_suite')